[转载+原创]Emgu CV on C# (七) —— Emgu CV on 轮廓检测

轮廓检测

对于查找轮廓我们一般要对图像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:检索所有的轮廓,并重构嵌套轮廓的整个层次,可以参见下图。 
[转载+原创]Emgu CV on C# (七) ——  Emgu CV on 轮廓检测
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)处开始。
 
上一篇:acdream1116 Gao the string!(hash二分 or 后缀数组)


下一篇:exec sp_spaceused如何只返回一个结果集(转载)