1. 程式人生 > >【opencv輪廓擬合備忘錄】

【opencv輪廓擬合備忘錄】

#include <iostream>
#include <vector>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>

using namespace std;
using namespace cv;

int main()
{
	// Read input binary image
	 Mat image= imread("./binaryGroup.bmp",0);
	 if (!image.data)
		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: " << contours.size() << endl;
	 vector<vector<Point>>::const_iterator itContours= contours.begin();
	for ( ; itContours!=contours.end(); ++itContours) {

		cout << "Size: " << itContours->size() << endl;//每個輪廓包含的點數
	}

	// draw black contours on white image
	 Mat result(image.size(),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
	vector<vector<Point>>::const_iterator itc= contours.begin();
	while (itc!=contours.end()) {

		if (itc->size() < cmin || itc->size() > cmax)
			itc= contours.erase(itc);
		else 
			++itc;
	}

	// draw contours on the original image
	Mat original= imread("./group.bmp");
	drawContours(original,contours,
		-1, // draw all contours
		Scalar(255,255,255), // in white
		2); // with a thickness of 2

	namedWindow("Contours on Animals");
	imshow("Contours on Animals",original);

	// Let's now draw black contours on white image
	//result.setTo(Scalar(0));
	//If the third parameter of this function is a negative value, then all contours are drawn.
	//Otherwise, it is possible to specify the index of the contour to be drawn
	drawContours(result,contours,
		-1, // draw all contours
		Scalar(255), // in black
		1); // with a thickness of 1
	image= imread("./binaryGroup.bmp",1);

	// testing the bounding box 
	Rect r0= boundingRect(Mat(contours[0]));//boundingRect獲取這個外接矩形
	rectangle(result,r0,Scalar(255,255,255),2);

	// testing the enclosing circle 
	float radius;
	Point2f center;
	minEnclosingCircle(Mat(contours[1]),center,radius);//對輪廓進行多變形逼近
	circle(result,Point(center),static_cast<int>(radius),Scalar(255),2);

	RotatedRect rrect= fitEllipse(Mat(contours[2]));
	ellipse(result,rrect,Scalar(255),2);

	 //testing the approximate polygon
	vector<Point> poly;
	approxPolyDP(Mat(contours[2]),poly,5,true);

	cout << "Polygon size: " << poly.size() << endl;

	//// Iterate over each segment and draw it
	vector<Point>::const_iterator itp= poly.begin();
	while (itp!=(poly.end()-1)) {
		line(result,*itp,*(itp+1),Scalar(255),2);
		++itp;
	}
	// last point linked to first point
	line(result,*(poly.begin()),*(poly.end()-1),Scalar(20),2);

	// testing the convex hull
	vector<Point> hull;
	convexHull(Mat(contours[3]),hull);

	// Iterate over each segment and draw it
	vector<Point>::const_iterator it= hull.begin();
	while (it!=(hull.end()-1)) {
		line(result,*it,*(it+1),Scalar(255),2);
		++it;
	}
	// last point linked to first point
	line(result,*(hull.begin()),*(hull.end()-1),Scalar(255),2);

	// testing the moments

	//iterate over all contours
	itc= contours.begin();
	while (itc!=contours.end()) {

		// compute all moments
		Moments mom= moments(Mat(*itc++));

		// draw mass center
		circle(result,
			// position of mass center converted to integer
			Point(mom.m10/mom.m00,mom.m01/mom.m00),
			2,Scalar(255),2); // draw black dot
	}

	namedWindow("Some Shape descriptors");
	imshow("Some Shape descriptors",result);

	// New call to findContours but with CV_RETR_LIST flag
	image= imread("./binaryGroup.bmp",0);

	// Get the contours of the connected components
	findContours(image, 
		contours, // a vector of contours 
		CV_RETR_LIST, // retrieve the external and internal contours
		CV_CHAIN_APPROX_NONE); // retrieve all pixels of each contours

	// draw black contours on white image
	result.setTo(Scalar(0));
	drawContours(result,contours,
		-1, // draw all contours
		Scalar(255), // in black
		2); // with a thickness of 2
	namedWindow("All Contours");
	imshow("All Contours",result);

	waitKey();
	return 0;
}
一、對於相對路徑的讀取
./ 表示當前目錄下,即cpp所在目錄下。
../表示上一目錄下

參考連結:https://blog.csdn.net/u014003644/article/details/72228950?utm_source=blogxgwz9

https://www.cnblogs.com/woshitianma/p/3681745.html