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 ) { 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); } void thresh_callback( int , void * ) { 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() ); 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>