在OpenCV中从Mat对象构建图像

building an image from Mat object in OpenCV

本文关键字:构建 图像 对象 Mat OpenCV 中从      更新时间:2023-10-16

我使用 OpenCV 并且我有一个大小为 1024*1024 的 Mat 对象(从照片中提取并进行处理),值在 [1..25] 范围内。

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

我想将这些值表示为图像。它只是一个插图图像来显示不同的区域,每个区域都有一种颜色。例如:所有等于 1=红色的值,所有等于 14=蓝色的值,依此类推。

然后构建并显示这张照片。

有人知道我应该如何进行吗?

谢谢!

如果你不太在意你得到什么颜色,你可以缩放你的数据(所以它几乎填满了0到255的范围),然后使用内置的颜色图。例如

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

请参阅 applyColorMap() doco

有 颜色图 ,但如果您的数据仅在 [0..25] 范围内,它们将无济于事。 所以你可能已经推出了你自己的版本:

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