opencv判断图片模糊/花屏

原理:先对图像用拉普拉斯算子进行滤波,然后求取得到的结果图像的方差,如果方差小于一定值则图片视为模糊。

1.python 版

import os
import cv2
img_path = r".\images"
good_img_path = r".\good"
bad_img_path = r".\bad"
Threshold = 50  
for filename in os.listdir(img_path):
    filepath = os.path.join(img_path, filename)
    frame = cv2.imread(filepath)
    img2gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  # 将图片压缩为单通道的灰度图
    score = cv2.Laplacian(img2gray, cv2.CV_64F).var()
    cv2.putText(frame, str(score), (123, 456),  cv2.FONT_HERSHEY_SIMPLEX, 2, (0, 255, 0), 3)
    if score < Threshold:
        path = os.path.join(bad_img_path, filename)
        if not os.path.exists(bad_img_path):
            os.makedirs(bad_img_path)
        cv2.imwrite(path, frame)
    else:
        path = os.path.join(good_img_path, filename)
        if not os.path.exists(good_img_path):
            os.makedirs(good_img_path)
        cv2.imwrite(path, frame)

2.c++版

#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>

using namespace std;
using namespace cv;

int blurryConf = 50;  //default
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);
        cv::convertScaleAbs( dst, abs_dst );
    cv::Mat tmp_m, tmp_sd;
    double m , sd;
    cv::meanStdDev(dst, tmp_m, tmp_sd);
    m = tmp_m.at<double>();
    sd = tmp_sd.at<double>();
    std::cout << "img score : " << sd * sd << std::endl;
    return ((sd * sd) <= blurryConf);  //true : 1 ;false : 0
}

int main()
{
	Mat img = imread("./test.jpg");
	if(isImageBlurry(img))
	{
		cout << " img is blurry !! " << endl; 
	}else
	{
		cout << " img is not blurry !! " << endl;
	} 

	return 0;
}
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MonicaUp

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值