本文主要是介绍opencv计算图像模糊度(sobel和laplacian),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
# -*-coding=UTF-8-*-
"""
在无参考图下,检测图片质量的方法
"""
import os
import cv2import numpy as np
from skimage import filtersclass BlurDetection:def __init__(self, strDir):print("图片检测对象已经创建...")self.strDir = strDirdef _getAllImg(self, strType='jpg'):"""根据目录读取所有的图片:param strType: 图片的类型:return: 图片列表"""names = []for root, dirs, files in os.walk(self.strDir): # 此处有bug 如果调试的数据还放在这里,将会递归的遍历所有文件for file in files:# if os.path.splitext(file)[1]=='jpg':names.append(str(file))return namesdef _imageToMatrix(self, image):"""根据名称读取图片对象转化矩阵:param strName::return: 返回矩阵"""imgMat = np.matrix(image)return imgMatdef _blurDetection(self, imgName):# step 1 图像的预处理img2gray, reImg = self.preImgOps(imgName)imgMat=self._imageToMatrix(img2gray)/255.0x, y = imgMat.shapescore = 0for i in range(x - 2):for j in range(y - 2):score += (imgMat[i + 2, j] - imgMat[i, j]) ** 2# step3: 绘制图片并保存 不应该写在这里 抽象出来 这是共有的部分score=score/10newImg = self._drawImgFonts(reImg, str(score))newDir = self.strDir + "/_blurDetection_/"if not os.path.exists(newDir):os.makedirs(newDir)newPath = newDir + imgNamecv2.imwrite(newPath, newImg) # 保存图片cv2.imshow(imgName, newImg)cv2.waitKey(0)return scoredef _SMDDetection(self, imgName):# step 1 图像的预处理img2gray, reImg = self.preImgOps(imgName)f=self._imageToMatrix(img2gray)/255.0x, y = f.shapescore = 0for i in range(x - 1):for j in range(y - 1):score += np.abs(f[i+1,j]-f[i,j])+np.abs(f[i,j]-f[i+1,j])# strp3: 绘制图片并保存 不应该写在这里 抽象出来 这是共有的部分score=score/100newImg = self._drawImgFonts(reImg, str(score))newDir = self.strDir + "/_SMDDetection_/"if not os.path.exists(newDir):os.makedirs(newDir)newPath = newDir + imgNamecv2.imwrite(newPath, newImg) # 保存图片cv2.imshow(imgName, newImg)cv2.waitKey(0)return scoredef _SMD2Detection(self, imgName):"""灰度方差乘积:param imgName::return:"""# step 1 图像的预处理img2gray, reImg = self.preImgOps(imgName)f=self._imageToMatrix(img2gray)/255.0x, y = f.shapescore = 0for i in range(x - 1):for j in range(y - 1):score += np.abs(f[i+1,j]-f[i,j])*np.abs(f[i,j]-f[i,j+1])# strp3: 绘制图片并保存 不应该写在这里 抽象出来 这是共有的部分score=scorenewImg = self._drawImgFonts(reImg, str(score))newDir = self.strDir + "/_SMD2Detection_/"if not os.path.exists(newDir):os.makedirs(newDir)newPath = newDir + imgNamecv2.imwrite(newPath, newImg) # 保存图片cv2.imshow(imgName, newImg)cv2.waitKey(0)return scoredef _Variance(self, imgName):"""灰度方差乘积:param imgName::return:"""# step 1 图像的预处理img2gray, reImg = self.preImgOps(imgName)f = self._imageToMatrix(img2gray)# strp3: 绘制图片并保存 不应该写在这里 抽象出来 这是共有的部分score = np.var(f)newImg = self._drawImgFonts(reImg, str(score))newDir = self.strDir + "/_Variance_/"if not os.path.exists(newDir):os.makedirs(newDir)newPath = newDir + imgNamecv2.imwrite(newPath, newImg) # 保存图片cv2.imshow(imgName, newImg)cv2.waitKey(0)return scoredef _Vollath(self,imgName):"""灰度方差乘积:param imgName::return:"""# step 1 图像的预处理img2gray, reImg = self.preImgOps(imgName)f = self._imageToMatrix(img2gray)source=0x,y=f.shapefor i in range(x-1):for j in range(y):source+=f[i,j]*f[i+1,j]source=source-x*y*np.mean(f)# strp3: 绘制图片并保存 不应该写在这里 抽象出来 这是共有的部分newImg = self._drawImgFonts(reImg, str(source))newDir = self.strDir + "/_Vollath_/"if not os.path.exists(newDir):os.makedirs(newDir)newPath = newDir + imgNamecv2.imwrite(newPath, newImg) # 保存图片cv2.imshow(imgName, newImg)cv2.waitKey(0)return sourcedef _Tenengrad(self,imgName):"""灰度方差乘积:param imgName::return:"""# step 1 图像的预处理img2gray, reImg = self.preImgOps(imgName)f = self._imageToMatrix(img2gray)tmp = filters.sobel(f)source=np.sum(tmp**2)source=np.sqrt(source)# strp3: 绘制图片并保存 不应该写在这里 抽象出来 这是共有的部分newImg = self._drawImgFonts(reImg, str(source))newDir = self.strDir + "/_Tenengrad_/"if not os.path.exists(newDir):os.makedirs(newDir)newPath = newDir + imgNamecv2.imwrite(newPath, newImg) # 保存图片cv2.imshow(imgName, newImg)cv2.waitKey(0)return sourcedef Test_Tenengrad(self):imgList = self._getAllImg(self.strDir)for i in range(len(imgList)):score = self._Tenengrad(imgList[i])print(str(imgList[i]) + " is " + str(score))def Test_Vollath(self):imgList = self._getAllImg(self.strDir)for i in range(len(imgList)):score = self._Variance(imgList[i])print(str(imgList[i]) + " is " + str(score))def TestVariance(self):imgList = self._getAllImg(self.strDir)for i in range(len(imgList)):score = self._Variance(imgList[i])print(str(imgList[i]) + " is " + str(score))def TestSMD2(self):imgList = self._getAllImg(self.strDir)for i in range(len(imgList)):score = self._SMD2Detection(imgList[i])print(str(imgList[i]) + " is " + str(score))returndef TestSMD(self):imgList = self._getAllImg(self.strDir)for i in range(len(imgList)):score = self._SMDDetection(imgList[i])print(str(imgList[i]) + " is " + str(score))returndef TestBrener(self):imgList = self._getAllImg(self.strDir)for i in range(len(imgList)):score = self._blurDetection(imgList[i])print(str(imgList[i]) + " is " + str(score))returndef preImgOps(self, imgName):"""图像的预处理操作:param imgName: 图像的而明朝:return: 灰度化和resize之后的图片对象"""strPath = self.strDir + imgNameimg = cv2.imread(strPath) # 读取图片cv2.moveWindow("", 1000, 100)# cv2.imshow("原始图", img)# 预处理操作reImg = cv2.resize(img, (800, 900), interpolation=cv2.INTER_CUBIC) #img2gray = cv2.cvtColor(reImg, cv2.COLOR_BGR2GRAY) # 将图片压缩为单通道的灰度图return img2gray, reImgdef _drawImgFonts(self, img, strContent):"""绘制图像:param img: cv下的图片对象:param strContent: 书写的图片内容:return:"""font = cv2.FONT_HERSHEY_SIMPLEXfontSize = 5# 照片 添加的文字 /左上角坐标 字体 字体大小 颜色 字体粗细cv2.putText(img, strContent, (0, 200), font, fontSize, (0, 255, 0), 6)return imgdef _lapulaseDetection(self, imgName):""":param strdir: 文件所在的目录:param name: 文件名称:return: 检测模糊后的分数"""# step1: 预处理img2gray, reImg = self.preImgOps(imgName)# step2: laplacian算子 获取评分resLap = cv2.Laplacian(img2gray, cv2.CV_64F)score = resLap.var()print("Laplacian %s score of given image is %s", str(score))# strp3: 绘制图片并保存 不应该写在这里 抽象出来 这是共有的部分newImg = self._drawImgFonts(reImg, str(score))newDir = self.strDir + "/_lapulaseDetection_/"if not os.path.exists(newDir):os.makedirs(newDir)newPath = newDir + imgName# 显示cv2.imwrite(newPath, newImg) # 保存图片cv2.imshow(imgName, newImg)cv2.waitKey(0)# step3: 返回分数return scoredef TestDect(self):names = self._getAllImg()for i in range(len(names)):score = self._lapulaseDetection(names[i])print(str(names[i]) + " is " + str(score))returnif __name__ == "__main__":BlurDetection = BlurDetection(strDir="D:/document/ZKBH/bug/face/")BlurDetection.Test_Tenengrad () # TestSMD
来源 https://github.com/Leezhen2014/python--/blob/master/BlurDetection.py
图像的模糊检测方法 - 修雨轩陈 - 博客园
在widerface数据集上测试, Test_Tenengrad的阈值选择为7能够得到一个较好的模糊度分类效果
不明白为什么要把图像resize到(800, 900)的尺度上再求梯度:
reImg = cv2.resize(img, (800, 900), interpolation=cv2.INTER_CUBIC) #
关于各种算子介绍的文章:
Roberts算子、Sobel算子和Laplacian算子的数学推导 | 致永远-For Aye
从实验结果上比对来看,sobel比laplacian更稳定一些,所以在工程上决定用sobel。
********************
个人公众号“AI技术评论”,欢迎关注
这篇关于opencv计算图像模糊度(sobel和laplacian)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!