【发布时间】:2017-08-14 23:44:09
【问题描述】:
作为我正在进行的项目的一部分,我需要使用 OpenCV 和 Python 在图像中找到一些“斑点”的中心点。 我遇到了一些麻烦,非常感谢任何帮助或见解:)
我目前的方法是:获取图像的轮廓,在其上覆盖椭圆,使用斑点检测器找到每个图像的中心。 这工作得相当好,但有时我需要忽略多余的斑点,有时这些斑点会相互接触。
以下是运行良好的示例: 良好的源图片: 提取轮廓后: 检测到 blob:
当它运行不佳时(您可以看到它错误地将一个椭圆覆盖在三个斑点上,并检测到一个我不想要的): 错误的源图片: 提取轮廓后: 检测到 blob:
这是我目前使用的代码。我不确定还有其他选择。
def process_and_detect(img_path):
img = cv2.imread(path)
imgray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(imgray, 50, 150, 0)
im2, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
drawn_img = np.zeros(img.shape, np.uint8)
min_area = 50
min_ellipses = []
for cnt in contours:
if cv2.contourArea(cnt) >= min_area:
ellipse = cv2.fitEllipse(cnt)
cv2.ellipse(drawn_img,ellipse,(0,255,0),-1)
plot_img(drawn_img, size=12)
# Change thresholds
params = cv2.SimpleBlobDetector_Params()
params.filterByColor = True
params.blobColor = 255
params.filterByCircularity = True
params.minCircularity = 0.75
params.filterByArea = True
params.minArea = 150
# Set up the detector
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs.
keypoints = detector.detect(drawn_img)
for k in keypoints:
x = round(k.pt[0])
y = round(k.pt[1])
line_length = 20
cv2.line(img, (x-line_length, y), (x+line_length, y), (255, 0, 0), 2)
cv2.line(img, (x, y-line_length), (x, y+line_length), (255, 0, 0), 2)
plot_img(img, size=12)
非常感谢您阅读本文,我真诚地希望有人可以帮助我,或者指出我正确的方向。谢谢!
【问题讨论】:
-
你事先知道会有多少个blob吗?所有 blob 的大小是否应该相似,或者您可能有一个与这 3 个组合 blob 大小相同的 blob?这条管道非常多余,因为
SimpleBlobDetector有自己的一组阈值和轮廓检测操作。在申请SimpleBlobDetector之前,您已经完成了大部分工作。 -
我正在使用的 blob 是神经网络的输出,因此我知道训练期间的 blob 数量,但不知道在测试时 - 这是关键时刻。组合大小为 3 的 blob 不太可能出现,但它们的大小确实会有些波动。
标签: python opencv image-processing