[Opencv]几种对轮廓的处理方式

时间:2019-09-24
本文章向大家介绍[Opencv]几种对轮廓的处理方式,主要包括[Opencv]几种对轮廓的处理方式使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
boundingRect()

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

形式:boundingRect(InputArray points);

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

points:输入信息,可以为包含点的容器(vector)或是Mat。
返回包覆输入信息的最小正矩形。如下图:
RotateRect minAreaRect(InputArray points)
作用:生成最小外接矩形
points,输入信息,可以为包含点的容器(vector)或者是Mat

mat input=img.clone()//img图像为预处理后的二值化图像
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;//存储查找到的第i个轮廓的后[i][0]、前[i][1]、父[i][2]、子轮廓[i][3]
findContours(input, contours, hierarchy, RETR_CCOMP, CHAIN_APPROX_SIMPLE);
RotatedRect r= minAreaRect(contours[i]);

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()的设置类似,这里不再赘述。
#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 );
}

原文地址:https://www.cnblogs.com/lx17746071609/p/11577372.html