How are RGB images converted to greyscale?

asked 2018-07-30 22:45:44 -0500

Given 3 images of pure red, pure green, and pure blue, once converted to greyscale I was surprised to find they're not the same shade of grey.

I suspect this is the desired behaviour and my initial assumption that they'd be identical once converted to greyscale was wrong. But now I'm curious to know how this conversion to greyscale takes place. How does OpenCV decide what colour maps to what shade of grey?

Here is how I create the 3 sample colour images:

cv::Mat b(60, 80, CV_8UC3, cv::Scalar(255   , 0     , 0     )); // blue
cv::Mat g(60, 80, CV_8UC3, cv::Scalar(0     , 255   , 0     )); // green
cv::Mat r(60, 80, CV_8UC3, cv::Scalar(0     , 0     , 255   )); // red
cv::imwrite("b.jpg", b, {cv::IMWRITE_JPEG_QUALITY, 75, cv::IMWRITE_JPEG_OPTIMIZE, 1});
cv::imwrite("g.jpg", g, {cv::IMWRITE_JPEG_QUALITY, 75, cv::IMWRITE_JPEG_OPTIMIZE, 1});
cv::imwrite("r.jpg", r, {cv::IMWRITE_JPEG_QUALITY, 75, cv::IMWRITE_JPEG_OPTIMIZE, 1});

Then to convert to greyscale I used this on each of the images:

cv::cvtColor(src, dst, cv::COLOR_BGR2GRAY);

The end result is this:

  • red converted to greyscale is: greyscale red
  • green converted to greyscale is: greyscale green
  • blue converted to greyscale is: image description
edit retag flag offensive close merge delete

Comments

3