我想用白色填充我图像中检测到的斑点。这是我正在使用的代码:
# 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)
这是我收到的输出:
...和源图片:
代码取自here 它解释得很好,但我不知道在哪里触摸&#34;上面的脚本来调整和执行我要求的内容。
由于
答案 0 :(得分:3)
将关键点绘制为填充的白色圆圈:
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)