首页 > 代码库 > OpenCV Tutorials —— Creating Bounding boxes and circles for contours
OpenCV Tutorials —— Creating Bounding boxes and circles for contours
同样是提取出轮廓之后的处理 ~~
void approxPolyDP(InputArray curve, OutputArray approxCurve, double epsilon, bool closed)
Parameters:
- curve –
Input vector of a 2D point stored in:
- std::vector or Mat (C++ interface)
- Nx2 numpy array (Python interface)
- CvSeq or CvMat (C interface)
- approxCurve – Result of the approximation. The type should match the type of the input curve. In case of C interface the approximated curve is stored in the memory storage and pointer to it is returned.
- epsilon – Parameter specifying the approximation accuracy. This is the maximum distance between the original curve and its approximation.
- closed – If true, the approximated curve is closed (its first and last vertices are connected). Otherwise, it is not closed.
- header_size – Header size of the approximated curve. Normally, sizeof(CvContour) is used.
- storage – Memory storage where the approximated curve is stored.
- method – Contour approximation algorithm. Only CV_POLY_APPROX_DP is supported.
- recursive – Recursion flag. If it is non-zero and curve is CvSeq*, the function cvApproxPolyapproximates all the contours accessible from curve by h_next and v_next links.
Rect boundingRect(InputArray points)
Calculates the up-right bounding rectangle of a point set.
void minEnclosingCircle(InputArray points, Point2f& center, float& radius)
Parameters:
- points –
Input vector of 2D points, stored in:
- std::vector<> or Mat (C++ interface)
- CvSeq* or CvMat* (C interface)
- Nx2 numpy array (Python interface)
- center – Output center of the circle.
- radius – Output radius of the circle.
Code
#include "stdafx.h"#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);/// Function headervoid thresh_callback(int, void* );/** @function main */int main( int argc, char** argv ){ /// Load source image and convert it to gray src = http://www.mamicode.com/imread("img2.jpg", 1 ); /// Convert image to gray and blur it cvtColor( src, src_gray, CV_BGR2GRAY ); blur( src_gray, src_gray, Size(3,3) ); // 均值滤波 —— 去除小的噪声 /// Create Window 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);}/** @function thresh_callback */void thresh_callback(int, void* ){ Mat threshold_output; vector<vector<Point> > contours; vector<Vec4i> hierarchy; /// Detect edges using Threshold threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY ); /// Find contours findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) ); /// Approximate contours to polygons + get bounding rects and circles 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 ); // 原始曲线和近似曲线的精度为3, 近似曲线封闭 boundRect[i] = boundingRect( Mat(contours_poly[i]) ); minEnclosingCircle( (Mat)contours_poly[i], center[i], radius[i] ); } /// Draw polygonal contour + bonding rects + circles 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 ); } /// Show in a window namedWindow( "Contours", CV_WINDOW_AUTOSIZE ); imshow( "Contours", drawing );}
OpenCV Tutorials —— Creating Bounding boxes and circles for contours
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。