0
votes

I'm trying to convert pixel (4 channels) to it's grayscale value.

Image looks like a jet colormap:

enter image description here

and I am trying to convert it to this:

enter image description here

When I'll have it converted, I want to set transparency to the original by it's grayscale value. My conversion looks like this:

uchar v = (inMat.at<cv::Vec4b>(i,j)[0]*0.07f) + 
          (inMat.at<cv::Vec4b>(i,j)[1]*0.72f) + 
          (inMat.at<cv::Vec4b>(i,j)[2]*0.21f);

I thought these values (0,07;0,72;0,21) will convert them, but it convert the pixel to something which looks more like this:

enter image description here

What values should I use to convert it right way?

3
It looks like it is converted correctly. It is the source image that has gradient towards both ends. Maybe you wanted to map color values to gray levels instead?user7860670
@VTT I know, but I need gradient only toward one end, like in second image - darkest blue will be black, darkest red will be whiteBrykyz
If you want to map values then you will need to iterate from left to right and map each color to index like (0,0,143) -> 0; (0,0,159) -> 1; ...user7860670
sounds like you want to convert from RGB to HSV and use the hue value (maybe?)kmdreko
standard grayscale conversion converts the brightness of some RGB value to a single value. A dark red is quite dark and a dark blue is quite dark, so your result makes sense. To convert the top image to some black-to-white gradient I would use a LUT and just iteratore over a single row of your top image, adding the pixel values to the LUT and the iteration index (from 0 to 255 or from 0 to 1, depending on your result type) as the result of your LUT. Same as VTT says.Micka

3 Answers

2
votes

The Jet colormap as many other colormaps, will map a greyscale value to a color, however they are not necessary related that you can convert it to greyscale and get the initial value.

If you want to get the mapping from jet colormap to greyscale you can do the following:

cv::Mat a(255, 1, CV_8U), b;
for (int i = 0; i < 255; ++i)
{
  a.at<uchar>(i) = i;
}
cv::applyColorMap(a,b,cv::COLORMAP_JET);

Now you have like a mapping between Jet color map and the greyscale value... You just need to find the color in b and the position tells you the equivalent in a. Not sure what is the fastest way to do find the color, but maybe a mapping like from the color in hex way to the greyscale value will be good.

0
votes

Basically your picture is a hue ramp, from 0 -> 240 degrees (not a full circle, as there is no purple in it). So you'll need to find a rgb -> hue formula. For example, try the answers of this question out.

Once, you've found the hue value, brightness is hue/240.0;

And maybe you'll need to apply gamma correction to the resulting image.

-3
votes

I don't know open CV but for gray-scale you just average r,g,b

Average = (R + G + B) / 3
R = Average
G = Average
B = Average