为什么我不能对此二进制图像进行斑点检测

Nan*_*Shi 3 python binary opencv blob image-processing

首先,现在我正在使用Opencv通过Python2.7进行斑点检测。我要做的是在颜色检测后完成斑点检测。我想检测红色圆圈(标记),并避免其他斑点干扰,我想先进行颜色检测,然后再进行斑点检测。

颜色检测后的图像是二进制蒙版

现在,我想对此图像进行斑点检测,但是它不起作用。这是我的代码。

import cv2
import numpy as np;

# Read image
im = cv2.imread("myblob.jpg", cv2.IMREAD_GRAYSCALE)

# Set up the detector with default parameters.

params = cv2.SimpleBlobDetector_Params()
# Change thresholds
params.minThreshold = 10;    # the graylevel of images
params.maxThreshold = 200;

params.filterByColor = True
params.blobColor = 255

# Filter by Area
params.filterByArea = False
params.minArea = 10000

detector = cv2.SimpleBlobDetector(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, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Show keypoints
cv2.imshow("Keypoints", im_with_keypoints)
cv2.waitKey(0)`
Run Code Online (Sandbox Code Playgroud)

我对这个代码感到非常困惑,因为它可以在该图像的白点上工作, 我认为白点图像与二进制蒙版很安静,但是为什么我不能对二进制图像进行斑点检测?正确的代码?

谢谢!!

问候,南

Mar*_*cos 6

看起来斑点检测器默认已启用filterByInertiafilterByConvexity参数。您可以在系统中进行检查:

import cv2
params = cv2.SimpleBlobDetector_Params()
print params.filterByColor
print params.filterByArea
print params.filterByCircularity
print params.filterByInertia
print params.filterByConvexity
Run Code Online (Sandbox Code Playgroud)

因此,当您打电话detector = cv2.SimpleBlobDetector(params)时,实际上还使用默认的最小值和最大值按惯性和凸性进行过滤。

如果您明确禁用那些过滤条件:

# Disable unwanted filter criteria params
params.filterByInertia = False
params.filterByConvexity = False
Run Code Online (Sandbox Code Playgroud)

...然后致电给detector = cv2.SimpleBlobDetector(params)您得到以下图像: 膨胀结果

该图像中的第三个斑点是由图像右下方的白框引起的。如果框架始终位于同一位置,则可以裁剪图像,也可以使用参数按圆形度过滤并删除不希望的斑点:

params.filterByCircularity = True
params.minCircularity = 0.1
Run Code Online (Sandbox Code Playgroud)

您将最终得到:

在此处输入图片说明