轮廓检测

对于查找轮廓我们一般要对图像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#
public static int cvFindContours(
IntPtr image,
IntPtr storage,
ref IntPtr firstContour,
int headerSize,
RETR_TYPE mode,
CHAIN_APPROX_METHOD method,
Point offset
)

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. JS辨别浏览器系统IOS或安卓

    详细内容请点击 /* * 智能机浏览器版本信息: * */ (function($,window,document){     $.extend({         browser:{         ...

  2. 为什么用服务不用线程-Android

    1.什么是Service? A Service is an application component representing either an application's desire to p ...

  3. Part 8 AngularJS filters

    Filters in angular can do 3 different things 1. Format data 2. Sort data 3. Filter data Filters can ...

  4. Part 67 to 70 Talking about method parameters in C#

    Part 67 Optional parameters in c# Part 68  Making method parameters optional using method overloadin ...

  5. Cocos2d-JS中的精灵菜单和图片菜单

    精灵菜单的菜单项类是cc.MenuItemSprite,图片菜单的菜单项类是cc.MenuItemImage.由于cc.MenuItemImage继承于cc.MenuItemSprite,所以图片菜单 ...

  6. MVC项目页面获取控制器的信息

    页面获取控制器的名字: @{ if (ViewContext.RouteData.Values["controller"].ToString()=="Home" ...

  7. Python 字典(Dictionary) setdefault()方法

    描述 Python 字典(Dictionary) setdefault() 函数和get()方法类似, 如果键不已经存在于字典中,将会添加键并将值设为默认值. 语法 setdefault()方法语法: ...

  8. JAVA:二进制(原码 反码 补码),位运算,移位运算,约瑟夫问题(5)

    一.二进制,位运算,移位运算 1.二进制 对于原码, 反码, 补码而言, 需要注意以下几点: (1).Java中没有无符号数, 换言之, Java中的数都是有符号的; (2).二进制的最高位是符号位, ...

  9. 《通过脚本查看哪些ip被占用》shell笔记

    改脚本查看哪些ip被占用. #!/bin/bash for i in {1..10}   //赋予i变量1-10 do   //干什么 ping -c1 -w1 192.168.7.$i && ...

  10. 转:关于Apache与Nginx的优势比较(经典)

    不断有人跟我说Nginx比Apache好.比Apache快之类.Nginx更主要是作为反向代理,而非Web服务器使用.我翻译过一本关于反向代理的技术书籍,同时精通Apache API开发,对Nginx ...