OpenCV with Laplacian formula to detect image is blur or not in iOS

Nikunj Jadav picture Nikunj Jadav · Jun 6, 2014 · Viewed 13.8k times · Source

Thanks for help in advance.

I have lots of R&D and search but I can't find any solution for detect blur image or not.

I have used this https://github.com/BloodAxe/OpenCV-Tutorial and for blur detection used Laplacian formula but can't get blur detection in image

-(void) checkForBurryImage:(UIImage *) image {

cv::Mat matImage = [image toMat];
cv::Mat matImageGrey;
cv::cvtColor(matImage, matImageGrey, CV_BGRA2GRAY);

cv::Mat dst2 =[image toMat];
cv::Mat laplacianImage;
dst2.convertTo(laplacianImage, CV_8UC1);
cv::Laplacian(matImageGrey, laplacianImage, CV_8U);
cv::Mat laplacianImage8bit;
laplacianImage.convertTo(laplacianImage8bit, CV_8UC1);
//-------------------------------------------------------------
//-------------------------------------------------------------
unsigned char *pixels = laplacianImage8bit.data;
//-------------------------------------------------------------
//-------------------------------------------------------------
 //    unsigned char *pixels = laplacianImage8bit.data;
int maxLap = -16777216;

for (int i = 0; i < ( laplacianImage8bit.elemSize()*laplacianImage8bit.total()); i++) {
    if (pixels[i] > maxLap)
        maxLap = pixels[i];
}

int soglia = -6118750;

printf("\n maxLap : %i",maxLap);


if (maxLap < soglia || maxLap == soglia) {
    printf("\n\n***** blur image *****");
}else
    printf("\nNOT a blur image"); }

And I used same code as Android and its working fine but In iOS, its give me always positive value so I think its not working,

So please give me idea or link or any suggestion.

Answer

Nikhil Kulkarni picture Nikhil Kulkarni · Jun 16, 2017

Use this :

Laplacian(gray, laplacianImage, CV_64F);
Scalar mean, stddev; // 0:1st channel, 1:2nd channel and 2:3rd channel
meanStdDev(laplacianImage, mean, stddev, Mat());
double variance = stddev.val[0] * stddev.val[0];

double threshold = 2900;

if (variance <= threshold) {
    // Blurry
} else {
    // Not blurry
}