本教程解释了使用OpenCV进行简单的blob检测。
; 1.Blob是什么?
Blob是图像中共享某些共同属性(例如灰度值)的一组连接的像素。在上图中,暗连通区域是Blob,Blob检测的目的就是识别和标记这些区域。
2.SimpleBlobDetector例子
OpenCV提供了一种方便的方法来检测Blob,并根据不同的属性对其进行过滤。让我们从最简单的例子开始。
(1)Python
import cv2
import numpy as np;
im = cv2.imread("blob.png", cv2.IMREAD_GRAYSCALE)
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3:
detector = cv2.SimpleBlobDetector()
else:
detector = cv2.SimpleBlobDetector_create()
keypoints = detector.detect(im)
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow("Keypoints", im_with_keypoints)
cv2.waitKey(0)
(2)C++
#include
#include
using namespace std;
using namespace cv;
Mat im = imread( "blob.png", IMREAD_GRAYSCALE );
#if CV_MAJOR_VERSION < 3
SimpleBlobDetector detector();
detector.detect( im, keypoints);
#else
Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create();
detector->detect( im, keypoints);
#endif
Mat im_with_keypoints;
drawKeypoints( im, keypoints, im_with_keypoints, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS );
imshow("keypoints", im_with_keypoints );
waitKey(0);
3.Blob检测是如何工作的
SimpleBlobDetector
,顾名思义,基于下面描述的一个相当简单的算法。该算法由参数控制(如下粗体所示),并具有以下步骤。下面了解如何设置参数。
Thresholding
:通过对源图像进行阈值化,将源图像转换为若干二值图像,阈值从minThreshold
开始。这些阈值通过thresholdStep
递增,直到maxThreshold
。所以第一个阈值是minThreshold
,第二个是minThreshold + thresholdStep
,第三个是minThreshold + 2 x thresholdStep
,以此类推。Grouping
:在每个二值图像中,连接的白色像素被组合在一起。我们称这些为二值blobs。Merging
:计算二值图像中二值blobs的中心,并合并距离小于minDistBetweenBlobs
的blobs。Center & Radius Calculation
:计算并返回新合并的blobs的中心和半径。
4.通过Color, Size 和 Shape过滤Blobs
可以设置 SimpleBlobDetector
的参数来过滤我们想要的blob类型。
- 1.按
Color
:首先,您需要设置filterByColor = 1
。设置blobColor = 0
来选择较暗的Blobs,设置blobColor = 255
来选择较亮的Blobs。按大小:可以根据大小过滤Blobs,方法是设置参数filterByArea = 1
,以及适当的minArea
和maxArea
值。例如,设置minArea = 100
将过滤掉所有像素个数小于100的Blobs。按Shape:现在Shape有三个不同的参数。 -
Circularity(圆度)
:这只是测量了这个blob与圆的距离。正六边形的圆度比正方形高。要根据圆度进行过滤,设置filterByCircularity = 1
。然后设置适当的minCircularity
和maxCircularity
值。圆度定义为4 π A r e a p e r i m e t e r 2 \frac{4\pi Area}{{perimeter}^2}p e r i m e t e r 2 4 πA r e a :这意味着圆的圆度为1,正方形的圆度为0.785,以此类推。
-
Convexity(凹凸性)
:一图胜千言。凸性定义为(Blob的面积/它的凸包的面积)。现在,凸包的形状是最紧的凸形状,完全包围了形状。设置filterByConvexity = 1
,然后设置0≤minConvexity≤1
和maxConvexity(≤1)
。
-
Inertia Ratio(惯性比)
:不要让这吓到你。数学家经常用令人困惑的词来描述一些非常简单的东西。你所需要知道的是,它衡量的是一个形状的伸长程度。例如,对于圆,这个值是1,对于椭圆,它在0和1之间,对于直线,它是0。根据惯性比进行滤波,设置filterByInertia = 1
,并适当设置0≤mininertiratio≤1
和maxinertiratio≤1
。
5.如何设置SimpleBlobDetector参数?
设置SimpleBlobDetector的参数很容易。下面是一个例子:
(1)Python
import cv2
import numpy as np;
im = cv2.imread("blob.png", cv2.IMREAD_GRAYSCALE)
params = cv2.SimpleBlobDetector_Params()
params.minThreshold = 10;
params.maxThreshold = 200;
params.filterByArea = True
params.minArea = 1500
params.filterByCircularity = True
params.minCircularity = 0.1
params.filterByConvexity = True
params.minConvexity = 0.87
params.filterByInertia = True
params.minInertiaRatio = 0.01
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3 :
detector = cv2.SimpleBlobDetector(params)
else :
detector = cv2.SimpleBlobDetector_create(params)
keypoints = detector.detect(im)
im_with_keypoints = cv2.drawKeypoints(im, keypoints, np.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
cv2.imshow("Keypoints", im_with_keypoints)
cv2.waitKey(0)
(2)C++
#include
#include
using namespace std;
using namespace cv;
Mat im = imread( "blob.png", IMREAD_GRAYSCALE );
SimpleBlobDetector::Params params;
params.minThreshold = 10;
params.maxThreshold = 200;
params.filterByArea = true;
params.minArea = 1500;
params.filterByCircularity = true;
params.minCircularity = 0.1;
params.filterByConvexity = true;
params.minConvexity = 0.87;
params.filterByInertia = true;
params.minInertiaRatio = 0.01;
#if CV_MAJOR_VERSION < 3
SimpleBlobDetector detector(params);
detector.detect( im, keypoints);
#else
Ptr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params);
detector->detect( im, keypoints);
#endif
Mat im_with_keypoints;
drawKeypoints( im, keypoints, im_with_keypoints, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS );
imshow("keypoints", im_with_keypoints );
waitKey(0);
参考目录
https://blog.csdn.net/dz4543/article/details/79897763
https://learnopencv.com/blob-detection-using-opencv-python-c/
Original: https://blog.csdn.net/weixin_43229348/article/details/120448954
Author: 求则得之,舍则失之
Title: OpenCV基础(10)使用OpenCV进行Blob检测
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/703799/
转载文章受原作者版权保护。转载请注明原作者出处!