简体   繁体   中英

building an image from Mat object in OpenCV

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!

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. eg

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

See the applyColorMap() doco

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 
       }
   }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM