
本文记录了对OpenCV示例contours2.cpp的分析。
资料地址:http://docs.opencv.org/3.0.0/d0/d2a/contours2_8cpp-example.html
1.findContours函数轮廓检测函数原型:void findContours(InputOutputArray image, OutputArrayOfArrays contours, OutputArray hierarchy, int mode, int method, Point offset=Point());
CHAIN_APPROX_NONE存储所有的轮廓点,相邻的两个点的像素位置差不超过1,即max(abs(x1-x2),abs(y2-y1))==1
CHAIN_APPROX_SIMPLE压缩水平方向,垂直方向,对角线方向的元素,只保留该方向的终点坐标,例如一个矩形轮廓只需4个点来保存轮廓信息
CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS使用teh-Chinl chain 近似算法
offset:代表轮廓点的偏移量,可以设置为任意值。对ROI图像中找出的轮廓,并要在整个图像中进行分析时,这个参数还是很有用的。
函数原型:
参数说明:
InputArray curve:输入的点集
OutputArray approxCurve:输出的点集,当前点集是能最小包容指定点集的。draw出来即是一个多边形;
double epsilon:指定的精度,也即是原始曲线与近似曲线之间的最大距离。
bool closed:若为true,则说明近似曲线是闭合的,它的首位都是相连,反之,若为false,则断开。
drawContours
(InputOutputArray image, InputArrayOfArrays contours, int contourIdx, const Scalar& color, int thickness=1, int lineType=8, InputArray hierarchy=noArray(), int maxLevel=INT_MAX, Point offset=Point() )
maxLevel=0,绘制与输入轮廓属于同一等级的所有轮廓即输入轮廓和与其相邻的轮廓
maxLevel=1, 绘制与输入轮廓同一等级的所有轮廓与其子节点。
maxLevel=2,绘制与输入轮廓同一等级的所有轮廓与其子节点以及子节点的子节点
PS:findContours()运行的时候,这个图像会被直接涂改,因此如果是将来还有用的图像,应该复制之后再传给findContours()。
以上描述摘至参考资料5
trackbarname:滑动空间的名称;
winname:滑动空间用于依附的图像窗口的名称;
value:初始化阈值;
count:滑动控件的刻度范围;
TrackbarCallback是回调函数。


取消approxPolyDP调用
Example分析
1.申明需要使用的变量
for( int i = ; i < ; i++ )
{
int dx = (i%)* - ;
int dy = (i/)*;
const Scalar white = Scalar();
const Scalar black = Scalar();
if( i == )
{
for( int j = ; j <= ; j++ )
{
double angle = (j+)*CV_PI/;
line(img, Point(cvRound(dx++j*-*cos(angle)),
cvRound(dy+-*sin(angle))),
Point(cvRound(dx++j*-*cos(angle)),
cvRound(dy+-*sin(angle))), white, , , );
}
}
ellipse( img, Point(dx+, dy+), Size(,), , , , white, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , black, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , black, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , white, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , white, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , black, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , black, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , black, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , black, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , white, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , white, -, , );
}
2.3.创建显示源图像的窗口
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <math.h>
#include <iostream> using namespace cv;
using namespace std; static void help()
{
cout
<< "\nThis program illustrates the use of findContours and drawContours\n"
<< "The original image is put up along with the image of drawn contours\n"
<< "Usage:\n"
<< "./contours2\n"
<< "\nA trackbar is put up which controls the contour level from -3 to 3\n"
<< endl;
} const int w = ;
int levels = ; vector<vector<Point> > contours;
vector<Vec4i> hierarchy; static void on_trackbar(int, void*)
{
Mat cnt_img = Mat::zeros(w, w, CV_8UC3);
int _levels = levels - ;
drawContours( cnt_img, contours, _levels <= ? : -, Scalar(,,),
, LINE_AA, hierarchy, std::abs(_levels) ); imshow("contours", cnt_img);
} int main( int argc, char**)
{
Mat img = Mat::zeros(w, w, CV_8UC1);
if(argc > )
{
help();
return -;
}
//Draw 6 faces
for( int i = ; i < ; i++ )
{
int dx = (i%)* - ;
int dy = (i/)*;
const Scalar white = Scalar();
const Scalar black = Scalar(); if( i == )
{
for( int j = ; j <= ; j++ )
{
double angle = (j+)*CV_PI/;
line(img, Point(cvRound(dx++j*-*cos(angle)),
cvRound(dy+-*sin(angle))),
Point(cvRound(dx++j*-*cos(angle)),
cvRound(dy+-*sin(angle))), white, , , );
}
} ellipse( img, Point(dx+, dy+), Size(,), , , , white, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , black, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , black, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , white, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , white, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , black, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , black, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , black, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , black, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , white, -, , );
ellipse( img, Point(dx+, dy+), Size(,), , , , white, -, , );
}
//show the faces
namedWindow( "image", );
imshow( "image", img );
//Extract the contours so that
vector<vector<Point> > contours0;
findContours( img, contours0, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE); contours.resize(contours0.size());
for( size_t k = ; k < contours0.size(); k++ )
approxPolyDP(Mat(contours0[k]), contours[k], , true); namedWindow( "contours", );
createTrackbar( "levels+3", "contours", &levels, , on_trackbar ); on_trackbar(,);
waitKey(); return ;
}