Blob检测不起作用

时间:2017-02-23 08:20:34

标签: python opencv

我正在尝试检测图像中的斑点,但它不能以某种方式工作。基本上我想确定圈数。

代码:

import cv2
import numpy as np
import sys
# Read image
im = cv2.imread("K.jpg", cv2.IMREAD_GRAYSCALE)

# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()

# Change thresholds
params.minThreshold = 10
params.maxThreshold = 200

# Filter by Area.
params.filterByArea = True
params.minArea = 50

# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0.75

# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.87

# Filter by Inertia
params.filterByInertia = True
params.minInertiaRatio = 0.7

detector = cv2.SimpleBlobDetector_create(params)


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

# Show blobs
cv2.imshow("Keypoints", im_with_keypoints)
if cv2.waitKey(0) and 0xff==27:
    cv2.destroyAllWindows()

这是输入图像:

Image

1 个答案:

答案 0 :(得分:1)

我已经找到了解决方案。

当涉及到斑点的检测;您正在识别图像中的异常值/不需要的对象。因此,这些所谓的 blob / outliers 在普通背景上被假定为黑色/浅灰色。换句话说,假设blob可以在白色背景下轻松识别。

当您对原始图像的灰度图像执行斑点检测时,存在的背景为黑色,如下所示:

the docs

在黑色背景下斑点检测什么都找不到:(

我该怎么办?

这就是我所做的。这只是一个黑客行。我模糊了灰度图像,然后使用inverted_img = cv2.bitwise_not(blur)

将其反转

然后我将获得的图像传递给斑点检测功能。这就是我得到的:

enter image description here

我也能够获得存在的斑点数量:

number = 0
for i in keypoints[0:]:
    number = number + 1    

print "Number of blobs:",number

这就是我在控制台屏幕中得到的结果:

enter image description here

相关问题