1

Im using OpenCV and I have a Mat object of size 1024*1024(extracted from a photo and manipulated) and the values are in the range [1..25].for example:

Mat g;
g=[1,5,2,14,13,5,22,24,5,13....;
21,12,...;
..
.];

I want to represent these values as an image.It is only an illustration image to show the different areas,each area with a color. For example: all the values that equals 1=red, all the values that equals 14=blue, and so on..

and then construct and display this photo.

Anybody have an idea how should i proceed?

Thanks!

2 Answers 2

1

If you are not too fussed what colors you get, you can scale your data (so it almost fills the 0 to 255 range) then use an inbuilt colormap. e.g.

cv::Mat g = ...
cv::Mat image;
cv::applyColorMap(g * 10, image, COLORMAP_RAINBOW);

See the applyColorMap() doco

Sign up to request clarification or add additional context in comments.

3 Comments

I tried it but i get a black image when i try to use colormaps i don't know why..thanks anyway :)
@triple13 The rainbow colormap doesn't even contain black so you have done something really wrong.
Yes i know. The problem i think is the type of my Mat, because if i load a normal image using imread and then apply colormaps it works fine
0

there are colormaps , but they won't help if your data is in the [0..25] range only. so you probably ned to roll your own version of that:

   Vec3b lut[26] = { 
        Vec3b(0,0,255),
        Vec3b(13,255,11),
        Vec3b(255,22,1),
        // all the way down, you get the picture, no ?
   };

   Mat color(w,h,CV_8UC3);
   for ( int y=0; y<h; y++ ) {   
       for ( int x=0; x<w; x++ ) {
           color.at<Vec3b>(y,x) = lut[ g.at<uchar>(y,x) ];   
          // check the type of "g" please, i assumed CV_8UC1 here. 
          // if it's CV_32S, use g.at<int>  , i.e, you need the right type here 
       }
   }

1 Comment

@berak..thanks so much this code works perfectly. Although i had to choose the elements values of 'lut' manually using suitable pixels values, but it is ok as long as it works fine. When i tried to use colormaps i had a black image and i don't know why.maybe it is because of the type of Mat or something else but no problem i used the other way anyway. Thanks again :)

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.