我正在尝试检测图像中的斑点,但它以某种方式不起作用。基本上我想确定圈数。
代码:
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()
Run Code Online (Sandbox Code Playgroud)
这是输入图像:
我已经想出了解决办法。
当涉及到斑点检测时;您正在识别图像中的异常值/不需要的对象。因此,假设这些所谓的斑点/异常值在纯背景上呈黑色/灰色。换句话说,斑点被认为在白色背景下很容易识别。
当您对原始图像的灰度图像执行斑点检测时,背景呈现为黑色,如下所示:
在黑色背景斑点检测中什么也没发现:(
这就是我所做的。这只是一行 hack。我模糊了灰度图像,然后使用inverted_img = cv2.bitwise_not(blur)
然后我将获得的图像传递给 blob 检测函数。这就是我得到的:
我也能够获得存在的 blob 数量:
number = 0
for i in keypoints[0:]:
number = number + 1
print "Number of blobs:",number
Run Code Online (Sandbox Code Playgroud)
这就是我在控制台屏幕上得到的:
| 归档时间: |
|
| 查看次数: |
2512 次 |
| 最近记录: |