Draw Color Circle on Grayscale Cv2
This tutorial explains elementary blob detection using OpenCV.
What is a Blob?
A Blob is a group of connected pixels in an prototype that share some mutual property ( Due east.thousand grayscale value ). In the prototype in a higher place, the dark connected regions are blobs, and the goal of blob detection is to identify and mark these regions.
SimpleBlobDetector Example
OpenCV provides a user-friendly way to find blobs and filter them based on different characteristics. Let'south start with the simplest example
This mail has been tested on OpenCV 4.ii.
Download Code To easily follow along this tutorial, please download code by clicking on the button below. It's FREE!
Python
# Standard imports import cv2 import numpy as np; # Read image im = cv2.imread("hulk.jpg", cv2.IMREAD_GRAYSCALE) # Set upwards the detector with default parameters. detector = cv2.SimpleBlobDetector() # Observe blobs. keypoints = detector.find(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.assortment([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS) # Show keypoints cv2.imshow("Keypoints", im_with_keypoints) cv2.waitKey(0) C++
using namespace cv; // Read image Mat im = imread( "blob.jpg", IMREAD_GRAYSCALE ); // Fix the detector with default parameters. SimpleBlobDetector detector; // Find blobs. std::vector<KeyPoint> keypoints; detector.detect( im, keypoints); // Describe detected blobs equally red circles. // DrawMatchesFlags::DRAW_RICH_KEYPOINTS flag ensures the size of the circle corresponds to the size of hulk Mat im_with_keypoints; drawKeypoints( im, keypoints, im_with_keypoints, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS ); // Bear witness blobs imshow("keypoints", im_with_keypoints ); waitKey(0); How does Blob detection work ?
SimpleBlobDetector, as the proper noun implies, is based on a rather uncomplicated algorithm described below. The algorithm is controlled by parameters ( shown in assuming below ) and has the following steps. Ringlet downwardly to know how the parameters are fix.
- Thresholding : Convert the source images to several binary images past thresholding the source image with thresholds starting atminThreshold. These thresholds are incremented pastthresholdStep untilmaxThreshold. So the starting time threshold isminThreshold,the second isminThreshold+thresholdStep,the tertiary isminThreshold+ 2 x thresholdStep, and so on.
- Grouping :In each binary image, connected white pixels are grouped together. Allow's call these binary blobs.
- Merging : The centers of the binary blobs in the binary images are computed, and blobs located closer thanminDistBetweenBlobsare merged.
- Center & Radius Calculation : The centers and radii of the new merged blobs are computed and returned.
I've partnered exclusively with OpenCV.org to bring you lot official courses in AI, Computer Vision, and Deep Learning to take you lot on a structured path from first steps to mastery.
Filtering Blobs by Color, Size and Shape
The parameters for SimpleBlobDetector can exist set to filter the type of blobs we desire.
1. By Color : [ Note : This feature appears to be broken. I checked the code, and it appears to accept a logical error ]
First y'all need to fixfilterByColor = 1. SetblobColor = 0 to select darker blobs, andblobColor = 255 for lighter blobs.By Size : You can filter the blobs based on size by setting the parameters filterByArea = 1, and appropriate values forminArea and maxArea. Eastward.chiliad. settingminArea = 100 will filter out all the blobs that have less and then 100 pixels.Past Shape :At present shape has iii different parameters.
ii. Circularity :
This just measures how close to a circle the blob is. E.g. a regular hexagon has college circularity than say a square. To filter past circularity, set filterByCircularity = 1. And then ready appropriate values for minCircularity and maxCircularity. Circularity is defined as
This means that a circle has a circularity of 1, circularity of a square is 0.785, and so on.
three. Convexity :
A flick is worth a grand words. Convexity is defined as the (Surface area of the Blob / Expanse of it's convex hull). At present, Convex Hull of a shape is the tightest convex shape that completely encloses the shape. To filter past convexity, gear upfilterByConvexity= 1, followed past setting 0 ≤minConvexity ≤ 1and maxConvexity ( ≤ one)
4. Inertia Ratio :
Don't let this scare you. Mathematicians oft apply confusing words to draw something very simple. All you take to know is that this measures how elongated a shape is. E.g. for a circle, this value is ane, for an ellipse information technology is betwixt 0 and 1, and for a line information technology is 0. To filter by inertia ratio, setfilterByInertia = one,and fix 0 ≤minInertiaRatio≤ 1andmaxInertiaRatio (≤ i )accordingly.
How to fix SimpleBlobDetector params ?
Setting parameters for SimpleBlobDetector is easy. Hither is an example
Python
# Setup SimpleBlobDetector parameters. params = cv2.SimpleBlobDetector_Params() # Alter thresholds params.minThreshold = 10; params.maxThreshold = 200; # Filter by Area. params.filterByArea = True params.minArea = 1500 # Filter past Circularity params.filterByCircularity = True params.minCircularity = 0.i # Filter by Convexity params.filterByConvexity = True params.minConvexity = 0.87 # Filter past Inertia params.filterByInertia = True params.minInertiaRatio = 0.01 # Create a detector with the parameters ver = (cv2.__version__).split('.') if int(ver[0]) < three : detector = cv2.SimpleBlobDetector(params) else : detector = cv2.SimpleBlobDetector_create(params) C++
Setting of params for SimpleBlobDetector in OpenCV two is slightly different from OpenCV three. In the code below we use the macro CV_MAJOR_VERSION to detect the version of OpenCV. In OpenCV 3, the SimpleBlobDetector::create method is used to create a smart pointer. The usage is shown in the lawmaking beneath.
// Setup SimpleBlobDetector parameters. SimpleBlobDetector::Params params; // Alter thresholds params.minThreshold = 10; params.maxThreshold = 200; // Filter by Area. params.filterByArea = true; params.minArea = 1500; // Filter by Circularity params.filterByCircularity = truthful; params.minCircularity = 0.ane; // Filter by Convexity params.filterByConvexity = true; params.minConvexity = 0.87; // Filter by Inertia params.filterByInertia = true; params.minInertiaRatio = 0.01; #if CV_MAJOR_VERSION < 3 // If yous are using OpenCV 2 // Fix detector with params SimpleBlobDetector detector(params); // Y'all tin can use the detector this way // detector.discover( im, keypoints); #else // Set up detector with params Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params); // SimpleBlobDetector::create creates a smart pointer. // And then you need to use pointer ( ->) instead of dot ( . ) // detector->detect( im, keypoints);
0 Response to "Draw Color Circle on Grayscale Cv2"
Post a Comment