PVC手套是一款以聚氯乙烯为主原料的手套产品,具有防静电的性能。在许多行业内都会用到,例如电子制造业、药品制造业、化工业、农业等等,运用非常广泛。在PVC手套的生产过程中,会出现有一些质量问题,如粘上油污、蚊虫等情况,这个时候需要将不良品检测出来,以确保供给客户的产品是100%合格。为了检测手套表面缺陷,本文使用OpenCV传统图像处理方法检测出手套表面缺陷。
提示:以下是本篇文章正文内容,下面案例可供参考
pip install virtualenv
pip install opencv-python
pip install numpy
代码如下(示例):
import os
import cv2
import numpy as np
代码如下(示例):
IMAGE_SIZE = (500, 500) #待测图片尺寸,也可以通过路径读取,通过shape获得,方法众多
#如果没有检测到轮廓,THRESHOLD_VALUE需要增加
#如果有不准确的轮廓值需要减少
THRESHOLD_VALUE = 110
MAX_VALUE = 255
#反向阈值
INV_THRESHOLD_VALUE = 50
INV_MAX_VALUE = 255
#Canny
THRESHOLD1 = 100
THRESHOLD2 = 70
#轮廓属性
CON_COLOR = (0, 0, 255)
CON_THICKNESS = 1
#图像叠加特性
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
GREEN = (0, 255, 0)
RED = (0, 0, 255)
STACK_IMG_SIZE = (200, 200)
while True:
files = os.listdir('images')
print("======================================")
print("= Available Images =")
print("======================================")
for i in files:
print('-> {}\t '.format(i), end='')
if files.index(i) % 3 == 0 and files.index(i) != 0:
print('\n')
print("\n======================================")
# 需要选择具有扩展名的图像名称 (ex: img1.jpeg)
file = input("Select a file from the directory(q- quit): ").strip()
if file == 'q' or file == 'Q':
break
#检测图像存放位置
PATH = 'images/' + file
# 图像路径
imageOri = cv2.imread(PATH)
try:
# 转换为灰度图
image = cv2.cvtColor(imageOri, cv2.COLOR_BGR2GRAY)
except:
print("Invalid Input! Please select correct file(ex: 'imgSample.jpg')")
else:
#图像resize
image = cv2.resize(image, IMAGE_SIZE)
imageOri = cv2.resize(imageOri, IMAGE_SIZE)
image = cv2.GaussianBlur(image, (3, 3), 0)
# 阈值图像,以便您的黑色标记是在白色背景上。
ret, thresh_basic = cv2.threshold(image, THRESHOLD_VALUE, MAX_VALUE, cv2.THRESH_BINARY)
# 显示阈值图像 - DEBUGGING
cv2.imshow("Thresh basic", thresh_basic)
#thresh_addapt = cv2.adaptiveThreshold(image, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 115, 1)
# cv2.imshow("Thresh Adapt", thresh_addapt)
# 取一个大小为5的矩阵作为核
kernel = np.ones((5, 5), np.uint8)
#形态学操作——侵蚀前景物体的边界
#使用形态学来清除多余的标记
img_erosion = cv2.erode(thresh_basic, kernel, iterations=1)
# 黑色的标记是黑色背景上的白色然后找到它们的外部轮廓.
ret, thresh_inv = cv2.threshold(img_erosion, INV_THRESHOLD_VALUE, INV_MAX_VALUE, cv2.THRESH_BINARY_INV)
#cv2.imshow("INV", thresh_inv)
# Canny算法找到边缘
edged = cv2.Canny(img_erosion, THRESHOLD1, THRESHOLD2)
# show canny edges - DEBUGGING
#cv2.imshow('Canny', edged)
#cv2.waitKey(0)
# 找到轮廓
# findContours
contours, hierarchy = cv2.findContours(thresh_inv, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# ++++++++++++++++++
# -- Image Stack --
# ++++++++++++++++++
font = cv2.FONT_HERSHEY_SIMPLEX
imageRz = cv2.resize(image, STACK_IMG_SIZE)
thresh_basicRz = cv2.resize(thresh_basic, STACK_IMG_SIZE)
img_erosionRz = cv2.resize(img_erosion, STACK_IMG_SIZE)
thresh_invRz = cv2.resize(thresh_inv, STACK_IMG_SIZE)
edgedRz = cv2.resize(edged, STACK_IMG_SIZE)
imageRz = cv2.putText(imageRz, 'GrayScale', (5, 15), font, 0.5, WHITE, 1, cv2.LINE_AA)
thresh_basicRz = cv2.putText(thresh_basicRz, 'ThresholdBasic', (5, 15), font,
0.5, WHITE, 1,cv2.LINE_AA)
img_erosionRz = cv2.putText(img_erosionRz, 'Morphology-Erosion', (5, 15), font,
0.5, WHITE, 1, cv2.LINE_AA)
thresh_invRz = cv2.putText(thresh_invRz, 'Threshold-mode INV', (5, 15), font,
0.5, BLACK, 1, cv2.LINE_AA)
edgedRz = cv2.putText(edgedRz, 'Canny Edges', (5, 15), font, 0.5, WHITE, 1, cv2.LINE_AA)
numpy_horizontal_concat = np.concatenate((imageRz, thresh_basicRz, img_erosionRz,
thresh_invRz, edgedRz), axis=1)
cv2.imshow('Filtering...', numpy_horizontal_concat)
# +++++++
# 得到总轮廓
num_of_con = str(len(contours) - 1)
print("Number of Contours found = " + num_of_con)
if len(contours) > 1:
print('= MARKINGS DETECTED =')
#显示原始img
cv2.imshow('Original Image', imageOri)
# draw contours on original img
if int(num_of_con) != 0:
for i in range(int(num_of_con)):
highlighted_img = cv2.drawContours(imageOri, contours, i, CON_COLOR, CON_THICKNESS)
highlighted_img = cv2.putText(highlighted_img, 'Approximately {} defect(s) detected'.
format(num_of_con), (5, 15),
font, 0.5, GREEN, 1, cv2.LINE_AA)
else:
highlighted_img = cv2.putText(imageOri, 'Unable to detect defects!',
(5, 15), font, 0.5, RED, 2, cv2.LINE_AA)
# 显示标记的图像
cv2.imshow('Highlighted Defect', highlighted_img)
# 保存包含突出显示缺陷的图像
cv2.imwrite('Output Images/{}_DEFECTS_HIGHLIGHTED.jpg'.format(file.split('.')[0]), highlighted_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
与人工检测相比,机器视觉检测设备能够高速、高准确性、长期且稳定地进行重复性工作,大量减少出错率,也避免人眼检测弹性疲乏的缺点,提升产品质量及生产速度。