opencv blob检测,缺少一些blob

时间:2018-01-29 20:04:52

标签: python opencv image-processing blob opencv3.0

import cv2
import numpy as np

im_m = cv2.imread("primary.png", cv2.IMREAD_GRAYSCALE)

# 50, 130

# Otsu's thresholding after Gaussian filtering 
blur = cv2.GaussianBlur(im_m,(25,25),0)
#(thresh, im2) = cv2.threshold(blur,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
# Black region overflowing into digit regions with otsu

(thresh, im) = cv2.threshold(im_m, 55, 130, cv2.THRESH_BINARY)

kernel = np.ones((5,5), np.uint8)

img_dilation = cv2.dilate(im, kernel, iterations=1)
img_erosion = cv2.erode(img_dilation, kernel, iterations=1)


params = cv2.SimpleBlobDetector_Params()


params.minThreshold = 10;
params.maxThreshold = 225;


params.filterByArea = True
params.minArea = 100
params.maxArea = 1500


params.filterByCircularity = False
params.minCircularity = 0.5


params.filterByConvexity = False
params.minConvexity = 0.2


params.filterByInertia = False
params.minInertiaRatio = 0.01


ver = (cv2.__version__).split('.')
if int(ver[0]) < 3 :
    detector = cv2.SimpleBlobDetector(params)
else : 
    detector = cv2.SimpleBlobDetector_create(params)



keypoints = detector.detect(im)


im_key = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255),             cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)




cv2.imshow("Keypoints", im_key)
cv2.imshow("Erosion", img_erosion)
cv2.imshow("Dilation", img_dilation)
cv2.waitKey(0)

我对使用opencv进行图像处理非常陌生。我正在尝试从嘈杂的图像中检测数字。

Input Image

我做了一些实验,调整阈值和按参数过滤,但是无法检测到具有3,4,0和有时5,7和2的斑点。我只需要检测数字。除了blob检测还有其他更好的方法吗?

Output Image

1 个答案:

答案 0 :(得分:0)

通常,您应该尝试使用大多数参数的不同值。我通常有一个带有一些滚动条的GUI,以便以交互方式修改参数。

在你的情况下,我猜maxarea太小了。您可以将其设为10000,如果有效,请将其缩小直至开始失败。如果它不起作用,你可以尝试使minarea更小。

我认为您没有其他参数,但是如果您这样做了,则应禁用所有过滤器,使所有范围更宽,然后逐个启用并缩小它们(始终查看图像结果)

相关问题