A blob is an area in an image that has similar characteristics. For example, in the image below, a figure such as a circle can be said to be a blob.
OpenCV has a built-in function that can automatically detect blobs, making it easy to find.
I want to find a round shape and count it.
blob.py
import cv2
import numpy as np
#Loading images
image = cv2.imread('shapes.jpg', 0)
#Parameter initialization
params = cv2.SimpleBlobDetector_Params()
#Blob area (minArea<= blob < maxArea)
params.filterByArea = True
params.minArea = 100
#Roundness (4 ∗ π ∗ Area)/perimeter ∗ defined by perimeter)
#(minCircularity <= blob < maxCircularity)
params.filterByCircularity = True
params.minCircularity = 0.85
#Convex surface information (minConvexity)<= blob < maxConvexity)
params.filterByConvexity = True
params.minConvexity = 0.1
#Represents an ellipse (minInertiaRatio)<= blob < maxInertiaRatio)
params.filterByInertia = True
params.minInertiaRatio = 0.1
#Detector creation
detector = cv2.SimpleBlobDetector_create(params)
#Blob detection
keypoints = detector.detect(image)
#Circle the blob in red
blank = np.zeros((1, 1))
blobs = cv2.drawKeypoints(image, keypoints, blank, (0, 0, 255),
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
#Number of blobs
count = len(keypoints)
print(f'Number of circles: {count}')
#Display image
cv2.imshow("out.jpg ", blobs)
cv2.waitKey(0)
cv2.destroyAllWindows()
>python blob.py
Number of circles: 8
When I tried the code posted on another site, Python crashed.
When I looked it up, it seems that the version of OpenCV had an effect. In previous versions of OpenCV, instead of SimpleBlobDetector_create (), a function called SimpleBlobDetector () was used, and I think that the flow of code execution with copy and paste → refer to the function that is not currently used → crash occurred. I will. As a workaround, you can avoid the error by using SimpleBlobDetector_create () or rewriting the code below.
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3:
detector = cv2.SimpleBlobDetector(params)
else:
detector = cv2.SimpleBlobDetector_create(params)
Application examples of blob detection include counting the number and detecting the position. It's easy to use with OpenCV, so I'd like to try other than this example.
Thank you for watching until the end. If you have any comments or suggestions, please do not hesitate to contact us.
https://www.programcreek.com/python/example/89350/cv2.SimpleBlobDetector https://www.visco-tech.com/technical/direction-presence/blob/
Recommended Posts