6

using open cv python i am trying to convert an rgb image to ycbcr using cv2.cvtclor.

the error is name 'CV_BGR2YCrCb' is not defined

Can anyone suggest few ideas.

user3590669
  • 61
  • 1
  • 1
  • 2
  • 1
    Please show the function that is throwing the error so that people will be able to help figure out what is going wrong. – krillgar Apr 30 '14 at 19:46
  • possible duplicate of [How does one convert a grayscale image to RGB in OpenCV (Python) for visualizing contours after processing an image in binary?](http://stackoverflow.com/questions/21596281/how-does-one-convert-a-grayscale-image-to-rgb-in-opencv-python-for-visualizing) – Aurelius Apr 30 '14 at 19:55

3 Answers3

15

You need to do this:

imgYCC = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB)

The attribute name is COLOR_BGR2YCR_CB not CV_BGR2YCrCb

Khawar Ali
  • 3,462
  • 4
  • 27
  • 55
  • And just to save some entropy due to an additional google search, you can imgRGB = cv2.cvtColor(imgYCC, cv2.COLOR_YCR_CB2BGR) to convert the image back to RGB space – Eduardo Pignatelli Apr 24 '18 at 10:28
5

OpenCV's Python bindings don't use the same flag values as the C++ constants (See this other answer for a little more detail. The correct flag value to pass is cv2.COLOR_BGR2YCR_CB. You would call cvtColor like this:

im = cv2.cvtColor(bgr, cv2.COLOR_BGR2YCR_CB)
Community
  • 1
  • 1
Aurelius
  • 11,111
  • 3
  • 52
  • 69
  • You mean "flag names" and not "flag values". The values are, actually, the same: 36 (and 37 for RGB2YCrCb): ./imgproc.hpp:580: COLOR_BGR2YCrCb = 36, //!< convert RGB/BGR to luma-chroma (aka YCC), @ref color_convert_rgb_ycrcb "color conversions" ./imgproc/types_c.h:159: CV_BGR2YCrCb =36, - and now try `import cv2; print(cv2.COLOR_BGR2YCR_CV)` – Tomasz Gandor Feb 16 '20 at 20:36
1

OpenCV reads image as BGR so if you need RGB image then you have to convert image into its RGB form then you can perform your tasks You can use these as below

YCrCb = cv2.cvtColor(img, cv2.COLOR_BGR2YCR_CB) (# if input image is BGR)  
YCrCb = cv2.cvtColor(img, cv2.COLOR_RGB2YCrCb) (# if input image is RGB) 
Nikolai Shevchenko
  • 7,083
  • 8
  • 33
  • 42