Ask Your Question
4

How to calculate blurriness and sharpness of a given image?

asked 2012-12-21 06:13:36 -0600

UserOpenCV gravatar image

updated 2020-12-05 12:00:23 -0600

Is there any function there in OpenCV to do it?

If there is no function in OpenCV how can I implement it? any idea would be great..

The input will be an image and the output should be the blurriness and sharpness of the image.

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted
10

answered 2012-12-22 12:58:13 -0600

updated 2015-10-19 14:14:20 -0600

In general, sum of magnitude of image shows focus fluctuations (smoothness).In special case, you can check the zero crossing in specific points of image. For more accurate value for smoothness compute FFT for image then you can survey the high pass frequency of image.

C:

double calcGradients(const IplImage*src,intaperture_size=7)
{
CvSize sz=cvGetSize(src);
IplImage*img16_x=cvCreateImage(sz,IPL_DEPTH_16S,1);
IplImage*img16_y=cvCreateImage(sz,IPL_DEPTH_16S,1);
cvSobel(src,img16_x,1,0,aperture_size);
cvSobel(src,img16_y,0,1,aperture_size);
IplImage*imgF_x=cvCreateImage(sz,IPL_DEPTH_32F,1);
IplImage*imgF_y=cvCreateImage(sz,IPL_DEPTH_32F,1);
cvScale(img16_x,imgF_x);
cvScale(img16_y,imgF_y);
IplImage*magnitude=cvCreateImage(sz,IPL_DEPTH_32F,1);
cvCartToPolar(imgF_x,imgF_y,magnitude);
double res=cvSum(magnitude).val[0];
cvReleaseImage(&magnitude);
cvReleaseImage(&imgF_x);
cvReleaseImage(&imgF_y);
cvReleaseImage(&img16_x);
cvReleaseImage(&img16_y);
return
res;
}

C++:

double contrast_measure( const Mat&img )
{
    Mat dx, dy;
    Sobel( img, dx, CV_32F, 1, 0, 3 );
    Sobel( img, dy, CV_32F, 0, 1, 3 );
    magnitude( dx, dy, dx );
    return sum(dx)[0];
}
edit flag offensive delete link more
2

answered 2015-10-19 14:13:32 -0600

updated 2015-12-15 12:21:22 -0600

test code for the function calcBlurriness which is an undocumented OpenCV function

#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "iostream"

using namespace cv;

float calcBlurriness( const Mat &src )
{
    Mat Gx, Gy;
    Sobel( src, Gx, CV_32F, 1, 0 );
    Sobel( src, Gy, CV_32F, 0, 1 );
    double normGx = norm( Gx );
    double normGy = norm( Gy );
    double sumSq = normGx * normGx + normGy * normGy;
    return static_cast<float>( 1. / ( sumSq / src.size().area() + 1e-6 ));
}

int main( int argc, char** argv )
{
    char* filename = argc >= 2 ? argv[1] : (char*)"lena.jpg";
    Mat src = imread( filename );

    if(src.data)
    {
        std::cout << "original image : " << calcBlurriness( src ) << std::endl;

        for( int i =3; i < 80; i+=2 )
        {
            Mat blurred;
            GaussianBlur( src, blurred, Size(i,i), 0);
            imshow(  "blurred image", blurred );
            waitKey(200);
            std::cout << "blurred image  : " << calcBlurriness( blurred ) << std::endl;
        }
    }
    return 0;
}

resuts : ( the lesser value means more sharpness )

original image : 4.97583e-005
blurred image  : 7.70266e-005
blurred image  : 9.97529e-005
blurred image  : 0.000134977
blurred image  : 0.000161698
blurred image  : 0.00020089
blurred image  : 0.000226519
blurred image  : 0.000275304
blurred image  : 0.00030933
blurred image  : 0.000356515
blurred image  : 0.000399192
blurred image  : 0.000450388
blurred image  : 0.000483238
blurred image  : 0.00054965
blurred image  : 0.000603829
blurred image  : 0.000645158
blurred image  : 0.00071919
blurred image  : 0.000767566
blurred image  : 0.000847372
blurred image  : 0.000895128
blurred image  : 0.000917833
blurred image  : 0.00102065
blurred image  : 0.00113219
.
.
edit flag offensive delete link more

Comments

I think he might be talking about a map of the sharpness of an image per pixel.

Royi gravatar imageRoyi ( 2015-10-20 10:43:05 -0600 )edit

Question Tools

2 followers

Stats

Asked: 2012-12-21 06:13:36 -0600

Seen: 30,903 times

Last updated: Dec 15 '15