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

Why cant I do blob detection on this binary image

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

颜色检测后的图像为binary mask

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

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)`

这段代码让我很困惑,因为它适用于这张图片 white dots 我认为白点图像与二进制掩码非常相似,但为什么我不能对二进制图像进行斑点检测?有人能告诉我区别或正确的代码吗?

谢谢!!

此致, 楠

这是按颜色过滤的 opencv 错误。您需要做的就是反转图像的颜色 -> 检测斑点 -> 再次反转以恢复原始颜色

代码

import cv2
import numpy as np;

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

# Set up the detector with default parameters.
im=cv2.bitwise_not(im)

params = cv2.SimpleBlobDetector_Params()
detector = cv2.SimpleBlobDetector_create(params)


# Detect blobs.
keypoints = detector.detect(im)
im=cv2.bitwise_not(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)

输出

最简单的方法就是@ArjitMukherjee 所说的。

但我也回应了 @meetaig 最初评论的关于两幅图像中斑点结构差异的评论

A clue for why it might not work could be the structure of the blobs. In the first image the white pixels are not all connected to a big blob (meaning there are a few single pixels "floating around") whereas in the second image the circles are perfect blobs

您需要微调您的算法,使其suits/aligns具有不同的斑点结构

我做了一些快速微调,可以部分满足你的要求:

import cv2
import numpy as np;

# Read image
im = cv2.imread("eRCe1.png", 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 = True
params.minArea = 300

detector = cv2.SimpleBlobDetector(params)

# Detect blobs.
keypoints = detector.detect(im)

print keypoints
# 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)


cv2.imshow("Keypoints", im_with_keypoints)
cv2.waitKey(0)

在您提供的两张相同图像上执行上述代码,下面是输出

示例 1:

示例 2:

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

import cv2
params = cv2.SimpleBlobDetector_Params()
print params.filterByColor
print params.filterByArea
print params.filterByCircularity
print params.filterByInertia
print params.filterByConvexity

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

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

# Disable unwanted filter criteria params
params.filterByInertia = False
params.filterByConvexity = False

... 然后调用 detector = cv2.SimpleBlobDetector(params) 你会得到下图:

该图像中的第三个斑点是由图像右下角的白框引起的。 您可以裁剪图像,如果框架总是在同一个地方,或者您可以使用参数按圆度过滤并删除不需要的斑点:

params.filterByCircularity = True
params.minCircularity = 0.1

你最终会得到: