openCV:从矩阵绘制色彩映射 [英] openCV: Plotting a colormap from a matrix

查看:868
本文介绍了openCV:从矩阵绘制色彩映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个50 x 50的矩阵,具有从1到50的某些值。我需要绘制一个使用OpenCV相同的颜色图。



我已经在MATLAB中完成了这项工作(在 this answer ):

 > A = randi([10,60],100,100); 
>> colormap('hot')
>> imagesc(A)
>> colorbar

输出如。



我想使用openCV做同样的事情。



如何处理?



我无法理解如何实现hot的功能,看起来像这样(只看热)





也将有兴趣放置如第一图像中所示的颜色条。






更新(我正在运行的最终代码)


$不幸的是,我无法绘制正确的颜色图:这是MATLAB代码:



注意:A是一个10乘100的矩阵。



A = [0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9;
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9;
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9;
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0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0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9;
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9;
]



colormap('hot');
imagec(A)
colorbar



这里是输出:



img src =http://i.stack.imgur.com/uJEFQ.pngalt =enter image description here>



现在我创建了一个文本文件具有相同的数据,我的文本文件如下所示:





并使用以下代码实现与MATLAB相同的结果:

  int main()


{




ifstream myfile.txt);

vector< int> nums;
while(!Read.eof()){
int n;
读取>> n;
nums.push_back(n);
}
//现在从向量中创建一个Mat:
Mat mat(nums);

cout<< mat<< endl; // for testing


Mat mat1d(nums);
Mat mat2d = mat1d.reshape(1,10);
//
Mat image; //创建空图像。 (你可以留空;)
////应用颜色映射,但是在2d垫上,而不是2d矢量,请:
applyColorMap(mat2d,image,COLORMAP_JET);
//显示结果:
imshow(colormap,image);
waitKey(0);
return 0;
}

但是上面的输出没有意义。



我需要匹配MATLAB和Opencv的结果。

解决方案

您必须重新塑造1d向量,以2d格式:

  // 
//在做任何事情之前,清理你的DAMN TXTFILE。
//它应该不包含数字和空格。
ifstream Read(m.txt);

//对这里的类型额外挑逗。
//我们正在读一个8位灰度图。
vector< uchar> nums;
for(int i = 0;(i <100 * 10)&&(!Read.eof()); i ++){
int n;
读取>> n;
nums.push_back(n);
}

//现在从向量中创建一个Mat:
Mat mat1d(nums);
Mat mat2d = mat1d.reshape(1,10);

//您的数据在[0..9]范围内,因此可以扩展到[0..255] uchar范围
mat2d * =(255/10);

cout<< mat2d<< endl; // for testing
//
Mat image; //创建空图像。 (你可以留空;)
////应用颜色映射,但是在2d垫上,而不是2d矢量,请:
applyColorMap(mat2d,image,COLORMAP_HOT);
//显示结果:
imshow(colormap,image);
waitKey(0);


I have a 50 x 50 matrix having certain values from 1 to 50. I need to plot a color map for the same using OpenCV .

I have already done this in MATLAB (with help from this answer):

>> A = randi([10,60],100,100);
>> colormap('hot')
>> imagesc(A)
>> colorbar    

And the output comes like .

I would like to do the same using openCV.

How do I proceed for this ?

I am unable to understand how do I implement the function of "hot" which appears like this (see only hot)

:

Additionally I would also be interested in putting a color bar as shown in the first image.


Update (Final code I am running)

Unfortunately still I am unable to plot the correct color map: Here is the MATLAB code:

Note : A is a 10 by 100 matrix.

A=[0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 ; 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9; 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9; 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 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 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9; 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9; ]

colormap('hot'); imagesc(A) colorbar

And here is the output:

Now I created a text file with same data, and my text file looks like this:

And used the following code to achieve the same result as of MATLAB:

   int main() 


{




ifstream Read("myfile.txt");

vector<int> nums;
while ( !Read.eof() ) {
    int n;
    Read >> n;
    nums.push_back(n);
}
// now make a Mat from the vector:
Mat mat(nums);

cout<<mat<<endl; //for testing 


Mat mat1d(nums);
Mat mat2d = mat1d.reshape(1, 10);
//
Mat image; //create an empty image. (you can leave it empty ;)
//// Apply the colormap, but on the 2d mat, not on the 2d vector, please:
applyColorMap( mat2d, image, COLORMAP_JET );
            // Show the result:
imshow("colormap", image);
waitKey(0);
return 0;
}

But the output from the above is meaningless.

I need to match the results from MATLAB and Opencv.

解决方案

you will have to reshape the 1d vector, to a 2d mat :

    //
    // before doing anything else, CLEAN UP YOUR DAMN TXTFILE. 
    // it should contain nothing than numbers and spaces.
    ifstream Read("m.txt");

    // be extra picky about the type here.
    // we are reading in a 8bit grayscale map.
    vector<uchar> nums;
    for (int i=0; (i<100*10)&&(!Read.eof()); i++ ) {
        int n;
        Read >> n;
        nums.push_back(n);
    }

    // now make a Mat from the vector:
    Mat mat1d(nums);
    Mat mat2d = mat1d.reshape(1, 10);

    // your data is in the [0..9] range, so scale up to [0..255] uchar range
    mat2d *= (255/10); 

    cout<<mat2d<<endl; //for testing 
    //
    Mat image; //create an empty image. (you can leave it empty ;)
    //// Apply the colormap, but on the 2d mat, not on the 2d vector, please:
    applyColorMap( mat2d, image, COLORMAP_HOT );
                // Show the result:
    imshow("colormap", image);
    waitKey(0);

这篇关于openCV:从矩阵绘制色彩映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆