轮廓检测

对于查找轮廓我们一般要对图像Canny检测。但是对于很特殊的场合其实我们还可以直接对二值化的图像进行轮廓的提取。

关键函数

1、 cvFindContours

Retrieves contours from the binary image and returns the number of retrieved contours. The pointer firstContour is filled by the function. It will contain pointer to the first most outer contour or IntPtr.Zero if no contours is detected (if the image is completely black). Other contours may be reached from firstContour using h_next and v_next links. The sample in cvDrawContours discussion shows how to use contours for connected component detection. Contours can be also used for shape analysis and object recognition - see squares.c in OpenCV sample directory The function modifies the source image? content

函数功能:对图像进行轮廓检测,这个函数将生成一条链表以保存检测出的各个轮廓信息,并传出指向这条链表表头的指针。

函数原型:

Namespace: Emgu.CV
Assembly: Emgu.CV (in Emgu.CV.dll) Version: 2.4.2.1777 (2.4.2.1777)

C#
  1. public static int cvFindContours(
  2. IntPtr image,
  3. IntPtr storage,
  4. ref IntPtr firstContour,
  5. int headerSize,
  6. RETR_TYPE mode,
  7. CHAIN_APPROX_METHOD method,
  8. Point offset
  9. )

Parameters

image
Type: System.IntPtr

The source 8-bit single channel image. Non-zero pixels are treated as 1s, zero pixels remain 0s - that is image treated as binary. To get such a binary image from grayscale, one may use cvThreshold, cvAdaptiveThreshold or cvCanny. The function modifies the source image content

第一个参数表示输入图像,必须为一个8位的二值图像。

storage
Type: System.IntPtr
Container of the retrieved contours
第二参数表示存储轮廓的容器。为CvMemStorage类型,定义在OpenCV的\core\types_c.h中。
firstContour
Type: System.IntPtr
Output parameter, will contain the pointer to the first outer contour
第三个参数为输出参数,这个参数将指向用来存储轮廓信息的链表表头。
headerSize
Type: System.Int32
Size of the sequence header, >=sizeof(CvChain) if method=CV_CHAIN_CODE, and >=sizeof(CvContour) otherwise
第四个参数表示存储轮廓链表的表头大小,当第六个参数传入CV_CHAIN_CODE时,要设置成izeof(CvChain),其它情况统一设置成sizeof(CvContour)。
mode
Type: Emgu.CV.CvEnum.RETR_TYPE
Retrieval mode
第五个参数为轮廓检测的模式,有如下取值:CV_RETR_EXTERNAL:只检索最外面的轮廓;  CV_RETR_LIST:检索所有的轮廓,并将其保存到一条链表当中;  CV_RETR_CCOMP:检索所有的轮廓,并将他们组织为两层:顶层是各部分的外部边界,第二层是空洞的边界;CV_RETR_TREE:检索所有的轮廓,并重构嵌套轮廓的整个层次,可以参见下图。 
method
Type: Emgu.CV.CvEnum.CHAIN_APPROX_METHOD
Approximation method (for all the modes, except CV_RETR_RUNS, which uses built-in approximation).

第六个参数用来表示轮廓边缘的近似方法的,常用值如下所示:

CV_CHAIN_CODE:以Freeman链码的方式输出轮廓,所有其他方法输出多边形(顶点的序列)。

    CV_CHAIN_APPROX_SIMPLE:压缩水平的、垂直的和斜的部分,也就是,函数只保留他们的终点部分。

offset
Type: System.Drawing.Point
Offset, by which every contour point is shifted. This is useful if the contours are extracted from the image ROI and then they should be analyzed in the whole image context
第七个参数表示偏移量,比如你要从图像的(100, 0)开始进行轮廓检测,那么就传入(100, 0)。

Return Value

The number of countours

轮廓线的数量 
2、cvDrawContours
使用cvFindContours函数能检测出图像的轮廓,将轮廓绘制出来则需要另一函数——cvDrawContours来配合了。下面介绍cvDrawContours函数。
Draws contour outlines in the image if thickness >=0 or fills area bounded by the contours if thickness<0.

Namespace: Emgu.CV
Assembly: Emgu.CV (in Emgu.CV.dll) Version: 2.4.2.1777 (2.4.2.1777)

函数功能:在图像上绘制外部和内部轮廓

void cvDrawContours(

CvArr *img,

CvSeq* contour,

CvScalar external_color,

CvScalar hole_color,

int max_level,

int thickness=1,

int line_type=8,

CvPoint offset=cvPoint(0,0)

);

Parameters

img
Type: System.IntPtr
Image where the contours are to be drawn. Like in any other drawing function, the contours are clipped with the ROI
第一个参数表示输入图像,函数将在这张图像上绘制轮廓。
contour
Type: System.IntPtr
Pointer to the first contour
第二个参数表示指向轮廓链表的指针。
externalColor
Type: Emgu.CV.Structure.MCvScalar
Color of the external contours
holeColor
Type: Emgu.CV.Structure.MCvScalar
Color of internal contours
第三个参数和第四个参数表示颜色,绘制时会根据轮廓的层次来交替使用这二种颜色。
maxLevel
Type: System.Int32
Maximal level for drawn contours. If 0, only contour is drawn. If 1, the contour and all contours after it on the same level are drawn. If 2, all contours after and all contours one level below the contours are drawn, etc. If the value is negative, the function does not draw the contours following after contour but draws child contours of contour up to abs(maxLevel)-1 level.
第五个参数表示绘制轮廓的最大层数,如果是0,只绘制contour;如果是1,追加绘制和contour同层的所有轮廓;如果是2,追加绘制比contour低一层的轮廓,以此类推;如果值是负值,则函数并不绘制contour后的轮廓,但是将画出其子轮廓,一直到abs(max_level) - 1层。
thickness
Type: System.Int32
Thickness of lines the contours are drawn with. If it is negative the contour interiors are drawn
第六个参数表示轮廓线的宽度,如果为CV_FILLED则会填充轮廓内部。
lineType
Type: Emgu.CV.CvEnum.LINE_TYPE
Type of the contour segments
第七个参数表示轮廓线的类型。
offset
Type: System.Drawing.Point
Shift all the point coordinates by the specified value. It is useful in case if the contours retrived in some image ROI and then the ROI offset needs to be taken into account during the rendering.
第八个参数表示偏移量,如果传入(10,20),那绘制将从图像的(10,20)处开始。
 

[转载+原创]Emgu CV on C# (七) —— Emgu CV on 轮廓检测的更多相关文章

  1. [转载+原创]Emgu CV on C# (三) —— Emgu CV on 均衡化

    本文简要描述了均衡化原理及数学实现等理论问题,最终利用emgucv实现图像的灰度均衡. 直方图的均衡化,这是图像增强的常用方法. 一.均衡化原理及数学实现(转载) 均衡化原理及数学实现可重点参看——& ...

  2. [转载+原创]Emgu CV on C# (五) —— Emgu CV on 局部自适应阈值二值化

    局部自适应阈值二值化 相对全局阈值二值化,自然就有局部自适应阈值二值化,本文利用Emgu CV实现局部自适应阈值二值化算法,并通过调节block大小,实现图像的边缘检测. 一.理论概述(转载自< ...

  3. [转载+原创]Emgu CV on C# (二) —— Emgu CV on 灰度化

    本文主要对彩色图片灰度化的方法及其实现过程进行总结,最终给出Emgu CV实现的代码. 一.灰度化原理及数学实现(转载自——<图像灰度化方法总结及其VC实现> 该篇文章使用opencv实现 ...

  4. [转载+原创]Emgu CV on C# (一) —— Emgu CV on Visual C# 2010

    2014-08-16 最近要进行图像识别,准备利用几天的时间研究一下Emgu CV,花了一晚上功夫进行调试环境安装,期间遇到了不少问题,现梳理一下安装过程和调试过程中出现的问题. 中间有转载别人的部分 ...

  5. [转载+原创]Emgu CV on C# (六) —— Emgu CV on Canny边缘检测

    Canny边缘检测也是一种边缘检测方法,本文介绍了Canny边缘检测的函数及其使用方法,并利用emgucv方法将轮廓检测解算的结果与原文进行比较. 图像的边缘检测的原理是检测出图像中所有灰度值变化较大 ...

  6. 【OpenCV学习笔记】三十、轮廓特征属性及应用(七)—位置关系及轮廓匹配

    http://blog.csdn.net/abc8730866/article/details/69219992 轮廓特征属性及应用(七)—位置关系及轮廓匹配 1.计算点与轮廓的距离及位置关系——po ...

  7. 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数003·contour,轮廓处理

    <zw版·Halcon-delphi系列原创教程> Halcon分类函数003·contour,轮廓处理 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“**”,替 ...

  8. 【opencv】cv::Mat转std::vector<cv::Point2d> (注意两容器中数据类型的一致性)

    获取cv::Mat大小: mymat.size() 获取cv::Mat指定位置的值:需指定数据类型,且注意数据类型应与存入时的数据类型一致,否则会导致不抛出异常的数据错误 mymat.at<,i ...

  9. [转载+原创]Emgu CV on C# (四) —— Emgu CV on 全局固定阈值二值化

    重点介绍了全局二值化原理及数学实现,并利用emgucv方法编程实现. 一.理论概述(转载,如果懂图像处理,可以略过,仅用作科普,或者写文章凑字数)  1.概述 图像二值化是图像处理中的一项基本技术,也 ...

随机推荐

  1. 【转载】Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)

    http://blog.csdn.net/congcong68/article/details/41113239 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及 ...

  2. 发布ASP.NET网站到IIS

    1.         在Web项目中点击发布网站,如图1所示 图1 2.         选择要发布的路径——>“确定”,如果项目显示发布成功就可以了.如图2所示 图2 3.         打 ...

  3. 番外篇 之 C#委托

    对于上一节 番外篇之C#多线程的反思 反思一:   Thread th = new Thread(参数); ////参数的总结 ////首先,第一情况,对于 Thread th = new Threa ...

  4. Javascript获取URL地址变量参数值的方法

    今天碰到在做一个动态页面的时候,需要用到 URL 的参数值来作判断,从而决定某一块内容在当前页面是否显示.例如exampe.html?parm1=xxx&parm2=xxx&parm3 ...

  5. easyui中Tab的tools按钮刷新当前tab

    easyui中Tab的tools按钮刷新当前tab 点击刷新按钮,刷新当前Tab选项卡. $('#index_tabs').tabs({ fit : true, border : false, too ...

  6. WCF之消息模式

    请求/响应:所有操作的默认行为,在WSDL中表现为Input/Output元素. One_Way. 在WSDL中只有Input,没有回应(Output),所以没有异常报告. 单向操作只会在发出调用的瞬 ...

  7. 延迟加载(Lazy Load)

    一个对象,它虽然不包含所需要的所有数据,但是它知道怎么获取这些数据 设计专门的对象来把数据从DB中加载到内存中. 该对象可以完成在加载所需对象的同时,把与之相关的对象也一并加载了. 否则,必须显示加载 ...

  8. 向CodeBlocks的Project中添加calss文件时,出现No such file or directory错误的解决方案

    我们在CodeBlocks中编写程序时,一般要建立工程.现在建立工程first,然后建立类文件Person,并将其添加到first中, int main() { Person p; p.display ...

  9. 对 Sea.js 进行配置(一) seajs.config

    可以对 Sea.js 进行配置,让模块编写.开发调试更方便. seajs.config seajs.config(options) 用来进行配置的方法. seajs.config({ // 别名配置 ...

  10. WEB-INF简介

    WEB-INF简介 WEB-INF是Java的WEB应用的安全目录.所谓安全就是客户端无法访问,只有服务端可以访问的目录. 如果想在页面中直接访问其中的文件,必须通过web.xml文件对要访问的文件进 ...