目录
[Python 检测运动模糊 源代码](#Python 检测运动模糊 源代码)
模拟运动模糊:
https://blog.csdn.net/jacke121/article/details/109191473
Python 检测运动模糊 源代码
python
import cv2
import numpy as np
def detect_motion_blur(img, blur_threshold=100, direction_threshold=0.8):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (3,3), 0)
# Sobel 边缘
gx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
gy = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3)
magnitude = np.sqrt(gx**2 + gy**2)
angle = np.arctan2(gy, gx)
# 模糊程度
blur_score = cv2.Laplacian(gray, cv2.CV_64F).var()
# 梯度方向分布统计
hist, _ = np.histogram(angle, bins=36, range=(-np.pi, np.pi))
hist = hist / np.sum(hist)
# 如果梯度方向过于集中(说明只有一个方向清晰)
concentration = np.max(hist)
if blur_score < blur_threshold:
if concentration > direction_threshold:
return "运动模糊", blur_score, concentration
else:
return "普通模糊", blur_score, concentration
else:
return "清晰", blur_score, concentration