0

I can't seem to convert RGB to YCrCb in the new OpenCV python API (cv2). When I run this code:

img = cv2.imread('img1.jpg')
imgYCC = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb)

, I get this error:

AttributeError: 'module' object has no attribute 'COLOR_RGB2YCrCb'

What am I doing wrong?

2
  • The attribute is CV_RGB2YCrCb, not COLOR_RGB2YCrCb Commented May 8, 2013 at 14:41
  • In python it's different than other languages. For example, COLOR_RGB2LAB works fine Commented May 8, 2013 at 14:47

1 Answer 1

6

The attribute name is COLOR_RGB2YCR_CB for RGB ordering.

Bear in mind that OpenCV natively uses BGR color ordering, not RGB, in which case the attribute is COLOR_BGR2YCR_CB. So, you may want to modify your code:

img = cv2.imread('img1.jpg')
imgYCC = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)
Sign up to request clarification or add additional context in comments.

6 Comments

Regardless of what OpenCV uses natively, .jpg images are RGB.
@martineau Interesting observation. Just for clarification: cv2.imread() loads color images into memory with BGR channel ordering, even if the file format specifies otherwise.
@Aurelius: Hmmm, I wonder why then they have different source colorspace conversion constants if it's always going to be from BGR?
@martineau It may not always be from BGR. There are other ways of getting an image into memory, and these may have a different color space, hence the multiple source formats. Not to mention having the ability to freely convert between color spaces as one may require.
In that case it might make more sense to use one of those "other ways" to get the natively RGB .jpg image into memory in that format and then do just one colorspace conversion rather than doing two of them.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.