OpenCV blob检测无法区分定义明确的blob

时间:2017-05-17 20:06:49

标签: python-2.7 opencv

我试图在一些图像中检测定义明确的斑点,我可以很容易地将图像阈值设置为只有我想要检测的白点,但斑点检测器似乎将它们中的一些组合在一起或者错过其他图像。有一个更好的方法吗?有没有办法找到不是超慢的连接白色像素的每个岛?

阈值图像:

Thresholded image

检测到Blob:

Detected Blobs

用于将阈值处理为blob的代码:

import numpy as np
import cv2

params = cv2.SimpleBlobDetector_Params()
# we are looking for white blobs in a mask
params.blobColor = 255
params.filterByColor = True
params.filterByArea = True
params.minArea = 0
# Blobs larger than 50 pixels are noise
params.maxArea = 50
# enabling these can cause us to miss points
params.filterByCircularity = False
params.filterByConvexity = False
params.filterByInertia = False
detector = cv2.SimpleBlobDetector_create(params)

img = cv2.imread('Threshold Out.png', flags=cv2.IMREAD_GRAYSCALE)

keypoints = detector.detect(img)

img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
img = cv2.drawKeypoints(img, keypoints, np.array([]), (0, 0, 255),
                        cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

while True:
    cv2.imshow('out', img)
    key = cv2.waitKey(0)
    if key == 27:
        break

1 个答案:

答案 0 :(得分:1)

在阅读C docs for the blob detector params之后,有一个名为minDistBetweenBlobs的参数,并将此设置为1解决了我的问题。

检测到Blob:

enter image description here

更新的代码:

import numpy as np
import cv2

params = cv2.SimpleBlobDetector_Params()
# we are looking for white blobs in a mask
params.blobColor = 255
params.filterByColor = True
params.filterByArea = True
params.minDistBetweenBlobs = 1
params.minArea = 0
# Blobs larger than 50 pixels are noise
params.maxArea = 50
# enabling these can cause us to miss points
params.filterByCircularity = False
params.filterByConvexity = False
params.filterByInertia = False
detector = cv2.SimpleBlobDetector_create(params)

img = cv2.imread('Threshold Out.png', flags=cv2.IMREAD_GRAYSCALE)

keypoints = detector.detect(img)

img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
img = cv2.drawKeypoints(img, keypoints, np.array([]), (0, 0, 255),
                        cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

while True:
    cv2.imshow('out', img)
    key = cv2.waitKey(0)
    if key == 27:
        break
相关问题