填充检测到的斑点(OpenCV,Python) [英] Fill detected blobs (OpenCV,Python)

查看:637
本文介绍了填充检测到的斑点(OpenCV,Python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to fill the detected blobs in my images with white color. This is the code I am using:

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

这是我收到的输出:

...以及源图像:

该代码摘自此处 解释得很好,但是我不知道在哪里可以触摸"上面的脚本来进行调整和执行我所要求的操作.

The code has been taken from here It is explained very well but I don't know where to "touch" the above script to tweak and do what I am requesting.

谢谢

推荐答案

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

Draw keypoints as filled white circles:

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 rectangle or square, then:

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)

这篇关于填充检测到的斑点(OpenCV,Python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆