Tags: DIP
findContours()
void findContours(InputOutputArray image, OutputArrayOfArrayscontours, OutputArray hierarchy, int mode, intmethod, Point offset=Point())
输入图像image必须为一个2值单通道图像
contours参数为检测的轮廓数组,每一个轮廓用一个point类型的vector表示
hiararchy参数和轮廓个数相同,每个轮廓contours[ i ]对应4个hierarchy元素hierarchy[ i ][ 0 ] ~hierarchy[ i ][ 3 ],分别表示后一个轮廓、前一个轮廓、父轮廓、内嵌轮廓的索引编号,如果没有对应项,该值设置为负数。
-
mode表示轮廓的检索模式:
CV_RETR_EXTERNAL表示只检测外轮廓
CV_RETR_LIST检测的轮廓不建立等级关系
CV_RETR_CCOMP建立两个等级的轮廓,上面的一层为外边界,里面的一层为内孔的边界信息。如果内孔内还有一个连通物体,这个物体的边界也在顶层。
CV_RETR_TREE建立一个等级树结构的轮廓。具体参考contours.c这个demo
-
method为轮廓近似办法
CV_CHAIN_APPROX_NONE存储所有的轮廓点,相邻的两个点的像素位置差不超过1,即max(abs(x1-x2),abs(y2-y1))==1
CV_CHAIN_APPROX_SIMPLE压缩水平方向,垂直方向,对角线方向的元素,只保留该方向的终点坐标,例如一个矩形轮廓只需4个点来保存轮廓信息
CV_CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS使用teh-Chinl chain 近似算法
approxPolyDP()函数对轮廓做多边形曲线近似处理
void cv::approxPolyDP(InputArray curve, OutputArray approxCurve, double epsilon, bool closed)
使用曲线或者多边形在一定的精度限制下,近似原来的曲线或者多边形。epsilon限制新的曲线和原有曲线的最大距离。closed:曲线是否闭合。
boundingRect() 返回轮廓正(upright)矩形
MinAreaRect() 返回轮廓的最小外接矩形
最小面积外接矩形通常是旋转矩形,返回RotatedRect对象。
RotatedRect对象:
class cv::RotatedRect {
cv::Point2f center; // Exact center point (around which to rotate)
cv::Size2f size; // Size of rectangle (centered on 'center')
float angle; // degrees
};
minEnclosingCircle() 返回轮廓最小外接圆
其他contour相关函数:
ArcLength() //计算轮廓的长度
ContourArea() //计算轮廓区域的面积
ConvexHull() //计算凸壳轮廓
IsContourConvex() //测试一个轮廓凸性
FitLine() //直线拟合
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
Mat src;
Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345); // random number generator
void thresh_callback(int, void* );
int main( int, char** argv )
{
src = imread( argv[1], IMREAD_COLOR );
cvtColor( src, src_gray, COLOR_BGR2GRAY );
blur( src_gray, src_gray, Size(3,3) );
const char* source_window = "Source";
namedWindow( source_window, WINDOW_AUTOSIZE );
imshow( source_window, src );
createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
thresh_callback( 0, 0 );
waitKey(0);
return(0);
}
void thresh_callback(int, void* )
{
Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );
findContours( threshold_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(0, 0) );
vector<vector<Point> > contours_poly( contours.size() );
vector<Rect> boundRect( contours.size() );
vector<Point2f>center( contours.size() );
vector<float>radius( contours.size() );
vector<RotatedRect> rotateBox(contours.size());
Point2f vtx[4];
for( size_t i = 0; i < contours.size(); i++ )
{
approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
boundRect[i] = boundingRect( Mat(contours_poly[i]) );
minEnclosingCircle( contours_poly[i], center[i], radius[i] );
rotateBox[i] = minAreaRect(Mat(contours_poly[i]));
}
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( size_t i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing, contours_poly, (int)i, color, 1, 8, vector<Vec4i>(), 0, Point() );
rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
circle( drawing, center[i], (int)radius[i], color, 2, 8, 0 );
rotateBox[i].points(vtx);
for(int i = 0; i < 4; i++){
line(drawing, vtx[i], vtx[(i+1)%4], color, 2, 8, 0);
}
}
namedWindow( "Contours", WINDOW_AUTOSIZE );
imshow( "Contours", drawing );
}