您现在的位置是:首页 >技术杂谈 >十、切分织物起球和非起球区域以便于计算毛球对比度网站首页技术杂谈
十、切分织物起球和非起球区域以便于计算毛球对比度
一、通过训练的模型可以将织物的起球区域进行识别区分
原图:
模型识别:
二、对比度的计算
为了对织物起球等级进行评定,需要这边不同的参数特征来构建模型的评级系统
通过查阅相关文献,确定最终的特征参数为:织物起球个数、织物起球最大面积、织物起球平均面积、对比度、光学体积、密度分布
织物起球个数、织物起球最大面积、织物起球平均面积这些通过轮廓即可实现统计,详情可参考博文:十七、轮廓发现
文献中给出的定义如下:
①毛球区域平均灰度值
织物原图为:E:/Jupyter_notebook/pytorch/opencv/2_origin.jpg
模型预测的图片为:E:/Jupyter_notebook/pytorch/opencv/2_predict.jpg
此时只需要对模型预测的图片2_predict.jpg
转灰度图
统计灰度图中灰度值总和、非0像素值的像素点个数,然后相除即可得到毛球区域平均灰度值
img = cv2.imread(r"E:Jupyter_notebookpytorchopencv2_predict.jpg")
,读取图片
grayImage = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
,转灰度图
x = cv2.countNonZero(grayImage)
,非0值的像素点个数
pill_gray = np.sum(grayImage)
,像素点之和,因为黑色像素点值为0,没有影响
aver_pill_gray = pill_gray/x
,得到毛球区域平均灰度值
最终得到毛球区域平均灰度值aver_pill_gray
import cv2
import numpy as np
from matplotlib import pyplot as plt
def show_photo(name,picture):#图像显示函数
cv2.imshow(name,picture)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread(r"E:Jupyter_notebookpytorchopencv2_predict.jpg")
grayImage = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
show_photo("origin",img)
show_photo("gray",grayImage)
x = cv2.countNonZero(grayImage) #非0值的像素点个数
pill_gray = np.sum(grayImage) #像素点之和
aver_pill_gray = pill_gray/x
print(pill_gray)
print(x)
print(aver_pill_gray)
"""
160314
8964
17.884203480589022
"""
起球区域:
起球区域灰度图:
②非毛球区域平均灰度值
原图和模型预测的图片做差即可得到非毛球区域,然后转换为灰度图,求非0像素点总个数、求总像素点之和,就可以计算出非毛球区域平均灰度值
img_origin = cv2.imread(r"E:/Jupyter_notebook/pytorch/opencv/2_origin.jpg")
,读取原图
img_predict = cv2.imread(r"E:/Jupyter_notebook/pytorch/opencv/2_predict.jpg")
,读取模型预测之后的只含有起球区域的图像
img = cv2.subtract(img_origin, img_predict)
,原图和起球区域做差,得到非起球区域图像
grayImage = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
,非起球区域图像灰度图
x = cv2.countNonZero(grayImage)
,计算非起球区域图像灰度图中非0值的像素点个数
nonpill_gray = np.sum(grayImage)
,计算非起球区域图像灰度图像素点之和,因为黑色像素点值为0,没有影响
aver_nonpill_gray = nonpill_gray/x
,计算非起球区域图像灰度图的平均值灰度值
最终得到非毛球区域平均灰度值aver_nonpill_gray
import cv2
import numpy as np
from matplotlib import pyplot as plt
def show_photo(name,picture):#图像显示函数
cv2.imshow(name,picture)
cv2.waitKey(0)
cv2.destroyAllWindows()
img_origin = cv2.imread(r"E:/Jupyter_notebook/pytorch/opencv/2_origin.jpg")
img_predict = cv2.imread(r"E:/Jupyter_notebook/pytorch/opencv/2_predict.jpg")
img = cv2.subtract(img_origin, img_predict)
grayImage = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
x = cv2.countNonZero(grayImage) #非0值的像素点个数
nonpill_gray = np.sum(grayImage)#像素点之和
aver_nonpill_gray = nonpill_gray/x
show_photo("origin",img_origin)
show_photo("pilling",img_predict)
show_photo("nonpilling",img)
show_photo("gray",grayImage)
print(nonpill_gray)
print(x)
print(aver_nonpill_gray)
"""
6103678
50237
121.49766108645022
"""
原图:
起球区域:
非起球区域:
非起球区域灰度图:
③对比度
根据论文中给出的公式,对比度就是aver_pill_gray
和aver_nonpill_gray
做差
C = abs(aver_pill_gray - aver_nonpill_gray)
三、完整代码
import cv2
import numpy as np
from matplotlib import pyplot as plt
def show_photo(name,picture):#图像显示函数
cv2.imshow(name,picture)
cv2.waitKey(0)
cv2.destroyAllWindows()
img_origin = cv2.imread(r"E:/Jupyter_notebook/pytorch/opencv/2_origin.jpg")
img_predict = cv2.imread(r"E:/Jupyter_notebook/pytorch/opencv/2_predict.jpg")
img_nonpill = cv2.subtract(img_origin, img_predict)
grayImage_pill = cv2.cvtColor(img_predict,cv2.COLOR_BGR2GRAY)
grayImage_nonpill = cv2.cvtColor(img_nonpill,cv2.COLOR_BGR2GRAY)
pill_number = cv2.countNonZero(grayImage_pill) #非0值的像素点个数
nonpill_number = cv2.countNonZero(grayImage_nonpill) #非0值的像素点个数
pill_gray = np.sum(grayImage_pill) #像素点之和
nonpill_gray = np.sum(grayImage_nonpill) #像素点之和
aver_pill_gray = pill_gray/pill_number
aver_nonpill_gray = nonpill_gray/nonpill_number
C = abs(aver_pill_gray - aver_nonpill_gray)
print(aver_pill_gray)
print(aver_nonpill_gray)
print(C)
"""
17.884203480589022
121.49766108645022
103.6134576058612
"""