ubuntu22.04@laptop OpenCV Get Started: 010_blob_detection

1. 源由

Blob是图像中的一组连接像素,它们共享一些共同特性(例如灰度值)。

在下图中,暗连接区域是斑点,斑点检测旨在识别和标记这些区域。

在这里插入图片描述

OpenCV提供了一种基于不同特征检测和过滤斑点的简单方法。

2. blob应用Demo

010_blob_detection是OpenCV通过设置blob参数过滤图像的示例程序。

2.1 C++应用Demo

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

2.2 Python应用Demo

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

3. 重点分析

下面是通过params过滤以后,显示blob的代码:

C++:

	// Storage for blobs
	vector<KeyPoint> 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<SimpleBlobDetector> 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)

3.1 Threshold

采样值阈值过滤,在[minThreshold, maxThreshold]之间采用thresholdStep步进方式过滤。

C++:

// Change thresholds
params.minThreshold = 10;
params.maxThreshold = 200;

Python:

# Change thresholds
params.minThreshold = 10
params.maxThreshold = 200

3.2 Area

面积过滤,小于当前面积时,被过滤掉。

C++:

// Filter by Area.
params.filterByArea = true;
params.minArea = 1500;

Python:

# Filter by Area.
params.filterByArea = True
params.minArea = 1500

3.3 Circularity

圆形度过滤,低于阈值被过滤掉(越接近圆的时候,为1)。

在这里插入图片描述

C++:

// Filter by Circularity
params.filterByCircularity = true;
params.minCircularity = 0.1;

Python:

# Filter by Circularity
params.filterByCircularity = True
params.minCircularity = 0.1

3.4 Convexity

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

3.5 Inertia Ratio

惯性率,通常圆(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

4. 总结

本文通过对图像进行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] .

5. 参考资料

【1】ubuntu22.04@laptop OpenCV Get Started
【2】ubuntu22.04@laptop OpenCV安装
【3】ubuntu22.04@laptop OpenCV定制化安装

6. 补充

学习是一种过程,对于前面章节学习讨论过的,就不在文中重复了。

有兴趣了解更多的朋友,请从《ubuntu22.04@laptop OpenCV Get Started》开始,一个章节一个章节的了解,循序渐进。

相关推荐

  1. Ubuntu2204配置samba

    2024-02-16 03:02:02       75 阅读
  2. Ubuntu 下 Docker安装 2024

    2024-02-16 03:02:02       28 阅读
  3. Ubuntu2204一句话下载VSCode

    2024-02-16 03:02:02       76 阅读
  4. Ubuntu2204安装小熊猫C++/DevCpp

    2024-02-16 03:02:02       75 阅读
  5. ubuntu2204,mysql8.x安装

    2024-02-16 03:02:02       59 阅读
  6. Ubuntu2204搭建ceph17

    2024-02-16 03:02:02       21 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-16 03:02:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-16 03:02:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-16 03:02:02       87 阅读
  4. Python语言-面向对象

    2024-02-16 03:02:02       96 阅读

热门阅读

  1. MySQL学习Day15——MySQL架构

    2024-02-16 03:02:02       49 阅读
  2. 奇异递归模板模式应用2-单例模板

    2024-02-16 03:02:02       58 阅读
  3. 2024/2/14

    2024-02-16 03:02:02       54 阅读
  4. Introduction to GraphQL-style APIs

    2024-02-16 03:02:02       46 阅读
  5. 在STM32微控制器中实现高速数据传输的DMA技巧

    2024-02-16 03:02:02       66 阅读
  6. 学习总结11

    2024-02-16 03:02:02       48 阅读
  7. 【30秒看懂大数据】数据标准

    2024-02-16 03:02:02       57 阅读
  8. 容器高级知识: 适配器模式与 Sidecar 模式的区别

    2024-02-16 03:02:02       52 阅读
  9. 算法刷题 DAY50

    2024-02-16 03:02:02       54 阅读