Is there a way to detect if an image is blurry?

后端 未结 12 1774
予麋鹿
予麋鹿 2020-11-22 14:48

I was wondering if there is a way to determine if an image is blurry or not by analyzing the image data.

12条回答
  •  北海茫月
    2020-11-22 15:24

    Building off of Nike's answer. Its straightforward to implement the laplacian based method with opencv:

    short GetSharpness(char* data, unsigned int width, unsigned int height)
    {
        // assumes that your image is already in planner yuv or 8 bit greyscale
        IplImage* in = cvCreateImage(cvSize(width,height),IPL_DEPTH_8U,1);
        IplImage* out = cvCreateImage(cvSize(width,height),IPL_DEPTH_16S,1);
        memcpy(in->imageData,data,width*height);
    
        // aperture size of 1 corresponds to the correct matrix
        cvLaplace(in, out, 1);
    
        short maxLap = -32767;
        short* imgData = (short*)out->imageData;
        for(int i =0;i<(out->imageSize/2);i++)
        {
            if(imgData[i] > maxLap) maxLap = imgData[i];
        }
    
        cvReleaseImage(&in);
        cvReleaseImage(&out);
        return maxLap;
    }
    

    Will return a short indicating the maximum sharpness detected, which based on my tests on real world samples, is a pretty good indicator of if a camera is in focus or not. Not surprisingly, normal values are scene dependent but much less so than the FFT method which has to high of a false positive rate to be useful in my application.

提交回复
热议问题