I have been using minMaxLoc to compute the maximum value of the data obtained by running a laplacian filter over a grayscale image. My simple intention is to roughly estimate the sharpness. I encountered a confusing situation, which I have discussed here.

The max value obtained from the minMaxLoc function was like 1360, 1456,450 etc for a set of images.

    Laplacian(src_gray,dst,ddepth,kernel_size,scale,delta,BORDER_DEFAULT);
    minMaxLoc(dst,&min,&estimate,&minLoc,&maxLoc,noArray());//Estimate is the max value

Now I just tried to compute the average to have a better idea of the spread of the sharpness in the image. Note that DST is the Mat variable holding data from the Laplacian.

    Size s = dst.size();
    rows = s.height;
    cols = s.width;
    total = 0;
    max =  0;
    for(int k=0;k<rows;k++)
    {
      for(int l=0;l<cols;l++)
      {
        total = total + abs(dst.at<int>(k,l));
      }
    }
    average = total/(rows*cols);

There are 2 baffling results I obtained. The average value I obtained, was not only greater than the max value obtained from minMaxLoc, but also was at times negative, when tried over a set of images. sample Average values where 22567, at times -25678.

The occurrence of negative was even more baffling as am using the abs() to get the absolute value of the laplacian results.

To get a proper understanding, I calculated the max value by myself and then the average values :

    Size s = dst.size();
    rows = s.height;
    cols = s.width;
    total = 0;
    max =  0;
    for(int k=0;k<rows;k++)
    {
      for(int l=0;l<cols;l++)
      {
        if(abs(dst.at<int>(k,l))>max)
        {
           max = abs(dst.at<int>(k,l));
        }
        total = total + abs(dst.at<int>(k,l));
       }
    }
    average = total/(rows*cols);

surprisingly, I found the max value to be in 8 digits.

This is why I got confused. What is the max value given from the minMaxLoc function? And why is the abs() in total, not working and why am I getting -ve average values.?

Please forgive me if am missing something in the code, but this is slightly confusing me. Thanks for your help in advance.

有帮助吗?

解决方案

I think you should use .at< uchar > instead of int (considering image to be grayscale) otherwise the value will overflow!

其他提示

Typically images have 8 bit images. So chances are, that you are accessing the pixels of your image using the wrong method. And in this case, the values you read from the matrix are wrong.

To check if you are working with a single channel integer matrix use

dst.type() == CV_32SC1 .

To check for a 8 bit matrix use

dst.type() == CV_8SC1 .

If you are actually having such an 8 bit integer matrix, you need to use .at<uchar> to access the pixels.

The reason your total variable is negative even though you only added positive values to it is probably due to an integer overflow. You can avoid this by using a long int for total.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top