创建包围轮廓的矩形和圆形边界框--boundingRect()、minEnclosingCircle()和approxPolyDP()

boundingRect()

作用:计算点集的右上边框。

形式:boundingRect(InputArray points);

参数:points:输入二维点集,并用std::vector or Mat存储;


minEnclosingCircle()

作用:找到包围二维点集面积最小的圆。

形式:void minEnclosingCircle(InputArray points, Point2f& center, float& radius);

参数:

points:输入二维点集,并用std::vector or Mat存储;

center:输出圆的中心;

radius:输出圆的半径;


approxPolyDP()

作用:以特定精度近似发出多边形曲线。

形式:void approxPolyDP(InputArray curve, OutputArray approxCurve, double epsilon, bool closed);

参数:

curve:输入一个在std::vector or Mat中存储的二维点向量;

approxCurve:近似的结果即输出;

epsilon:规定估计精度的参数;

closed:如果是true:估计的曲线是封闭的,如果是false:估计的曲线不是封闭的;


rectangle()

作用:画一个简单、明显或充满的右上角矩形。

形式:void rectangle(Mat& img, Point pt1, Point pt2, const Scalar& color, int thickness=1, int lineType=8, int shift=0);

参数:

img:处理的对象图片;

pt1:矩形的顶点;

pt2:与pt1相对的矩形的顶点;

color:矩形的颜色或亮度;

thickness:矩形的边线的粗细,像CV_FILLED的负值,意味着要画一个填充的矩形;

lineType:矩形的边线的类型;

shift:坐标点的小数位;


circle()

作用:画一个圆。

形式:void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0);

参数和rectangle()的设置类似,这里不再赘述。

[cpp]  view plain  copy
  1. <span style="font-size:14px;">#include "opencv2/highgui/highgui.hpp"  
  2. #include "opencv2/imgproc/imgproc.hpp"  
  3. #include <iostream>  
  4. #include <stdio.h>  
  5. #include <stdlib.h>  
  6.   
  7. using namespace cv;  
  8. using namespace std;  
  9.   
  10. Mat src; Mat src_gray;  
  11. int thresh = 100;  
  12. int max_thresh = 255;  
  13. RNG rng(12345);  
  14.   
  15. /// 函数声明  
  16. void thresh_callback(intvoid* );  
  17.   
  18. /** @主函数 */  
  19. int main( int argc, char** argv )  
  20. {  
  21.   /// 载入原图像, 返回3通道图像  
  22.   src = imread( argv[1], 1 );  
  23.   
  24.   /// 转化成灰度图像并进行平滑  
  25.   cvtColor( src, src_gray, CV_BGR2GRAY );  
  26.   blur( src_gray, src_gray, Size(3,3) );  
  27.   
  28.   /// 创建窗口  
  29.   char* source_window = "Source";  
  30.   namedWindow( source_window, CV_WINDOW_AUTOSIZE );  
  31.   imshow( source_window, src );  
  32.   
  33.   createTrackbar( " Threshold:""Source", &thresh, max_thresh, thresh_callback );  
  34.   thresh_callback( 0, 0 );  
  35.   
  36.   waitKey(0);  
  37.   return(0);  
  38. }  
  39.   
  40. /** @thresh_callback 函数 */  
  41. void thresh_callback(intvoid* )  
  42. {  
  43.   Mat threshold_output;  
  44.   vector<vector<Point> > contours;  
  45.   vector<Vec4i> hierarchy;  
  46.   
  47.   /// 使用Threshold检测边缘  
  48.   threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );  
  49.   /// 找到轮廓  
  50.   findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );  
  51.   
  52.   /// 多边形逼近轮廓 + 获取矩形和圆形边界框  
  53.   vector<vector<Point> > contours_poly( contours.size() );  
  54.   vector<Rect> boundRect( contours.size() );  
  55.   vector<Point2f>center( contours.size() );  
  56.   vector<float>radius( contours.size() );  
  57.   
  58.   forint i = 0; i < contours.size(); i++ )  
  59.      { approxPolyDP( Mat(contours[i]), contours_poly[i], 3, true );  
  60.        boundRect[i] = boundingRect( Mat(contours_poly[i]) );  
  61.        minEnclosingCircle( contours_poly[i], center[i], radius[i] );  
  62.      }  
  63.   
  64.   
  65.   /// 画多边形轮廓 + 包围的矩形框 + 圆形框  
  66.   Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );  
  67.   forint i = 0; i< contours.size(); i++ )  
  68.      {  
  69.        Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );  
  70.        drawContours( drawing, contours_poly, i, color, 1, 8, vector<Vec4i>(), 0, Point() );  
  71.        rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );  
  72.        circle( drawing, center[i], (int)radius[i], color, 2, 8, 0 );  
  73.      }  
  74.   
  75.   /// 显示在一个窗口  
  76.   namedWindow( "Contours", CV_WINDOW_AUTOSIZE );  
  77.   imshow( "Contours", drawing );  
  78. }</span>  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值