填充检测到的blob(OpenCV、Python)
我想用白色填充图像中检测到的斑点。这是我正在使用的代码:填充检测到的blob(OpenCV、Python),python,opencv,Python,Opencv,我想用白色填充图像中检测到的斑点。这是我正在使用的代码: # 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.mi
# 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)
#标准导入
进口cv2
将numpy作为np导入
#读取图像
im=cv2.imread(“5.tif”,cv2.imread_灰度)
#设置SimpleBlobDetector参数。
params=cv2.SimpleBlobDetector_params()
#更改阈值
params.minThreshold=10
params.maxThreshold=200
#按面积过滤。
params.filterByArea=True
参数minArea=0.01
参数minArea=0.05
#圆度滤波
params.filterByCircularity=真
参数最小圆度=0.1
#凸性滤波
params.filterByConvexity=True
params.Min凸度=0.87
#惯性过滤
params.filterByInertia=True
参数minInertiaRatio=0.02
#使用参数创建一个检测器
版本=(cv2.版本.拆分('.'))
如果int(ver[0])<3:
检测器=cv2.SimpleBlobDetector(参数)
其他:
检测器=cv2.SimpleBlobDetector_创建(参数)
#检测斑点。
关键点=检测器。检测(im)
#将检测到的斑点绘制为红色圆圈。
#cv2.DRAW\u匹配\u标志\u DRAW\u丰富\u关键点确保
#圆的大小对应于水滴的大小
带_keypoints的im_=cv2.drawKeypoints(im,keypoints,np.array([]),(0,0,255),
cv2.DRAW_匹配_标志_DRAW_丰富_关键点)
#显示斑点
cv2.imshow(“关键点”,带关键点的im)
cv2.等待键(0)
这是我收到的输出:
…和源图像:
代码取自
这是解释得很好,但我不知道在哪里“触摸”上面的脚本来调整和做我的要求
谢谢将关键点绘制为填充的白色圆圈:
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)
将关键点绘制为填充的白色圆圈:
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)
你也知道如何改变水滴的形状吗?例如,从实际的圆形到正方形。你也知道如何改变水滴的形状吗?例如,从实际的圆到平方。。