如何使用opencv在c ++中规范化差异映射?

时间:2015-01-20 08:23:19

标签: c++ opencv image-processing

我想以这样一种方式规范差异地图 - 1.将地图中的值Nornalize到固定范围[0..M] 2.找到M(差异图像中的全局最大值)和m(所有其他局部最大值的平均值) 3.通过(M-m)

的平方来全局地绘制地图

但我没有得到正确的输出...... 缩放因子太高..大约23000 ..不应该在0-255之间?

int main()
{

    Mat img = imread("lena.jpg",CV_LOAD_IMAGE_GRAYSCALE);   
    Mat img1,diff;   

    if (img.empty()) //if unsuccessful, exit the program
    {
            cout << "Image cannot be loaded..!!" << endl;
            return -1;
    }

    int m,n;
    m=(int)(img.rows/2);
    n=(int)(img.cols/2);
    resize(img, img1,Size(m,n));        //downsample by 2

    resize(img1, img1,img.size(), 0, 0, CV_INTER_NN);  //interpolation by 2

    absdiff(img,img1,diff);


    //------to find global maxima present in the image-------------
    double min2,max2;
    Point x,y;
    minMaxLoc(diff,&min2,&max2,&x,&y);
    cout<<"global max= "<<max2<<" location= "<<y<<endl;

    //--to find local maximas present in the image using 5*5 kernel----
    double min1,max1;
    double val[2025];
    int count=0;
    Mat arr=Mat::zeros(5,5,img.type());
    int i,j,a,b,a1;
    for(a=0;a<diff.rows;a=a+5)
    {
        for(b=0;b<diff.cols;b=b+5)
        {
            m=n=0;
            for(i=a;i<a+5;i++)
            {
               for(j=b;j<b+5;j++)
               {                
                  a1=diff.at<uchar>(i,j);
                  arr.at<uchar>(m,n)=a1;                
                  n++;          
               }
               m++;n=0;
            }   
            minMaxLoc(arr,&min1,&max1);
            val[count]=max1;
            count++;
        }   
    }

    double sum=0,mean;  
    for(i=0;i<count;i++)
    {
        sum=sum+val[i];
    }

    mean=sum/count;
    cout<<"count="<<count<<endl;
    cout<<"sum= "<<sum<<endl;
    cout<<"mean= "<<mean<<endl;     

    double scale;
    scale=pow((max2-mean),2);
    cout<<"scale= "<<scale;

    imshow("original",diff);
    imshow("normalized output",scale*diff); 
    waitKey(0);
    return 0;
}

1 个答案:

答案 0 :(得分:0)

您的比例变量在您的代码中是平方的:

scale=pow((max2-mean),2);表示(max2-mean)^2

(M-m)^2

您要求使用scale = (M-m),如果我看到正确的话,代码中会scale = max2-mean;