Ask Your Question
0

RGB vs. BGR

asked 2014-04-08 21:38:35 -0500

João M. S. Silva gravatar image

updated 2014-04-08 22:09:28 -0500

If I capture a frame and then write it to JPG, like this:

VideoCapture capture = VideoCapture(0); capture.read(frame); imwrite("frame.jpg", frame);

ImageMagick's identify says frame.jpg is Colorspace: sRGB.

I made this experiment because I read that OpenCV's default is BGR, not RGB. But it seems to produce RGB JPG files.

Can I use CV_RGB2GRAY in cvtColor always and be safe? Is RGB OpenCV's default?

edit retag flag offensive close merge delete

Comments

Anyone?

Any definitive references about the "eternal" RGB vs BGR?

João M. S. Silva gravatar imageJoão M. S. Silva ( 2014-04-17 13:52:10 -0500 )edit

3 answers

Sort by » oldest newest most voted
0

answered 2017-01-05 08:13:18 -0500

You need to use BGR2GRAY unless you are sure that the image you are processing is in RGB format (usually OpenCV stores images in BGR format). The fomula for calculating grayscale which OpenCV uses assigns different weights to blue and red pixels. It is as follows (see the [[http://docs.opencv.org/master/de/d25/imgproc_color_conversions.html#color_convert_rgb_gray documentation]]:

Gray = 0.299 * Red + 0.587 * Green + 0.114 * Blue

Thus if you use RGB2GRAY on a BGR picture you will get the wrong grayscale values.

edit flag offensive delete link more
0

answered 2014-12-07 23:49:54 -0500

rafaoc gravatar image

When you use the imread() opencv function to read an image. It will read a color image as BGR format, so use better BGR2gray for that transformation.

edit flag offensive delete link more
0

answered 2014-12-07 19:09:06 -0500

As far as I can tell, BGR vs RGB only seems to matter if you're manipulating pixels directly in your code. So, if you have an image you've made, or if you're coming up with a coloring scheme for your polygons or what have you, you need to worry about it. But given that there's no built in method for checking the formatting, it seems that effectively OpenCV assumes it all is. Whenever you load an image, OpenCV "thinks" of it as being in BGR, even if it's an RGB ppm file. It just loads the first member of the tuple into the third, and vice versa. So, BGR is the default in OpenCV (which is really weird, IMO) and the only time it matters as far as I can tell is if you're fiddling with pixels directly, which should be fairly rare and would explain why the RGB standard is ignored. In fairness, I'm far from a definitive expert on the topic, but this comes from me fiddling with OpenCV for a few weeks trying to get a good handle on it!

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

Stats

Asked: 2014-04-08 21:38:35 -0500

Seen: 7,989 times

Last updated: Dec 07 '14