我正在尝试使用opencv SimpleBlobDetector。我当前的程序是一个像这样的简单测试程序:
import cv2
import numpy as np;
im = cv2.imread("blobs.jpg", cv2.IMREAD_GRAYSCALE)
params = cv2.SimpleBlobDetector_Params()
params.filterByArea = True;
params.minArea = 1;
params.maxArea = 1000;
detector = cv2.SimpleBlobDetector(params)
keypoints = detector.detect(im)
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)
代码一直运行到关键点行,该行应实际检测到斑点。它不显示任何错误消息,只是重新启动内核。我使用了非常简单的图片,因此应该检测到斑点。
似乎为我工作。如果使用的是Python 3,则要使用detector = cv2.SimpleBlobDetector_create(params)
,而不是以前的版本。我将最大面积增加到10000
像素,并且还包括了输出的屏幕截图:
import cv2
import numpy as np;
im = cv2.imread("blobs.jpg", cv2.IMREAD_GRAYSCALE)
params = cv2.SimpleBlobDetector_Params()
params.filterByArea = True;
params.minArea = 1;
params.maxArea = 10000;
detector = cv2.SimpleBlobDetector_create(params)
keypoints = detector.detect(im)
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)