Reputation: 502
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)
This is the output I am receiving:
...and the source image:
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.
Thanks
Upvotes: 1
Views: 3903
Reputation: 6468
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)
Edit: 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)
Upvotes: 4