基于python+opencv的图片质量检测(清晰度+亮度+色偏)

1.清晰度检测:

  • 利用拉普拉斯算子计算图片的二阶导数,反映图片的边缘信息,同样事物的图片,清晰度高的,相对应的经过拉普拉斯算子滤波后的图片的方差也就越大。
import cv2
#利用拉普拉斯
def getImageVar(imgPath):
    image = cv2.imread(imgPath)
    img2gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    imageVar = cv2.Laplacian(img2gray, cv2.CV_64F).var()
    return imageVar
imageVar = getImageVar("tooth/15.jpg")
print(imageVar)

2.亮度检测:

  • 计算图片在灰度图上的均值和方差,当存在亮度异常时,均值会偏离均值点(可以假设为128),方差也会偏小;通过计算灰度图的均值和方差,就可评估图像是否存在过曝光或曝光不足。
import cv2
import numpy as np
img = cv2.imread('1.jpg')
# 把图片转换为单通道的灰度图
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# 获取形状以及长宽
img_shape = gray_img.shape
height, width = img_shape[0], img_shape[1]
size = gray_img.size
# 灰度图的直方图
hist = cv2.calcHist([gray_img], [0], None, [256], [0, 256])
# 计算灰度图像素点偏离均值(128)程序
a = 0
ma = 0
#np.full 构造一个数组,用指定值填充其元素
reduce_matrix = np.full((height, width), 128)
shift_value = gray_img - reduce_matrix
shift_sum = np.sum(shift_value)
da = shift_sum / size
# 计算偏离128的平均偏差
for i in range(256):
    ma += (abs(i-128-da) * hist[i])
m = abs(ma / size)
# 亮度系数
k = abs(da) / m
print(k)
if k[0] > 1:
    # 过亮
    if da > 0:
        print("过亮")
    else:
        print("过暗")
else:
    print("亮度正常")

3.色偏检测:

  • 将RGB图像转变到CIE Lab空间,其中L表示图像亮度,a表示图像红/绿分量,b表示图像黄/蓝分量。通常存在色偏的图像,在a和b分量上的均值会偏离原点很远,方差也会偏小;通过计算图像在a和b分量上的均值和方差,就可评估图像是否存在色偏。
import cv2
img = cv2.imread('tooth/10.jpg')
img = cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l_channel, a_channel, b_channel = cv2.split(img)
h,w,_ = img.shape
da = a_channel.sum()/(h*w)-128
db = b_channel.sum()/(h*w)-128
histA = [0]*256
histB = [0]*256
for i in range(h):
    for j in range(w):
        ta = a_channel[i][j]
        tb = b_channel[i][j]
        histA[ta] += 1
        histB[tb] += 1
msqA = 0
msqB = 0
for y in range(256):
    msqA += float(abs(y-128-da))*histA[y]/(w*h)
    msqB += float(abs(y - 128 - db)) * histB[y] / (w * h)
import math
result = math.sqrt(da*da+db*db)/math.sqrt(msqA*msqA+msqB*msqB)
print("d/m = %s"%result)

参考链接:清晰度检测:https://blog.csdn.net/qq_42238397/article/details/81745600
亮度检测:https://blog.csdn.net/qq_40790959/article/details/106107574
色偏检测:
https://blog.csdn.net/sparrowwf/article/details/86595162
https://blog.csdn.net/fightingforcv/article/details/52724848

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值