opencv/samples/cpp/minarea.cpp

64 lines
1.6 KiB
C++
Raw Normal View History

2010-11-28 00:15:16 +01:00
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
2011-07-30 13:59:09 +02:00
2010-12-04 09:29:18 +01:00
#include <iostream>
2011-07-30 13:59:09 +02:00
2010-11-28 00:15:16 +01:00
using namespace cv;
2010-12-04 09:29:18 +01:00
using namespace std;
static void help()
2010-12-04 09:29:18 +01:00
{
cout << "This program demonstrates finding the minimum enclosing box or circle of a set\n"
2011-07-30 13:59:09 +02:00
"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;
2010-12-04 09:29:18 +01:00
}
2010-12-21 12:37:08 +01:00
int main( int /*argc*/, char** /*argv*/ )
2010-11-28 00:15:16 +01:00
{
2010-12-04 09:29:18 +01:00
help();
2011-07-30 13:59:09 +02:00
Mat img(500, 500, CV_8UC3);
RNG& rng = theRNG();
2011-07-30 13:59:09 +02:00
2010-11-28 00:15:16 +01:00
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);
2010-11-28 00:15:16 +01:00
points.push_back(pt);
}
2010-11-28 00:15:16 +01:00
RotatedRect box = minAreaRect(Mat(points));
2011-07-30 13:59:09 +02:00
2010-11-28 00:15:16 +01:00
Point2f center, vtx[4];
float radius = 0;
minEnclosingCircle(Mat(points), center, radius);
box.points(vtx);
2010-11-28 00:15:16 +01:00
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);
2010-11-28 00:15:16 +01:00
imshow( "rect & circle", img );
char key = (char)cvWaitKey();
if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
break;
}
2011-07-30 13:59:09 +02:00
2010-11-28 00:15:16 +01:00
return 0;
}