Blob是图像中的一组连接像素,它们共享一些共同特性(例如灰度值)。
在下图中,暗连接区域是斑点,斑点检测旨在识别和标记这些区域。
OpenCV提供了一种基于不同特征检测和过滤斑点的简单方法。
010_blob_detection是OpenCV通过设置blob参数过滤图像的示例程序。
C++应用Demo工程结构:
010_blob_detection/CPP$ tree . -L 1
.
├── blob.cpp
├── blob.jpg
└── CMakeLists.txt
0 directories, 3 files
确认OpenCV安装路径:
$ find /home/daniel/ -name "OpenCVConfig.cmake"
/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/
/home/daniel/OpenCV/opencv/build/OpenCVConfig.cmake
/home/daniel/OpenCV/opencv/build/unix-install/OpenCVConfig.cmake
$ export OpenCV_DIR=/home/daniel/OpenCV/installation/opencv-4.9.0/lib/cmake/opencv4/
C++应用Demo工程编译执行:
$ mkdir build
$ cd build
$ cmake ..
$ cmake --build . --config Release
$ cd ..
$ ./build/blob
Python应用Demo工程结构:
010_blob_detection/Python$ tree . -L 1
.
├── blob.jpg
└── blob.py
0 directories, 2 files
Python应用Demo工程执行:
$ workoncv-4.9.0
$ python blob.py
下面是通过params过滤以后,显示blob的代码:
C++:
// Storage for blobs
vector keypoints;
#if CV_MAJOR_VERSION < 3 // If you are using OpenCV 2
// Set up detector with params
SimpleBlobDetector detector(params);
// Detect blobs
detector.detect( im, keypoints);
#else
// Set up detector with params
Ptr detector = SimpleBlobDetector::create(params);
// Detect blobs
detector->detect( im, keypoints);
#endif
// Draw detected blobs as red circles.
// DrawMatchesFlags::DRAW_RICH_KEYPOINTS flag ensures
// the size of the circle corresponds to the size of blob
Mat im_with_keypoints;
drawKeypoints( im, keypoints, im_with_keypoints, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS );
// Show blobs
imshow("keypoints", im_with_keypoints );
Python:
# Create a detector with the parameters
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3 :
detector = cv2.SimpleBlobDetector(params)
else :
detector = cv2.SimpleBlobDetector_create(params)
# Detect blobs.
keypoints = detector.detect(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.array([]), (0,0,255), cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# Show blobs
cv2.imshow("Keypoints", im_with_keypoints)
采样值阈值过滤,在[minThreshold, maxThreshold]之间采用thresholdStep步进方式过滤。
C++:
// Change thresholds
params.minThreshold = 10;
params.maxThreshold = 200;
Python:
# Change thresholds
params.minThreshold = 10
params.maxThreshold = 200
面积过滤,小于当前面积时,被过滤掉。
C++:
// Filter by Area.
params.filterByArea = true;
params.minArea = 1500;
Python:
# Filter by Area.
params.filterByArea = True
params.minArea = 1500
圆形度过滤,低于阈值被过滤掉(越接近圆的时候,为1)。
C++:
// Filter by Circularity
params.filterByCircularity = true;
params.minCircularity = 0.1;
Python:
# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0.1
blob面积和凸包的面积之比(不凹陷的图形该值为1),因此,凸包越厉害越接近0。
C++:
// Filter by Convexity
params.filterByConvexity = true;
params.minConvexity = 0.87;
Python:
# Filter by Convexity
params.filterByConvexity = True
params.minConvexity = 0.87
惯性率,通常圆(1),椭圆(0, 1),过滤惯性率小于该值的物体。
C++:
// Filter by Inertia
params.filterByInertia = true;
params.minInertiaRatio = 0.01;
Python:
# Filter by Inertia
params.filterByInertia = True
params.minInertiaRatio = 0.01
本文通过对图像进行SimpleBlobDetector操作,从而对blob物体进行过滤,主要目的是理解该对象(SimpleBlobDetector)参数的含义。
- images Image set.
- keypoints The detected keypoints. In the second variant of the method keypoints[i] is a set of keypoints detected in images[i] .
【1】ubuntu22.04@laptop OpenCV Get Started
【2】ubuntu22.04@laptop OpenCV安装
【3】ubuntu22.04@laptop OpenCV定制化安装
学习是一种过程,对于前面章节学习讨论过的,就不在文中重复了。
有兴趣了解更多的朋友,请从《ubuntu22.04@laptop OpenCV Get Started》开始,一个章节一个章节的了解,循序渐进。