Home | History | Annotate | Download | only in cpp
      1 #include "opencv2/highgui/highgui.hpp"
      2 #include "opencv2/imgproc/imgproc.hpp"
      3 
      4 #include <iostream>
      5 
      6 using namespace cv;
      7 using namespace std;
      8 
      9 static void help()
     10 {
     11     cout << "This program demonstrates finding the minimum enclosing box, triangle or circle of a set\n"
     12          << "of points using functions: minAreaRect() minEnclosingTriangle() minEnclosingCircle().\n"
     13          << "Random points are generated and then enclosed.\n\n"
     14          << "Press ESC, 'q' or 'Q' to exit and any other key to regenerate the set of points.\n\n"
     15          << "Call:\n"
     16          << "./minarea\n"
     17          << "Using OpenCV v" << CV_VERSION << "\n" << endl;
     18 }
     19 
     20 int main( int /*argc*/, char** /*argv*/ )
     21 {
     22     help();
     23 
     24     Mat img(500, 500, CV_8UC3);
     25     RNG& rng = theRNG();
     26 
     27     for(;;)
     28     {
     29         int i, count = rng.uniform(1, 101);
     30         vector<Point> points;
     31 
     32         // Generate a random set of points
     33         for( i = 0; i < count; i++ )
     34         {
     35             Point pt;
     36             pt.x = rng.uniform(img.cols/4, img.cols*3/4);
     37             pt.y = rng.uniform(img.rows/4, img.rows*3/4);
     38 
     39             points.push_back(pt);
     40         }
     41 
     42         // Find the minimum area enclosing bounding box
     43         RotatedRect box = minAreaRect(Mat(points));
     44 
     45         // Find the minimum area enclosing triangle
     46         vector<Point2f> triangle;
     47 
     48         minEnclosingTriangle(points, triangle);
     49 
     50         // Find the minimum area enclosing circle
     51         Point2f center, vtx[4];
     52         float radius = 0;
     53         minEnclosingCircle(Mat(points), center, radius);
     54         box.points(vtx);
     55 
     56         img = Scalar::all(0);
     57 
     58         // Draw the points
     59         for( i = 0; i < count; i++ )
     60             circle( img, points[i], 3, Scalar(0, 0, 255), FILLED, LINE_AA );
     61 
     62         // Draw the bounding box
     63         for( i = 0; i < 4; i++ )
     64             line(img, vtx[i], vtx[(i+1)%4], Scalar(0, 255, 0), 1, LINE_AA);
     65 
     66         // Draw the triangle
     67         for( i = 0; i < 3; i++ )
     68             line(img, triangle[i], triangle[(i+1)%3], Scalar(255, 255, 0), 1, LINE_AA);
     69 
     70         // Draw the circle
     71         circle(img, center, cvRound(radius), Scalar(0, 255, 255), 1, LINE_AA);
     72 
     73         imshow( "Rectangle, triangle & circle", img );
     74 
     75         char key = (char)waitKey();
     76         if( key == 27 || key == 'q' || key == 'Q' ) // 'ESC'
     77             break;
     78     }
     79 
     80     return 0;
     81 }
     82