只需尝试使用blob来检测我的图像,请使用此处的示例: https://www.learnopencv.com/blob-detection-using-opencv-python-c/, 但是它什么也没检测到。
我尝试使用原始图像,灰色图像,并将其阈值设置为仅黑白,但它们均未检测到任何斑点,并且关键点始终保持为0。
import numpy as np
import cv2
im_width = 320
im_height = 240
img = cv2.imread("D:\\20190822\\racket.bmp")
GreyImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh=cv2.threshold(GreyImage,50,255,cv2.THRESH_BINARY)
detector = cv2.SimpleBlobDetector_create()
keypoints = detector.detect(thresh)
blobs = cv2.drawKeypoints(thresh, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
print(len(keypoints))
cv2.imshow("Keypoints", blobs)
cv2.waitKey(0)
cv2.destroyAllWindows()
答案 0 :(得分:0)
我不是这方面的专家,但是从文档中可以看出,默认值是查找圆形斑点。除了一些小点,您没有圆圈。因此,您必须放宽所有参数以适应各种形状。见
https://docs.opencv.org/3.4/d0/d7a/classcv_1_1SimpleBlobDetector.html
https://docs.opencv.org/3.4/d2/d29/classcv_1_1KeyPoint.html#a308006c9f963547a8cff61548ddd2ef2
https://craftofcoding.wordpress.com/tag/cv2/
所以尝试一下:
输入:
import numpy as np
import cv2
import math
img = cv2.imread("racket.png")
GreyImage=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret,thresh=cv2.threshold(GreyImage,50,255,cv2.THRESH_BINARY)
# erode to one large blob
#thresh = cv2.erode(thresh, None, iterations=4)
cv2.imshow("Threshold", thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Set up the SimpleBlobdetector with default parameters.
params = cv2.SimpleBlobDetector_Params()
# Change thresholds
params.minThreshold = 0
params.maxThreshold = 256
# Filter by Area.
params.filterByArea = True
params.minArea = 0
params.maxArea = 100000000000
# Filter by Color (black)
params.filterByColor = True
params.blobColor = 0
# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0
params.maxCircularity = 100000000
# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0
params.maxConvexity = 100000000
# Filter by InertiaRatio
params.filterByInertia = True
params.minInertiaRatio = 0
params.maxInertiaRatio = 100000000
# Distance Between Blobs
params.minDistBetweenBlobs = 0
# Do detecting
detector = cv2.SimpleBlobDetector_create(params)
# Get keypoints
keypoints = detector.detect(thresh)
print(len(keypoints))
print('')
# Get keypoint locations and radius
for keypoint in keypoints:
x = int(keypoint.pt[0])
y = int(keypoint.pt[1])
s = keypoint.size
r = int(math.floor(s/2))
print (x,y,r)
#cv2.circle(img, (x, y), r, (0, 0, 255), 2)
# Draw blobs
blobs = cv2.drawKeypoints(thresh, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow("Keypoints", blobs)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Save result
cv2.imwrite("racket_blobs.jpg", blobs)
42
136 226 35
138 225 1
136 225 1
134 225 1
140 223 1
122 223 1
137 222 1
144 221 1
114 221 1
83 232 9
144 219 1
150 217 1
114 215 1
164 209 1
158 209 1
163 206 1
118 203 1
128 195 1
175 194 1
134 189 1
185 184 1
154 175 1
197 174 1
159 174 1
157 172 1
196 171 1
162 171 1
200 169 1
198 167 1
204 165 1
200 165 1
200 163 1
211 162 1
179 160 1
208 159 1
210 157 1
204 157 1
135 227 1
203 156 1
214 155 1
204 155 1
200 155 1
如果要查看形状,则最好使用轮廓而不是斑点。