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

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

最新推荐文章于 2024-08-30 00:08:45 发布

骚火棍 于 2020-08-17 14:47:31 发布

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

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)12345678

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("亮度正常")

123456789101112131415161718192021222324252627282930313233

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)

12345678910111213141516171819202122

参考链接:清晰度检测: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

相关知识

图像质量判断:模糊/色偏/亮度检测综合
棉花疵点检测系统的制作方法
基于光照过强过弱条件下的图像增强研究
基于svm机器学习的植物病虫害检测方法
PCB stm32根据光强调整led灯亮度
基于SVM机器学习的植物病虫害检测方法.pdf
基于STM32的光照检测系统设计
基于人工智能的花卉识别和病虫害防治系统研发
利用OpenCV根据图片识别环境的亮度
蚕茧质量检测技术概述

网址: 基于python+opencv的图片质量检测(清晰度+亮度+色偏) https://m.huajiangbk.com/newsview1515617.html

所属分类:花卉
上一篇: 光敏电阻测光照强度
下一篇: 基于51单片机光照强度检测(C代