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

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM