【发布时间】:2020-10-28 19:31:27
【问题描述】:
目标是检测大圆圈内的小圆圈。
目前我所做的是将图像转换为灰度并应用阈值 (cv2.THRESH_OTSU),从而生成此图像
在此之后,我使用我在 stackoverflow 上找到的椭圆形内核应用了 Morph open 的 findcontours 过滤掉了大型对象
有人可以指导我正确的做法,以及我哪里出错了。
下面是我一直在研究的附加代码
import cv2
import numpy as np
# Load image, grayscale, Otsu's threshold
image = cv2.imread('01.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
#cv2.imwrite('thresh.jpg', thresh)
# Filter out large non-connecting objects
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
area = cv2.contourArea(c)
#print(area)
if area < 200 and area > 0:
cv2.drawContours(thresh,[c],0,0,-1)
# Morph open using elliptical shaped kernel
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=3)
# Find circles
cnts = cv2.findContours(opening, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
area = cv2.contourArea(c)
if area > 20 and area < 50:
((x, y), r) = cv2.minEnclosingCircle(c)
cv2.circle(image, (int(x), int(y)), int(r), (36, 255, 12), 2)
cv2.namedWindow('orig', cv2.WINDOW_NORMAL)
cv2.imshow('orig', thresh)
cv2.namedWindow('image', cv2.WINDOW_NORMAL)
cv2.imshow('image', image)
cv2.waitKey()
谢谢!
【问题讨论】:
-
一个可能有用的想法,如果你得到最大圆的轮廓,那么它里面的所有轮廓都将是你想要的更小的圆。所以,试着找出最大的圆圈。
-
我猜你可以牺牲几个点来对抗“大圆圈”。然后过滤大小的斑点是一件容易的事。
标签: python opencv image-processing computer-vision opencv-contour