OpenCV学习(32) 求轮廓的包围盒

      在OpenCV中,能够很方便的求轮廓包围盒。包括矩形,圆形,椭圆形以及倾斜的矩形(包围面积最小)集中包围盒。用到的四个函数是:

Rect boundingRect(InputArray points)

void minEnclosingCircle(InputArray points, Point2f& center, float& radius)

RotatedRect minAreaRect(InputArray points)

RotatedRect fitEllipse(InputArray points)

 

输入的参数都是轮廓,下面是程序代码:

1. Rect和原型包围盒代码:

nt main( int argc, char** argv )
{
//装入图像
src = imread("../ballon.jpg", 1 );

//转化为灰度图并进行blur操作
cvtColor( src, src_gray, CV_BGR2GRAY );
blur( src_gray, src_gray, Size(3,3) );

namedWindow( "source", CV_WINDOW_AUTOSIZE );
imshow( "source", src );

Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

//得到二值图像
threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );
//查找轮廓
findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_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() );

//得到每个轮廓的包围盒RECT以及园,园用中心和半径表示
for( int i = 0; i < contours.size(); i++ )
{
approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );
boundRect[i] = boundingRect( Mat(contours_poly[i]) );
minEnclosingCircle( (Mat)contours_poly[i], center[i], radius[i] );
}


//画轮廓以及它的四边形和原型包围盒
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( int 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, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
//tl是左上角坐标, br是右下角坐标
rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
circle( drawing, center[i], (int)radius[i], color, 2, 8, 0 );
}

namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
imshow( "Contours", drawing );


while(1)
waitKey(0);
return(0);
}

程序运行效果:

imageimage

 

2. 椭圆形和倾斜的矩形包围盒代码:

int main( int argc, char** argv )
{
//装入图像
src = imread("../ballon.jpg", 1 );

//转化为灰度图并进行blur操作
cvtColor( src, src_gray, CV_BGR2GRAY );
blur( src_gray, src_gray, Size(3,3) );

namedWindow( "source", CV_WINDOW_AUTOSIZE );
imshow( "source", src );

Mat threshold_output;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;

//得到二值图像
threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );
//查找轮廓
findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

//查找轮廓的最小旋转rect和椭圆包围盒
vector<RotatedRect> minRect( contours.size() );
vector<RotatedRect> minEllipse( contours.size() );

for( int i = 0; i < contours.size(); i++ )
{ minRect[i] = minAreaRect( Mat(contours[i]) );
if( contours[i].size() > 5 )
{ minEllipse[i] = fitEllipse( Mat(contours[i]) ); }
}

//画轮廓和包围盒
Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
for( int i = 0; i< contours.size(); i++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
// 轮廓
drawContours( drawing, contours, i, color, 1, 8, vector<Vec4i>(), 0, Point() );
// 椭圆
ellipse( drawing, minEllipse[i], color, 2, 8 );
// 旋转rect
Point2f rect_points[4]; minRect[i].points( rect_points );
for( int j = 0; j < 4; j++ )
line( drawing, rect_points[j], rect_points[(j+1)%4], color, 1, 8 );
}

namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
imshow( "Contours", drawing );


while(1)
waitKey(0);
return(0);
}

程序运行效果:

imageimage

 

 

 

原文地址:https://www.cnblogs.com/mikewolf2002/p/3427079.html