1

I run a simple detector on an aerial image to detect blobs according to the parameters I set (i don't care for distractors for now).
Eventually, I need to display the coordinates (x,y) of the blobs on the image (at least the ones bigger than a certain area), or alternatively store them in a list.

Here's my code

# Standard imports
import cv2
import numpy as np;

# Read image
img = cv2.imread("image.jpg", cv2.IMREAD_GRAYSCALE)
# Taking a matrix of size 5 as the kernel 
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3))

# The first parameter is the original image, 
# kernel is the matrix with which image is  
# convolved and third parameter is the number  
# of iterations, which will determine how much  
# you want to erode/dilate a given image.  
img = cv2.equalizeHist(img)
img_dilation = cv2.erode(img, kernel, iterations=1) 


# Setup SimpleBlobDetector parameters.
params = cv2.SimpleBlobDetector_Params()

# Change thresholds
params.minThreshold = 40 #10
params.maxThreshold = 200 #200

# Filter by Area.
params.filterByArea = True # True
params.minArea = 16 # 3*3
params.maxArea = 121 #11*11 pixel

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


# Filter by Colr
params.filterByColor = True #True
params.blobColor = 0 #0.1

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

# Filter by Inertia
params.filterByInertia = True #True
params.minInertiaRatio = 0.25 #0.01

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


# 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
total_count = 0
for i in keypoints:
    total_count = total_count + 1



im_with_keypoints = cv2.drawKeypoints(img_dilation, keypoints, np.array([]), (255, 0, 0), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)

# Show blobs
cv2.imwrite("Keypoints.jpg", im_with_keypoints)
cv2.waitKey(0)

Any idea?

Tristo
  • 2,328
  • 4
  • 17
  • 28
  • Print len(keypoints) to see if you actually found any. Perhaps zero were detected by your params or your morphology cleaned them all. Check your morphology and equalize images to see that they look reasonable. – fmw42 Jun 05 '20 at 18:44
  • Yes, the counter return thousands of keypoints. Actually I don't have any clue why the blob function pinpoint zone with literally no dark pixels. The morphology is OK, since i identify correctly my "target" blobs. But I got plenty of false positives. How can I print the coordinates within this snippet? – Andrea Rolli Jun 05 '20 at 19:23
  • I am not an expert on keypoints, but try simply print(keypoint.pt). So in your for loop, add print(i.pt) or print(i.pt[0],i.pt[1]) – fmw42 Jun 05 '20 at 19:54
  • Thank you, it does work. But there's a way to "print" them on the image? – Andrea Rolli Jun 06 '20 at 16:58
  • Print the numbers or print a dot at those locations? If the latter, just draw a small circle centered at that location. See cv2.circle() at https://docs.opencv.org/4.1.1/d6/d6e/group__imgproc__draw.html#gaf10604b069374903dbd0f0488cb43670 – fmw42 Jun 06 '20 at 17:11
  • Sorry, i meant print the coordinates on the picture – Andrea Rolli Jun 06 '20 at 17:31
  • See cv2.putText() at https://docs.opencv.org/4.1.1/d6/d6e/group__imgproc__draw.html#ga5126f47f883d730f633d74f07456c576 – fmw42 Jun 06 '20 at 17:50

0 Answers0