创建包围轮廓的矩形和圆形边界框--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()的设置类似,这里不再赘述。

<span style="font-size:14px;">#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);

/// 函数声明
void thresh_callback(int, void* );

/** @主函数 */
int main( int argc, char** argv )
{
  /// 载入原图像, 返回3通道图像
  src = imread( argv[1], 1 );

  /// 转化成灰度图像并进行平滑
  cvtColor( src, src_gray, CV_BGR2GRAY );
  blur( src_gray, src_gray, Size(3,3) );

  /// 创建窗口
  char* source_window = "Source";
  namedWindow( source_window, CV_WINDOW_AUTOSIZE );
  imshow( source_window, src );

  createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
  thresh_callback( 0, 0 );

  waitKey(0);
  return(0);
}

/** @thresh_callback 函数 */
void thresh_callback(int, void* )
{
  Mat threshold_output;
  vector<vector<Point> > contours;
  vector<Vec4i> hierarchy;

  /// 使用Threshold检测边缘
  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() );

  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( 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() );
       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 );
}</span>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值