Blob 是什么
一个Blob是在一张图片中共享一些诸如灰度值等属性的一组连接的像素点。在上图中,黑色的连接区域就是blob,blob detection的目标就是识别并标记这些区域。
Blob detection 工作原理
SimpleBlobDetector, 是非常简单的模块,算法使用如下参数控制,且执行之后的流程:
- Thresholding: 将源图转为一些二值图,使用从minThreshold开始的阈值. 阈值从minThreshold开始,使用thresholdStep为梯度增加.
- Grouping: 在每个二值图中,相邻的白色像素点是被group在一起的,称之为binary blobs。
- Merging: 计算二值图片中binary blobs的中心点,blobs之间的距离比minDistBetweenBlobs更小的会被合并。
- Center & Radius Calculation: 计算并返回新合并的blobs的中心和半径。
利用color, size, shape 过滤 Blobs
-
By Color : [ Note : This feature appears to be broken. I checked the code, and it appears to have a logical error ] First you need to set filterByColor = 1. Set blobColor = 0 to select darker blobs, and blobColor = 255 for lighter blobs.
-
By Size : You can filter the blobs based on size by setting the parameters filterByArea = 1, and appropriate values for minArea and maxArea. E.g. setting minArea = 100 will filter out all the blobs that have less then 100 pixels.
-
By Shape : Now shape has three different parameters.
Circularity
This just measures how close to a circle the blob is. E.g. a regular hexagon has higher circularity than say a square. To filter by circularity, set filterByCircularity = 1. Then set 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.
Convexity
A picture is worth a thousand words. Convexity is defined as the (Area of the Blob / Area of it’s convex hull). Now, Convex Hull of a shape is the tightest convex shape that completely encloses the shape. To filter by convexity, set filterByConvexity = 1, followed by setting 0 ≤ minConvexity ≤ 1 and maxConvexity ( ≤ 1)
Inertia Ratio¶
Don’t let this scare you. Mathematicians often use confusing words to describe something very simple. All you have to know is that this measures how elongated a shape is. E.g. for a circle, this value is 1, for an ellipse it is between 0 and 1, and for a line it is 0. To filter by inertia ratio, set filterByInertia = 1, and set 0 ≤ minInertiaRatio ≤ 1 and maxInertiaRatio (≤ 1 ) appropriately.
下例将展示各个参数对标准图块的选取影响:
import env from mvlib import * from mvlib.application import *
# Read image im = read_image("blob.jpg") im_gray = gray(im) dev_imshow(im_gray)
blob_detect_params = create_blob_detector_params(10, 200, minArea=1500, minCircularity=0.1, minConvexity=0.87, minInertiaRatio=0.01) detector = create_blob_detector(blob_detect_params) # Detect blobs. keypoints = detector.detect(im_gray) # 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 = dev_draw_keypoints(im, keypoints) dev_imshow(im_with_keypoints)