Ask Your Question
0

How to use the functions of convertTo, applyColorMap,imshow to get the result I want?

asked 2016-11-26 00:44:19 -0600

buyi1128 gravatar image

updated 2016-11-26 00:55:07 -0600

Here is the code in Matlab and the result picture I get using Matlab. For some purposes, I need to convert Matlab code into C++. I have done the conversion of data structure in c++, and then I need to deal with the part of showing picture. I decide to use Opencv library to replace the image procesing in Matlab. I have found that some functions like convertTo, applyColorMap and imshow in Opencv can replace the function imagesc in Matlab. So, I imitate the code online I searched. But it doesn't work.Maybe there are some grammer mistakes in my questions, sorry about that. Here is my code in C++

for (cutNumber = 1; cutNumber <= 9; cutNumber++)
{
    momentString = "dBT";
    DataSelect* BaseData_Select = select(theObj,cutNumber,momentString);
    int ncols = BaseData_Select->allLength / BaseData_Select->dataLength;
   //对应matlab auto.m第138行。下面是绘制dBZ的B显图
    Mat mydata(BaseData_Select->dataLength, ncols, CV_32F);
    for (int i = 0; i < BaseData_Select->dataLength; i++)
     {
        for (int j = 0; j < ncols; j++)
        {
            int temmmp = i*ncols+ j;
            mydata.at<float>(i, j) = BaseData_Select->data[i*ncols + j];
        }
     }
    double Amin = *min_element(mydata.begin<float>(), mydata.end<float>());  //Amin is -19
    double Amax = *max_element(mydata.begin<float>(), mydata.end<float>());  //Amax is 64
    cv::minMaxIdx(mydata, &Amin, &Amax);
    cv::Mat adjMap;
    float scale = 255 / (Amax - Amin);
    mydata.convertTo(adjMap, CV_8UC1, scale, -Amin*scale);
    cv::Mat resultMap;
    applyColorMap(adjMap, resultMap, cv::COLORMAP_AUTUMN);
    cv::imshow("Out", resultMap);
    cv::imwrite("output.bmp", resultMap);
}

Here is the code dealing with images in Matlab.

 figure(H_figure_ZDR);
            subplot(3,3,Cut_Number);
            imagesc(ZDR.Data);
            colormap(radarcolor_CJJ(40,1));   %产生40种颜色
            caxis([ -2 6]);
            ylim([Sphere_Distance_Cell-Sphere_Distance_Cell_Extend Sphere_Distance_Cell+Sphere_Distance_Cell_Extend])
            xlim([Sphere_Center_Ray-Sphere_Azimuth_Cell_Extend Sphere_Center_Ray+Sphere_Azimuth_Cell_Extend])
            xlabel('径向数目');
            ylabel('距离库');

The result picture running on Matlab which is I want to get using Opencv functions. image description

And my result picture in C++ is the following picture which is the wrong picture absolutely. I am a new learner for opencv, but time is limited. Could any one help me to solve this problem? image description

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
0

answered 2016-11-26 12:06:27 -0600

LBerger gravatar image

May be No answer=no error. Check your data

I have just copy your program with my own data and everything is good using opencv 3.1

Mat data(256,256,CV_32FC1);
double xc=data.cols/2,yc=data.rows/2,pi=acos(-1.0);
double minVal,maxVal;

for (int i = 0; i<data.rows; i++)
    for (int j = 0; j < data.cols; j++)
    {
        float d= (xc - j)*(xc - j) +(yc - i)*(yc - i) ;
        data.at<float>(i,j)=sin(2*pi/32*sqrt(d));
   }
minMaxLoc(data,&minVal,&maxVal);
Mat data8uc;
data.convertTo(data8uc,CV_8UC1,255/(maxVal-minVal),-255*minVal / (maxVal - minVal));

cv::Mat resultMap;
applyColorMap(data8uc, resultMap, cv::COLORMAP_AUTUMN);
cv::imshow("Out", resultMap);
waitKey();
applyColorMap(data8uc, resultMap, cv::COLORMAP_JET);
cv::imshow("Out", resultMap);
waitKey();

results are :

image description

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-11-26 00:44:19 -0600

Seen: 1,632 times

Last updated: Nov 26 '16