import os import cv2 from scipy.fftpack import fftshift, fft2 import numpy as np def variance_of_laplacian(image): # 计算输入图像的拉普拉斯响应的方差 return cv2.Laplacian(image, cv2.CV_64F).var() def frequency_domain_analysis(gray): # 计算傅里叶变换并移动到中心 f_transform = fftshift(fft2(gray)) # 计算频谱幅度 magnitude_spectrum = 20 * np.log(np.abs(f_transform)) # 计算高频成分的比例 high_frequency_magnitude = np.sum(magnitude_spectrum > np.mean(magnitude_spectrum)) total_magnitude = magnitude_spectrum.size ratio = high_frequency_magnitude / total_magnitude return ratio def edge_gradient(gray): sobelx = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3) sobely = cv2.Sobel(gray, cv2.CV_64F, 0, 1, ksize=3) magnitude = np.sqrt(sobelx**2 + sobely**2) return np.mean(magnitude) def testOne(): # 加载图像并转换为灰度图 image = cv2.imread('H:/images/1/20250114193113.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 计算图像的清晰度分数 score = variance_of_laplacian(gray) ratio = frequency_domain_analysis(gray) edge_gradient_score = edge_gradient(gray) print("Image clarity score: ", score) print("High frequency ratio: ", ratio) print("Edge gradient score: ", edge_gradient_score) # 根据经验设置阈值来判断图像是否模糊 threshold = 100 # 这个阈值可以根据实际情况调整 if score < threshold: print("The image is considered blurry.") else: print("The image is considered clear.") def batch_score(): global name, join # 读取文件夹下的所有图像分别计算分数 folder_path = 'H:/images/2' for filename in os.listdir(folder_path): if filename.endswith('.jpg') or filename.endswith('.png'): image_path = os.path.join(folder_path, filename) image = cv2.imread(image_path) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) score = variance_of_laplacian(gray) ratio = frequency_domain_analysis(gray) edge_gradient_score = edge_gradient(gray) # 所有结果保留两位小数 score = round(score, 2) ratio = round(ratio, 2) edge_gradient_score = round(edge_gradient_score, 2) name = "score=" + str(score) + "ratio=" + str(ratio) + "edge_gradient_score=" + str( edge_gradient_score) + ".jpg" path_ = folder_path + "/22/" # 判断文件夹是否存在,不存在则新建 if not os.path.exists(path_): os.makedirs(path_) join = path_ + name print(join) cv2.imwrite(join, image) def cam_score(): # 调用摄像头计算采集到图像的得分 camera_index = 2 print("第二个摄像头索引:" + str(camera_index)) # 打开摄像头 capture = cv2.VideoCapture(camera_index, cv2.CAP_DSHOW) # 设置分辨率 capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1921) capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1081) # 检查摄像头是否成功打开 if not capture.isOpened(): print("无法打开摄像头2") exit() # 循环读取画面 while True: ret, frame = capture.read() if not ret: print("无法读取摄像头画面") break gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) score = variance_of_laplacian(gray) # ratio = frequency_domain_analysis(gray) # edge_gradient_score = edge_gradient(gray) # 将FPS绘制在图像上 cv2.putText(frame, f"score={str(score)}" , (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA) # cv2.putText(frame, f"ratio={str(ratio)}" # , (10, 70), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA) # cv2.putText(frame, f"edge_gradient_score={str(edge_gradient_score)}" # , (10, 110), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA) # 显示画面 cv2.imshow("Camera", frame) # 检测按键,如果按下q键则退出循环 if cv2.waitKey(1) & 0xFF == ord('q'): break # 关闭摄像头 capture.release() # 关闭所有窗口 cv2.destroyAllWindows() if __name__ == '__main__': testOne() # batch_score() # cam_score()