OpenCV学习笔记(十五)形状检测

本文主要介绍了使用OpenCV进行形状检测中的轮廓检测技术,重点讲解了findContours函数的使用,包括参数模式、轮廓检索模式和轮廓近似方法,以及如何在图像中绘制轮廓。
摘要由CSDN通过智能技术生成

1 轮廓检测

图像目标中的形状检测是图像识别的重要的技术之一,对物体进行检测并提取,首先要做的就是提取物体的轮廓信息,然后在通过点集特征选择相应的算法进行处理,最后得到物体的形状信息。轮廓形状是我们看到物体最开始的印象,轮廓提取的原理是通过对图像进行二值化,利用边缘点连接的层次差别,提取位于数结构特征高的区域点集构成的集合,这部分最可能是物体的轮廓。

主要用到的函数findContours()对物体轮廓进行检测。

void findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point())

  • image – Source, an 8-bit single-channel image. Non-zero pixels are treated as 1’s. Zero pixels remain 0’s, so the image is treated as binary . You can use compare() , inRange() , threshold() , adaptiveThreshold() ,Canny() , and others to create a binary image out of a grayscale or color one. The function modifies the imagewhile extracting the contours.
  • contours – Detected contours. Each contour is stored as a vector of points.
  • hiararchy – Optional output vector containing information about the image topology. It has as many elements as the number of contours. For each contour contours[i] , the elements hierarchy[i][0] , hiearchy[i][1] ,hiearchy[i][2] , and hiearchy[i][3] are set to 0-based indices in contours of the next and previous contours at the same hierarchical level: the first child contourand the parent contour, respectively. If for a contour ithere are no next, previous, parent, or nested contours, the corresponding elements of hierarchy[i] will be negative.
  • mode –

    Contour retrieval mode.

    • CV_RETR_EXTERNAL retrieves only the extreme outer contours. It sets hierarchy[i][2]=hierarchy[i][3]=-1 for all the contours.
    • CV_RETR_LIST retrieves all of the contours without establishing any hierarchical relationships.
    • CV_RETR_CCOMP retrieves all of the contours and organizes them into a two-level hierarchy. At the top level, there are external boundaries of the components. At the second level, there are bound
OpenCV(Open Source Computer Vision Library)是一个开源计算机视觉库,主要用于实时图像处理、计算机视觉任务和机器学习应用。在C++中使用OpenCV需要注意以下几个关键点: 1. **安装**:首先需要从官网下载适合你的平台的版本,并按照文档安装。确保路径添加到系统环境变量中,以便程序能够找到库文件。 2. **基本结构**:OpenCV的主要数据结构包括Mat(矩阵),它是最基本的数据容器,用于存储图像和像素数据。还有其他如Point、Size、Rect等用于表示几何形状的对象。 3. **函数使用**:OpenCV提供大量图像处理和计算机视觉函数,例如读取和显示图片(imread、imshow)、图像操作(resize、crop)、颜色空间转换、边缘检测(Canny)、特征提取(SIFT、SURF)以及机器学习模块(比如Haar cascades用于人脸检测)。 4. **模板编程**:利用OpenCV提供的API,通常通过模板类和函数来编写代码,因为很多操作都是针对不同类型的图像矩阵。 5. **异常处理**:由于OpenCV函数可能会抛出错误,记得在关键部分使用try-catch块来捕获并处理异常。 6. **内存管理**:Mat对象在内部管理其内存,但建议手动释放不再使用的大型矩阵,避免内存泄漏。 7. **多线程支持**:OpenCV提供了线程安全的函数,如果你需要在多线程环境中工作,确保正确同步访问共享资源。 8. **实践项目**:熟悉OpenCV最好的方式就是实际参与一些项目,比如图像分类、物体追踪等。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值