文档介绍:opencv轮廓提取和轮廓拟合
#include <iostream>
#include <vector>
#include <opencv2/core/>
#include <opencv2/imgproc/>
#include <opencv2/highgui/>
using namespace std;
using namespace cv;
int main()
{
// Read input binary image
Mat image= imread("./",0);
if (!)
return 0;
namedWindow("Binary Image");
imshow("Binary Image",image);
// Get the contours of the connected components
vector<vector<Point>> contours;
//findContours输入是二值图像
findContours(image,
contours, // a vector of contours
CV_RETR_EXTERNAL, // retrieve the external contours
CV_CHAIN_APPROX_NONE); // retrieve all pixels of each contours
// Print contours' length轮廓个数
cout << "Contours: " << () << endl;
vector<vector<Point>>::const_iterator itContours= ();
for ( ; itContours!=(); ++itContours) {
cout << "Size: " << itContours->size() << endl;//每个轮廓包含点数
}
// draw black contours on white image
Mat result((),CV_8U,Scalar(0));
drawContours(result,contours, //画出轮廓
-1, // draw all contours
Scalar(255), // in black
2); // with a thickness of 2
namedWindow("Contours");
imshow("Contours",result);
// Eliminate too short or too long contours
int cmin= 100; // minimum contour length
int cmax= 1000; // maximum contour length