How to detect Blur rate of a face effectively in c++?

风格不统一 提交于 2021-02-20 05:24:23

问题


I am trying to detect blur rate of the face images with below code.

cv::Mat greyMat;
cv::Mat laplacianImage;
cv::Mat imageClone = LapMat.clone();

cv::resize(imageClone, imageClone, cv::Size(150, 150), 0, 0, cv::INTER_CUBIC);

cv::cvtColor(imageClone, greyMat, CV_BGR2GRAY);
Laplacian(greyMat, laplacianImage, CV_64F);

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

cv::Mat M = (cv::Mat_(3, 1) << -1, 2, -1); cv::Mat G = cv::getGaussianKernel(3, -1, CV_64F);

cv::Mat Lx;
cv::sepFilter2D(LapMat, Lx, CV_64F, M, G);

cv::Mat Ly;
cv::sepFilter2D(LapMat, Ly, CV_64F, G, M);

cv::Mat FM = cv::abs(Lx) + cv::abs(Ly);

double focusMeasure = cv::mean(FM).val[0];
return focusMeasure;

it some times gives not good results as attached picture.

Is there a best practice way to detect blurry faces ? I attached an example image which is high scored with above code which is false.

Best


回答1:


I'm not sure how are you interpreting your results. To measure blur, you usually take the output of the Blur Detector (a number) and compare it against a threshold value, then determine if the input is, in fact, blurry or not. I don't see such a comparison in your code.

There are several ways to measure "blurriness", or rather, sharpness. Let's take a look at one. It involves computing the variance of the Laplacian and then comparing it to an expected value. This is the code:

//read the image and convert it to grayscale:
cv::Mat inputImage = cv::imread( "dog.png" );
cv::Mat gray;
cv::cvtColor( inputImage, gray, cv::COLOR_RGB2GRAY );

//Cool, let's compute the laplacian of the gray image:
cv::Mat laplacianImage;
cv::Laplacian( gray, laplacianImage, CV_64F );

//Prepare to compute the mean and standard deviation of the laplacian:
cv::Scalar mean, stddev; 
cv::meanStdDev( laplacianImage, mean, stddev, cv::Mat() );

//Let’s compute the variance:
double variance = stddev.val[0] * stddev.val[0];

Up until this point, we've effectively calculated the variance of the Laplacian, but we still need to compare against a threshold:

double blurThreshold = 300;

if ( variance <= blurThreshold ) {
    std::cout<<"Input image is blurry!"<<std::endl;
} else {
    std::cout<<"Input image is sharp"<<std::endl;
}

Let’s check out the results. These are my test images. I've printed the variance value in the lower-left corner of the images. The threshold value is 300, blue text is within limits, red text is below.



来源:https://stackoverflow.com/questions/60587428/how-to-detect-blur-rate-of-a-face-effectively-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!