gpt4 book ai didi

python - 填充检测到的 Blob (OpenCV,Python)

转载 作者:太空宇宙 更新时间:2023-11-03 22:09:11 29 4
gpt4 key购买 nike

我想用白色填充图像中检测到的 Blob 。这是我正在使用的代码:

# Standard imports
import cv2
import numpy as np

# Read image
im = cv2.imread("5.tif", 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 = 0.01
params.minArea = 0.05

# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0.1

# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.87

# Filter by Inertia
params.filterByInertia = True
params.minInertiaRatio = 0.02

# Create a detector with the parameters
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3:
detector = cv2.SimpleBlobDetector(params)
else:
detector = cv2.SimpleBlobDetector_create(params)

# Detect blobs.
keypoints = detector.detect(im)

# 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)
cv2.waitKey(0)

这是我收到的输出:

output

...和源图像:

src

代码取自here它解释得很好,但我不知道在哪里“触摸”上面的脚本来调整和做我要求的事情。

谢谢

最佳答案

将关键点绘制为实心白色圆圈:

img = im.copy()
for x in range(1,len(keypoints)):
img=cv2.circle(img, (np.int(keypoints[x].pt[0]),np.int(keypoints[x].pt[1])), radius=np.int(keypoints[x].size), color=(255), thickness=-1)

编辑:对于矩形或正方形,则:

for i in range(1,len(keypoints)):
x,y = np.int(keypoints[i].pt[0]),np.int(keypoints[i].pt[1])
sz = np.int(keypoints[i].size)
if sz > 1:
sz = np.int(sz/2)
# notice there's no boundary check for pt1 and pt2, you have to do that yourself
img = cv2.rectangle(img, (x-sz,y-sz), (x+sz,y+sz), color=(255), thickness=-1)

关于python - 填充检测到的 Blob (OpenCV,Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47156711/

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