python-opencv 垫片缺陷检测

import cv2
import numpy as np
# 8 后座表面有色斑
im = cv2.imread("img/8.bmp")
im_o = cv2.resize(im, (800, 600))

im_gauss = cv2.cvtColor(im_o, cv2.COLOR_RGB2GRAY)
im_gauss = cv2.GaussianBlur(im_gauss, (7, 7), 0)
ret, im = cv2.threshold(im_gauss, 30, 255, 0)
cv2.imshow("o", im)
# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()

# Change thresholds
params.minThreshold = 10
params.maxThreshold = 200

# Filter by Area.
params.filterByArea = True
# 过滤掉小于像素16的区域
params.minArea = 16

# Filter by Circularity
params.filterByCircularity = True
# 设置类圆性
params.minCircularity = 0.3

# Filter by Convexity
params.filterByConvexity = True
# 设置最小凸性
params.minConvexity = 0.67  #0.57

# Filter by Inertia
params.filterByInertia = True
# 测量了一个形状有多长:对于一个圆,这个值是1,对于一个椭圆,它在0到1之间,对于一条直线,它是0
params.minInertiaRatio = 0.01

# Create a detector with the parameters
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3:
    detector = cv2.SimpleBlobDetector(params)
else:
    detector = cv2.SimpleBlobDetector_create(params)

# Detect blobs.
keypoints = detector.detect(im)

# Draw detected blobs as red circles.
# cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS ensures
# the size of the circle corresponds to the size of blob

im_with_keypoints = cv2.drawKeypoints(im_o, keypoints, np.array([]), (0, 0, 255),
                                      cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Show blobs
cv2.imshow("Keypoints", im_with_keypoints)
cv2.waitKey(0)

python-opencv 垫片缺陷检测_第1张图片
python-opencv 垫片缺陷检测_第2张图片

import numpy as np
import cv2
import matplotlib.pyplot as plt
# 2 铝垫圈表面压伤
image = cv2.imread("img/2.bmp", 0)
image1 = cv2.imread("img/2.bmp")
image2 = cv2.resize(image, (800, 600))
cv2.imshow('image1', image2)

# 定义两个圆形
circle1 = np.zeros(image.shape[0:2], dtype="uint8")
cv2.circle(circle1, (625, 500), 210, 255, -1)
circle2 = np.zeros(image.shape[0:2], dtype="uint8")
# cv2.circle(circle2, (625, 505), 284, 255, -1)
cv2.circle(circle2, (625, 505), 252, 255, -1)

# 异或运算,通过两个圆建立一个圆环作为掩膜
bitwiseXor = cv2.bitwise_xor(circle1, circle2)
mask = bitwiseXor
# cv2.imshow('Mask', mask)

# 将掩膜应用于原图
masked = cv2.bitwise_and(image, image, mask=mask)
# cv2.imshow('Mask applied to Image', masked)
# cv2.waitKey(0)

# 高斯滤波
im_o = cv2.resize(masked, (800, 600))
im_gauss = cv2.GaussianBlur(masked, (3, 3), 0)

# Canny算子
canny = cv2.Canny(im_gauss, 120, 240)
canny1 = cv2.resize(canny, (800, 600))
cv2.imshow('Canny', canny1)

# 1.image 不用多说,输入矩阵
# 2.method cv2.HOUGH_GRADIENT 也就是霍夫圆检测,梯度法
# 3.dp 计数器的分辨率图像像素分辨率与参数空间分辨率的比值(官方文档上写的是图像分辨率与累加器分辨率的比值,它把参数空间认为是一个累加器,毕竟里面存储的都是经过的像素点的数量),dp=1,则参数空间与图像像素空间(分辨率)一样大,dp=2,参数空间的分辨率只有像素空间的一半大
# 4.minDist 圆心之间最小距离,如果距离太小,会产生很多相交的圆,如果距离太大,则会漏掉正确的圆
# 5.param1 canny检测的双阈值中的高阈值,低阈值是它的一半
# 6.param2 最小投票数(基于圆心的投票数)
# 7.minRadius 需要检测圆的最小半径
# 8.maxRadius 需要检测圆的最大半径
circles = cv2.HoughCircles(canny, cv2.HOUGH_GRADIENT, 1, 10, param1=240, param2=10, minRadius=2, maxRadius=50)
circles = np.uint16(np.around(circles))  # 四舍五入,取整

for i in circles[0, :]:
    cv2.circle(image1, (i[0], i[1]), i[2], (0, 0, 255), 2)  # 画圆
    # cv2.circle(image, (i[0], i[1]), 2, (255, 0, 255), 10)  # 画圆心

image2 = cv2.resize(image1, (800, 600))
cv2.imshow("HoughCircle", image2)
cv2.waitKey(0)
# cv2.destroyAllWindows()
# plt.subplot(121), plt.imshow(canny, 'gray')
# plt.title('img'), plt.xticks([]), plt.yticks([])
# plt.subplot(122), plt.imshow(im_gauss, 'gray')
# plt.title('img'), plt.xticks([]), plt.yticks([])
# plt.show()


# cv2.imshow('image',image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()

python-opencv 垫片缺陷检测_第3张图片
python-opencv 垫片缺陷检测_第4张图片

import numpy as np
import cv2
import matplotlib.pyplot as plt
# 1 铝垫圈外径顶角有缺料现象
image = cv2.imread("img/1.bmp", 0)
image1 = cv2.imread("img/1.bmp")
image2 = cv2.resize(image, (800, 600))
cv2.imshow('image1', image2)

# 定义两个圆形
circle1 = np.zeros(image.shape[0:2], dtype="uint8")
cv2.circle(circle1, (633, 543), 249, 255, -1)
circle2 = np.zeros(image.shape[0:2], dtype="uint8")
# cv2.circle(circle2, (625, 505), 284, 255, -1)
cv2.circle(circle2, (633, 543), 259, 255, -1)

# 异或运算,通过两个圆建立一个圆环作为掩膜
bitwiseXor = cv2.bitwise_xor(circle1, circle2)
mask = bitwiseXor
# cv2.imshow('Mask', mask)

# 将掩膜应用于原图
masked = cv2.bitwise_and(image, image, mask=mask)
# cv2.imshow('Mask applied to Image', masked)

# 高斯滤波
im_o = cv2.resize(masked, (800, 600))
im_gauss = cv2.GaussianBlur(masked, (3, 3), 0)
ret, im = cv2.threshold(im_gauss, 60, 255, 0)
cv2.imshow('threshold', im)

# 开运算:先腐蚀,后膨胀
kernel = np.ones((3, 3), np.uint8)
opening = cv2.morphologyEx(im, cv2.MORPH_OPEN, kernel)
opening = cv2.morphologyEx(opening, cv2.MORPH_OPEN, kernel)
# cv2.imshow('open', opening)

# Canny算子
canny = cv2.Canny(opening, 100, 200)
canny1 = cv2.resize(canny, (800, 600))
cv2.imshow('Canny', canny1)

# 1.image 不用多说,输入矩阵
# 2.method cv2.HOUGH_GRADIENT 也就是霍夫圆检测,梯度法
# 3.dp 计数器的分辨率图像像素分辨率与参数空间分辨率的比值(官方文档上写的是图像分辨率与累加器分辨率的比值,它把参数空间认为是一个累加器,毕竟里面存储的都是经过的像素点的数量),dp=1,则参数空间与图像像素空间(分辨率)一样大,dp=2,参数空间的分辨率只有像素空间的一半大
# 4.minDist 圆心之间最小距离,如果距离太小,会产生很多相交的圆,如果距离太大,则会漏掉正确的圆
# 5.param1 canny检测的双阈值中的高阈值,低阈值是它的一半
# 6.param2 最小投票数(基于圆心的投票数)
# 7.minRadius 需要检测圆的最小半径
# 8.maxRadius 需要检测圆的最大半径
circles = cv2.HoughCircles(canny, cv2.HOUGH_GRADIENT, 1, 10, param1=200, param2=5, minRadius=6, maxRadius=11)
circles = np.uint16(np.around(circles))  # 四舍五入,取整

for i in circles[0, :]:
    cv2.circle(image1, (i[0], i[1]), i[2], (0, 0, 255), 2)  # 画圆
    # cv2.circle(image, (i[0], i[1]), 2, (255, 0, 255), 10)  # 画圆心

image2 = cv2.resize(image1, (800, 600))
cv2.imshow("HoughCircle", image2)
cv2.waitKey(0)

python-opencv 垫片缺陷检测_第5张图片
python-opencv 垫片缺陷检测_第6张图片

import cv2
import numpy as np
# 4 M12螺纹导向处有碰伤
image = cv2.imread("img/4.bmp")
image1 = cv2.resize(image, (800, 600))
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('image1', image1)

# 定义两个圆形
circle1 = np.zeros(image.shape[0:2], dtype="uint8")
cv2.circle(circle1, (628, 493), 211, 255, -1)
circle2 = np.zeros(image.shape[0:2], dtype="uint8")
# cv2.circle(circle2, (625, 505), 284, 255, -1)
cv2.circle(circle2, (628, 493), 256, 255, -1)

# 异或运算,通过两个圆建立一个圆环作为掩膜
bitwiseXor = cv2.bitwise_xor(circle1, circle2)
mask = bitwiseXor
# cv2.imshow('Mask', mask)

# 将掩膜应用于原图
masked = cv2.bitwise_and(image, image, mask=mask)
# cv2.imshow('Mask applied to Image', masked)


im_o = cv2.resize(masked, (800, 600))
im_gauss = cv2.GaussianBlur(im_o, (7, 7), 0)
ret, im = cv2.threshold(im_gauss, 180, 255, 0)
# cv2.imshow("o", im)

canny = cv2.Canny(im, 64, 128)
cv2.imshow('Canny', canny)

# 1.image 不用多说,输入矩阵
# 2.method cv2.HOUGH_GRADIENT 也就是霍夫圆检测,梯度法
# 3.dp 计数器的分辨率图像像素分辨率与参数空间分辨率的比值(官方文档上写的是图像分辨率与累加器分辨率的比值,它把参数空间认为是一个累加器,毕竟里面存储的都是经过的像素点的数量),dp=1,则参数空间与图像像素空间(分辨率)一样大,dp=2,参数空间的分辨率只有像素空间的一半大
# 4.minDist 圆心之间最小距离,如果距离太小,会产生很多相交的圆,如果距离太大,则会漏掉正确的圆
# 5.param1 canny检测的双阈值中的高阈值,低阈值是它的一半
# 6.param2 最小投票数(基于圆心的投票数)
# 7.minRadius 需要检测圆的最小半径
# 8.maxRadius 需要检测圆的最大半径
circles = cv2.HoughCircles(canny, cv2.HOUGH_GRADIENT, 1, 10, param1=128, param2=3, minRadius=1, maxRadius=0)
circles = np.uint16(np.around(circles))  # 四舍五入,取整

for i in circles[0, :]:
    cv2.circle(image1, (i[0], i[1]), i[2], (0, 0, 255), 2)  # 画圆
    # cv2.circle(image, (i[0], i[1]), 2, (255, 0, 255), 10)  # 画圆心

cv2.imshow("HoughCircle", image1)
cv2.waitKey(0)

python-opencv 垫片缺陷检测_第7张图片
python-opencv 垫片缺陷检测_第8张图片

import cv2
import numpy as np
# 7 后座底部有碰伤
image = cv2.imread("img/7.bmp")
image1 = cv2.resize(image, (800, 600))
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('image1', image1)

# 定义一个圆形
circle1 = np.zeros(image.shape[0:2], dtype="uint8")
cv2.circle(circle1, (655, 447), 122, 255, -1)

mask = circle1
masked = cv2.bitwise_and(image, image, mask=mask)
# cv2.imshow("masked", masked)

im_o = cv2.resize(masked, (800, 600))
im_gauss = cv2.GaussianBlur(im_o, (3, 3), 0)
ret, im = cv2.threshold(im_gauss, 80, 255, 0)
# cv2.imshow("o", im)

canny = cv2.Canny(im, 100, 200)
cv2.imshow('Canny', canny)

# 1.image 不用多说,输入矩阵
# 2.method cv2.HOUGH_GRADIENT 也就是霍夫圆检测,梯度法
# 3.dp 计数器的分辨率图像像素分辨率与参数空间分辨率的比值(官方文档上写的是图像分辨率与累加器分辨率的比值,它把参数空间认为是一个累加器,毕竟里面存储的都是经过的像素点的数量),dp=1,则参数空间与图像像素空间(分辨率)一样大,dp=2,参数空间的分辨率只有像素空间的一半大
# 4.minDist 圆心之间最小距离,如果距离太小,会产生很多相交的圆,如果距离太大,则会漏掉正确的圆
# 5.param1 canny检测的双阈值中的高阈值,低阈值是它的一半
# 6.param2 最小投票数(基于圆心的投票数)
# 7.minRadius 需要检测圆的最小半径
# 8.maxRadius 需要检测圆的最大半径
circles = cv2.HoughCircles(canny, cv2.HOUGH_GRADIENT, 1, 20, param1=200, param2=6, minRadius=3, maxRadius=10)
circles = np.uint16(np.around(circles))  # 四舍五入,取整

for i in circles[0, :]:
    cv2.circle(image1, (i[0], i[1]), i[2], (0, 0, 255), 2)  # 画圆
    # cv2.circle(image, (i[0], i[1]), 2, (255, 0, 255), 10)  # 画圆心

cv2.imshow("HoughCircle", image1)
cv2.waitKey(0)

python-opencv 垫片缺陷检测_第9张图片
python-opencv 垫片缺陷检测_第10张图片

import cv2
import numpy as np
# 3 铝垫圈表面有贯穿伤
image = cv2.imread("img/3.bmp")
image1 = cv2.resize(image, (800, 600))
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imshow('image1', image1)
# cv2.waitKey(0)

# 定义两个圆形
circle1 = np.zeros(image.shape[0:2], dtype="uint8")
cv2.circle(circle1, (605, 529), 246, 255, -1)
circle2 = np.zeros(image.shape[0:2], dtype="uint8")
# cv2.circle(circle2, (625, 505), 284, 255, -1)
cv2.circle(circle2, (605, 529), 204, 255, -1)

# 异或运算,通过两个圆建立一个圆环作为掩膜
bitwiseXor = cv2.bitwise_xor(circle1, circle2)
mask = bitwiseXor
cv2.imshow('Mask', mask)

# 将掩膜应用于原图
masked = cv2.bitwise_and(image, image, mask=mask)
# cv2.imshow('Mask applied to Image', masked)

im_o = cv2.resize(masked, (800, 600))
im_gauss = cv2.GaussianBlur(im_o, (3, 3), 0)
ret, im = cv2.threshold(im_gauss, 180, 255, 0)
# cv2.imshow("o", im)
# cv2.waitKey(0)

canny = cv2.Canny(im, 64, 128)
cv2.imshow('Canny', canny)

# 1.image 不用多说,输入矩阵
# 2.method cv2.HOUGH_GRADIENT 也就是霍夫圆检测,梯度法
# 3.dp 计数器的分辨率图像像素分辨率与参数空间分辨率的比值(官方文档上写的是图像分辨率与累加器分辨率的比值,它把参数空间认为是一个累加器,毕竟里面存储的都是经过的像素点的数量),dp=1,则参数空间与图像像素空间(分辨率)一样大,dp=2,参数空间的分辨率只有像素空间的一半大
# 4.minDist 圆心之间最小距离,如果距离太小,会产生很多相交的圆,如果距离太大,则会漏掉正确的圆
# 5.param1 canny检测的双阈值中的高阈值,低阈值是它的一半
# 6.param2 最小投票数(基于圆心的投票数)
# 7.minRadius 需要检测圆的最小半径
# 8.maxRadius 需要检测圆的最大半径
circles = cv2.HoughCircles(canny, cv2.HOUGH_GRADIENT, 1, 10, param1=128, param2=5, minRadius=1, maxRadius=0)
circles = np.uint16(np.around(circles))  # 四舍五入,取整

for i in circles[0, :]:
    cv2.circle(image1, (i[0], i[1]), i[2], (0, 0, 255), 2)  # 画圆
    # cv2.circle(image, (i[0], i[1]), 2, (255, 0, 255), 10)  # 画圆心

cv2.imshow("HoughCircle", image1)
cv2.waitKey(0)

python-opencv 垫片缺陷检测_第11张图片
python-opencv 垫片缺陷检测_第12张图片

import cv2
import numpy as np
# 5 六面角端面有碰伤
image = cv2.imread("img/5.bmp")
image2 = cv2.resize(image, (800, 600))
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

image = cv2.circle(image, (644, 503), 293, 0, -1)
image1 = cv2.resize(image, (800, 600))
# cv2.imshow('image2', image2)
cv2.imshow('image1', image1)
# cv2.waitKey(0)

im_gauss = cv2.GaussianBlur(image1, (3, 3), 0)
ret, im = cv2.threshold(im_gauss, 40, 255, 0)
cv2.imshow("o", im)
# canny = cv2.Canny(im, 125, 250)
canny = cv2.Canny(im, 50, 100)
cv2.imshow('Canny', canny)
# cv2.waitKey(0)

# 1.image 不用多说,输入矩阵
# 2.method cv2.HOUGH_GRADIENT 也就是霍夫圆检测,梯度法
# 3.dp 计数器的分辨率图像像素分辨率与参数空间分辨率的比值(官方文档上写的是图像分辨率与累加器分辨率的比值,它把参数空间认为是一个累加器,毕竟里面存储的都是经过的像素点的数量),dp=1,则参数空间与图像像素空间(分辨率)一样大,dp=2,参数空间的分辨率只有像素空间的一半大
# 4.minDist 圆心之间最小距离,如果距离太小,会产生很多相交的圆,如果距离太大,则会漏掉正确的圆
# 5.param1 canny检测的双阈值中的高阈值,低阈值是它的一半
# 6.param2 最小投票数(基于圆心的投票数)
# 7.minRadius 需要检测圆的最小半径
# 8.maxRadius 需要检测圆的最大半径
# circles = cv2.HoughCircles(canny, cv2.HOUGH_GRADIENT, 1, 20, param1=250, param2=5, minRadius=4, maxRadius=8)
circles = cv2.HoughCircles(canny, cv2.HOUGH_GRADIENT, 1, 20, param1=100, param2=7, minRadius=6, maxRadius=1)
circles = np.uint16(np.around(circles))  # 四舍五入,取整

for i in circles[0, :]:
    cv2.circle(image2, (i[0], i[1]), i[2], (0, 0, 255), 2)  # 画圆
    # cv2.circle(image, (i[0], i[1]), 2, (255, 0, 255), 10)  # 画圆心

cv2.imshow("HoughCircle", image2)
cv2.waitKey(0)

python-opencv 垫片缺陷检测_第13张图片
python-opencv 垫片缺陷检测_第14张图片

你可能感兴趣的:(python-opencv 垫片缺陷检测)