gpt4 book ai didi

python - Blob 检测不起作用

转载 作者:行者123 更新时间:2023-11-30 22:41:43 26 4
gpt4 key购买 nike

我正在尝试检测图像中的 Blob ,但不知何故它不起作用。基本上我想确定圆圈的数量。

代码:

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()

这是输入图像:

Image

最佳答案

我已经找到解决办法了。

当涉及到 Blob 检测时;您正在识别图像中的异常值/不需要的对象。因此,这些所谓的 Blob /异常值被假定为在普通背景上呈黑色/灰色。换句话说, Blob 被认为在白色背景下很容易识别。

当您对原始图像的灰度图像执行 Blob 检测时,出现的背景为黑色,如下所示:

enter image description here

在黑色背景下 Blob 检测什么也没发现:(

我该怎么办?

这就是我所做的。这只是黑客的一行。我模糊了灰度图像,然后使用 inverted_img = cv2.bitwise_not(blur)

将其反转

然后我将获得的图像传递给 Blob 检测函数。这就是我得到的:

enter image description here

我还可以获得存在的 Blob 数量:

number = 0
for i in keypoints[0:]:
number = number + 1

print "Number of blobs:",number

这就是我在控制台屏幕上看到的内容:

enter image description here

关于python - Blob 检测不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42410390/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com