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