ubuntu22.04@laptop OpenCV Get Started: 010_blob_detection

2024-02-16 07:28

本文主要是介绍ubuntu22.04@laptop OpenCV Get Started: 010_blob_detection,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

ubuntu22.04@laptop OpenCV Get Started: 010_blob_detection

  • 1. 源由
  • 2. blob应用Demo
    • 2.1 C++应用Demo
    • 2.2 Python应用Demo
  • 3. 重点分析
    • 3.1 Threshold
    • 3.2 Area
    • 3.3 Circularity
    • 3.4 Convexity
    • 3.5 Inertia Ratio
  • 4. 总结
  • 5. 参考资料
  • 6. 补充

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.txt0 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.py0 directories, 2 files

Python应用Demo工程执行:

$ workoncv-4.9.0
$ python blob.py

3. 重点分析

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

C++:

	// Storage for blobsvector<KeyPoint> keypoints;#if CV_MAJOR_VERSION < 3   // If you are using OpenCV 2// Set up detector with paramsSimpleBlobDetector detector(params);// Detect blobsdetector.detect( im, keypoints);
#else // Set up detector with paramsPtr<SimpleBlobDetector> detector = SimpleBlobDetector::create(params);   // Detect blobsdetector->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 blobMat im_with_keypoints;drawKeypoints( im, keypoints, im_with_keypoints, Scalar(0,0,255), DrawMatchesFlags::DRAW_RICH_KEYPOINTS );// Show blobsimshow("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 blobim_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)参数的含义。

  • detect(images,keypoints))
  • 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》开始,一个章节一个章节的了解,循序渐进。

这篇关于ubuntu22.04@laptop OpenCV Get Started: 010_blob_detection的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/713891

相关文章

解决Failed to get nested archive for entry BOOT-INF/lib/xxx.jar问题

《解决FailedtogetnestedarchiveforentryBOOT-INF/lib/xxx.jar问题》解决BOOT-INF/lib/xxx.jar替换异常需确保路径正确:解... 目录Failed to get nested archive for entry BOOT-INF/lib/xxx

OpenCV在Java中的完整集成指南分享

《OpenCV在Java中的完整集成指南分享》本文详解了在Java中集成OpenCV的方法,涵盖jar包导入、dll配置、JNI路径设置及跨平台兼容性处理,提供了图像处理、特征检测、实时视频分析等应用... 目录1. OpenCV简介与应用领域1.1 OpenCV的诞生与发展1.2 OpenCV的应用领域2

在Java中使用OpenCV实践

《在Java中使用OpenCV实践》用户分享了在Java项目中集成OpenCV4.10.0的实践经验,涵盖库简介、Windows安装、依赖配置及灰度图测试,强调其在图像处理领域的多功能性,并计划后续探... 目录前言一 、OpenCV1.简介2.下载与安装3.目录说明二、在Java项目中使用三 、测试1.测

Python使用OpenCV实现获取视频时长的小工具

《Python使用OpenCV实现获取视频时长的小工具》在处理视频数据时,获取视频的时长是一项常见且基础的需求,本文将详细介绍如何使用Python和OpenCV获取视频时长,并对每一行代码进行深入解析... 目录一、代码实现二、代码解析1. 导入 OpenCV 库2. 定义获取视频时长的函数3. 打开视频文

Python get()函数用法案例详解

《Pythonget()函数用法案例详解》在Python中,get()是字典(dict)类型的内置方法,用于安全地获取字典中指定键对应的值,它的核心作用是避免因访问不存在的键而引发KeyError错... 目录简介基本语法一、用法二、案例:安全访问未知键三、案例:配置参数默认值简介python是一种高级编

Python如何将OpenCV摄像头视频流通过浏览器播放

《Python如何将OpenCV摄像头视频流通过浏览器播放》:本文主要介绍Python如何将OpenCV摄像头视频流通过浏览器播放的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完... 目录方法1:使用Flask + MJPEG流实现代码使用方法优点缺点方法2:使用WebSocket传输视

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

Python中OpenCV与Matplotlib的图像操作入门指南

《Python中OpenCV与Matplotlib的图像操作入门指南》:本文主要介绍Python中OpenCV与Matplotlib的图像操作指南,本文通过实例代码给大家介绍的非常详细,对大家的学... 目录一、环境准备二、图像的基本操作1. 图像读取、显示与保存 使用OpenCV操作2. 像素级操作3.

C/C++中OpenCV 矩阵运算的实现

《C/C++中OpenCV矩阵运算的实现》本文主要介绍了C/C++中OpenCV矩阵运算的实现,包括基本算术运算(标量与矩阵)、矩阵乘法、转置、逆矩阵、行列式、迹、范数等操作,感兴趣的可以了解一下... 目录矩阵的创建与初始化创建矩阵访问矩阵元素基本的算术运算 ➕➖✖️➗矩阵与标量运算矩阵与矩阵运算 (逐元