改进轮廓检测
我想检测气泡中气泡的轮廓照片。但是,无论我使用哪种轮廓检测,我的程序都不会检测到整个轮廓线。我尝试使用腐蚀、模糊和morphologyEx方法使轮廓连续,但气泡形状似乎完全改变了。 请问还有其他方法可以帮助我正确绘制与原始照片相似的气泡轮廓吗? 我的代码如下:
cv2.imshow(self.window_name, clone)
canvas = cv2.imshow('canvas', canvas)
# waiting for the user to press any key
imageThresh = cv2.adaptiveThreshold(cloneG, 200, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 3, 11)
bubble_only = cv2.bitwise_and(imageThresh, imageThresh, mask = VA_mask)
thresh = cv2.adaptiveThreshold(bubble_only, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 3, 9)
kernel1 = np.ones((2,2), np.uint8)
# Perform morphological hit-or-miss for
erosion = cv2.morphologyEx(thresh, cv2.MORPH_ERODE, kernel1, iterations = 2)
# Inverted this image and blurred it with a kernel size of 4
ret, thresh1 = cv2.threshold(erosion, 127, 255, 1)
blur = cv2.blur(thresh1, (4,4))
# Again perform another threshold on this image to get the central portion of the edge
ret, thresh2 = cv2.threshold(blur, 145, 255, 0)
# Perform morphological dilation to thin the edge. Use an ellipse structuring element of kernel size of 2
kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (1,1))
final = cv2.morphologyEx(thresh2, cv2.MORPH_ERODE, kernel2, iterations = 2)
# find the contour then fill the contour
mask = np.zeros(clone.shape[:2], dtype = 'uint8')
contours, hier = cv2.findContours(final, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
bubble_shade = cv2.drawContours(mask, contours, -1, color = (255,255,255), thickness=cv2.FILLED)
area = []
for contour in contours:
cnt_Area = cv2.contourArea(contour)
area.append(cnt_Area)
print(sorted(area))
cv2.imwrite('bubble_shade.jpg', bubble_shade)
cv2.waitKey()
cv2.destroyAllWindows()
return clone
The bubbles are the target of contour detection
I would like to detect the contour of the bubble in the photo. However, no matter what kind of contour detection I used, my program would not detect the whole contour lines. I tried with erosion, blur and morphologyEx methods to make the contour continuous, but it seems the bubble shape has changed completely.
May I ask is there other method that can help me correctly draw the bubble contour similar as original photo?
My code is as following:
cv2.imshow(self.window_name, clone)
canvas = cv2.imshow('canvas', canvas)
# waiting for the user to press any key
imageThresh = cv2.adaptiveThreshold(cloneG, 200, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY_INV, 3, 11)
bubble_only = cv2.bitwise_and(imageThresh, imageThresh, mask = VA_mask)
thresh = cv2.adaptiveThreshold(bubble_only, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 3, 9)
kernel1 = np.ones((2,2), np.uint8)
# Perform morphological hit-or-miss for
erosion = cv2.morphologyEx(thresh, cv2.MORPH_ERODE, kernel1, iterations = 2)
# Inverted this image and blurred it with a kernel size of 4
ret, thresh1 = cv2.threshold(erosion, 127, 255, 1)
blur = cv2.blur(thresh1, (4,4))
# Again perform another threshold on this image to get the central portion of the edge
ret, thresh2 = cv2.threshold(blur, 145, 255, 0)
# Perform morphological dilation to thin the edge. Use an ellipse structuring element of kernel size of 2
kernel2 = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (1,1))
final = cv2.morphologyEx(thresh2, cv2.MORPH_ERODE, kernel2, iterations = 2)
# find the contour then fill the contour
mask = np.zeros(clone.shape[:2], dtype = 'uint8')
contours, hier = cv2.findContours(final, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
bubble_shade = cv2.drawContours(mask, contours, -1, color = (255,255,255), thickness=cv2.FILLED)
area = []
for contour in contours:
cnt_Area = cv2.contourArea(contour)
area.append(cnt_Area)
print(sorted(area))
cv2.imwrite('bubble_shade.jpg', bubble_shade)
cv2.waitKey()
cv2.destroyAllWindows()
return clone
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论