Execution example of blob detection using OpenCV

Introduction

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.

shapes.jpg

OpenCV has a built-in function that can automatically detect blobs, making it easy to find.

Thing you want to do

I want to find a round shape and count it.

code

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

result

>python blob.py
Number of circles: 8

out.jpg

Story that failed in execution

When I tried the code posted on another site, Python crashed.

キャプチャ.JPG

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)  

in conclusion

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.

Reference URL

https://www.programcreek.com/python/example/89350/cv2.SimpleBlobDetector https://www.visco-tech.com/technical/direction-presence/blob/

Recommended Posts

Execution example of blob detection using OpenCV
Example of using lambda
Feature detection using opencv (corner detection)
Vertical Tower of Pisa using OpenCV
Judgment of backlit image using OpenCV
[Python] Using OpenCV with Python (Edge Detection)
I tried using GrabCut of OpenCV
Example of using class variables and class methods
I tried object detection using Python and OpenCV
I tried using the image filter of OpenCV
Try projective transformation of images using OpenCV with Python
Reproduce the execution example of Chapter 4 of Hajipata in Python
Reproduce the execution example of Chapter 5 of Hajipata in Python
Measurement of execution time
Detection of ArUco markers
Sample program and execution example of ensemble learning (Stacked generalization)
Summary of execution of anonymous recursive function using fixed point combinator
Comparison of color detection methods in OpenCV inRange, numpy, cupy
python> Example of using strip ()> src = '0123456789ABCDEF' / wrk = src.strip ('DEF')
Anomaly detection by autoencoder using keras [Implementation example for beginners]