如何解决通过 OpenCV 在原始图像上标记 Blob 检测
我想用找到的 blob 标记原始图像。但是每当我进行 blob 检测时,它只会生成这样的新图像:
blob 后的结果图像:
我只是使用常规代码进行斑点检测。有没有另一种方法可以在原始图像上做红色圆圈?所以很清楚他们在哪里。
im_gray = cv2.imread(img,cv2.IMREAD_GRAYSCALE)
(thresh,im_bw) = cv2.threshold(im_gray,128,255,cv2.THRESH_BINARY |
cv2.THRESH_OTSU)
thresh = 50
im_bw = cv2.threshold(im_gray,thresh,cv2.THRESH_BINARY)[1]
#detect blobs based on features
params = cv2.SimpleBlobDetector_Params()
# Filter by Area.
params.filterByArea = True
params.minArea = 70
params.maxArea = 150
# Filter by Color (black=0)
params.filterByColor = False # Set true for cast_iron as we'll be detecting black regions
params.blobColor = 0
# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0.5
params.maxCircularity = 1
# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.5
params.maxConvexity = 1
# Filter by Inertiaratio
params.filterByInertia = True
params.minInertiaratio = 0.3
params.maxInertiaratio = 0.9
# distance Between Blobs
params.mindistBetweenBlobs = 0
#thresholded to value 70 detecting blobs:
detector = cv2.SimpleBlobDetector_create(params)
keypoints = detector.detect(im_bw)
print("Number of blobs detected are : ",len(keypoints))
#detect blobs: missing the detection based on features
im_with_keypoints = cv2.drawKeypoints(im_bw,keypoints,numpy.array([]),(0,255),cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
解决方法
你的问题是你的最后一行:
im_with_keypoints = cv2.drawKeypoints(im_bw,keypoints,numpy.array([]),(0,255),cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
在这里,您在 im_bw
上绘制关键点,这不是您的原始图像,而是阈值图像。如果您将此处的 im_bw
替换为您的原始图像(例如,使用您已经加载为 im_gray
的灰度版本),您应该会得到您想要的结果。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。