參考:
https://www.pyimagesearch.com/2015/09/07/blur-detection-with-opencv/
https://www.cnblogs.com/arkenstone/p/7900978.html
先對圖像用拉普拉斯算子進行濾波,然后求取得到的結果圖像的方差,如果方差小於一定值則圖片視為模糊。利用python很好實現:
img2gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # 將圖片壓縮為單通道的灰度圖 score = cv2.Laplacian(img2gray, cv2.CV_64F).var()
C++實現如下:
bool isImageBlurry(cv::Mat& img) { cv::Mat matImageGray; // converting image's color space (RGB) to grayscale cv::cvtColor(img, matImageGray, CV_BGR2GRAY); cv::Mat dst, abs_dst; cv::Laplacian(matImageGray, dst, CV_16S, 3); cv::convertScaleAbs( dst, abs_dst ); cv::Mat tmp_m, tmp_sd; double m = 0, sd = 0; int threshold = 1000; cv::meanStdDev(dst, tmp_m, tmp_sd); m = tmp_m.at<double>(0,0); sd = tmp_sd.at<double>(0,0); std::cout << "StdDev: " << sd * sd << std::endl; return ((sd * sd) <= threshold); }