OpenCV2.4 例程三 (求外包圆和外包矩形)

以下程序来自OpenCV例程:

#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include <iostream>

using namespace cv;
using namespace std;

void help()
{
	cout << "This program demonstrates finding the minimum enclosing box or circle of a set\n"
		"of points using functions: minAreaRect() minEnclosingCircle().\n"
		"Random points are generated and then enclosed.\n"
		"Call:\n"
		"./minarea\n"
		"Using OpenCV version %s\n" << CV_VERSION << "\n" << endl;
}

int main( int /*argc*/, char** /*argv*/ )
{
	help();

	Mat img(500, 500, CV_8UC3);
	RNG& rng = theRNG();    

	for(;;)
	{
		int i, count = rng.uniform(1, 101);
		vector<Point> points;
		for( i = 0; i < count; i++ )
		{
			Point pt;
			pt.x = rng.uniform(img.cols/4, img.cols*3/4);
			pt.y = rng.uniform(img.rows/4, img.rows*3/4);

			points.push_back(pt);
		}

		RotatedRect box = minAreaRect(Mat(points));

		Point2f center, vtx[4];
		float radius = 0;
		minEnclosingCircle(Mat(points), center, radius);
		box.points(vtx);

		img = Scalar::all(0);
		for( i = 0; i < count; i++ )
			circle( img, points[i], 3, Scalar(0, 0, 255), CV_FILLED, CV_AA );

		for( i = 0; i < 4; i++ )
			line(img, vtx[i], vtx[(i+1)%4], Scalar(0, 255, 0), 1, CV_AA);

		circle(img, center, cvRound(radius), Scalar(0, 255, 255), 1, CV_AA); 

		imshow( "rect & circle", img );

		char key = (char)cvWaitKey();
		if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
			break;
	}

	return 0;
}

结果图:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值