1. 程式人生 > >opencv中使用形狀包圍輪廓

opencv中使用形狀包圍輪廓

外部矩形邊界 C++ Rect boundingRect(InputArray points)

最小包圍矩形:C++ RotatedRect minAreaRect(InputArray points)
最小包圍圓形:C++ void minEnclosingCircle(InputArrayPoints,Point2f&center,float&radius)
橢圓擬合:C++ RotatedRect fitEllipse(InputArray points)
逼近多邊形:C++ void approxPolyDP(InputArray curve,OutputArray approxCurve,double epsilon,bool closed);
curve是vector或者Mat的點集
approx輸出型別和輸入一樣
epsilon是近似曲線和原始曲線的最大間隔
closed是指曲線是否閉合
例子一:

用最小包圍矩形包住隨機生成的點,在講之前,先講一下RotatedRect這個返回型別,是旋轉矩形
要將這個返回型別,變到Mat裡面,方法是:將其四角存到(Point  陣列名) 裡面,變成一個輪廓
RotatedRect::RotatedRect(const Point2f& center, const Size2f& size, float angle)// 中心點(不是左上角座標),尺寸,旋轉角度

RotatedRect rRect = RotatedRect(Point2f(100,100), Size2f(100,50), 30);

int main()
{
	Mat src;
	src=Mat::zeros(600,600,CV_8UC3); //建立一個畫布
	RNG rng;
	vector points;
	for(int i=0;i<20;i++)
	{
		Point2f point;
		point.x=rng.uniform(src.cols*0.4,src.cols*0.7);
		point.y=rng.uniform(src.rows*0.5,src.rows*0.7);
		points.push_back(point);
		circle(src,point,3,Scalar(255,255,255),CV_FILLED,8,0);//順便把圓畫出來
	}
	RotatedRect box=minAreaRect(points);  //提取RotatedRect需要用以下方法,存四角
	Point2f four_cornor[4];
	box.points(four_cornor);
	    for(int i=0;i<4;i++)  //把RotatedRect畫出來
		{
			line(src,four_cornor[i],four_cornor[(1+i)%4],Scalar(255,255,255),1,8,0);
		}
	imshow("a",src);
	waitKey(0);
}

例子二:用圓包圍

int main()
{
	Mat src;
	src=Mat::zeros(600,600,CV_8UC3); //建立一個畫布
	RNG rng;
	vector points;
	for(int i=0;i<50;i++)
	{
		Point2f point;
		point.x=rng.uniform(src.cols*0.4,src.cols*0.7);
		point.y=rng.uniform(src.rows*0.5,src.rows*0.7);
		points.push_back(point);
		circle(src,point,3,Scalar(255,255,255),CV_FILLED,8,0);//順便把圓畫出來
	}
	Point2f center;
	float radiu;
	minEnclosingCircle(points,center,radiu);  
	//把圓畫出來
		circle(src,center,radiu,Scalar(255,255,255),1,8,0);
	imshow("a",src);
	waitKey(0);
}

例子三:使用多邊形包圍真實影象的輪廓

#include
#include 
#include 
#include 
#include 
#include 
#include  "vector"

using namespace std;
using namespace cv;

//目的:找到一個影象的輪廓,並用多邊形逼近,且可以用進度條調整閾值化函式
//步驟:1.讀入一張圖,同時灰度化 2.閾值化 3.findCounters 4.多邊形逼近函式 5.視覺化 

Mat src,dst;
int s_threshold=80;
void thresCallback(int,void*);

int main()
{
	src=imread("zuqiuchang.jpg",0);
	
	namedWindow("a");
	createTrackbar("閾值","a",&s_threshold,255,thresCallback);
	thresCallback(s_threshold,0);

	waitKey(0);

}

void thresCallback(int,void*)
{
	Mat result;
	threshold(src,dst,s_threshold,255,THRESH_BINARY);
	vector> contour;
	vector hierarchy;
	result.create(dst.size(),CV_8UC3);//建立一個最後接受結果用的Mat

	findContours(dst,contour,hierarchy,RETR_CCOMP,CHAIN_APPROX_SIMPLE);
	//這裡輪廓應該有很多個,還是要有一個迴圈遍歷

	vector> contour_result(contour.size());  
	vector center(contour.size());
	vector   radius (contour.size());
	vector rect(contour.size());
 
	for(int i=0;i

原圖:

結果: