minAreaRect関数

13111 ワード

1、minAreaRect関数
関数の役割:
主に点セットの最小面積を含む矩形を求め,この矩形は偏向角度があり,画像の境界と平行でないことができる.
2、minAreaRect関数呼び出し形式
C++: RotatedRect minAreaRect(InputArray points)
InputArray points:入力されたポイントセットを表す
出力は長方形の4つの点座標です
RotatedRect
class 
RotatedRect
class CV_EXPORTS RotatedRect
{
public:
    //! various constructors
    RotatedRect();
    RotatedRect(const Point2f& center, const Size2f& size, float angle);
    RotatedRect(const CvBox2D& box);

    //! returns 4 vertices of the rectangle
    void points(Point2f pts[]) const;
    //! returns the minimal up-right rectangle containing the rotated rectangle
    Rect boundingRect() const;
    //! conversion to the old-style CvBox2D structure
    operator CvBox2D() const;

    Point2f center; //< the rectangle mass center
    Size2f size;    //< width and height of the rectangle
    float angle;    //< the rotation angle. When the angle is 0, 90, 180, 270 etc., the rectangle becomes an up-right rectangle.
};

The class represents rotated (i.e. not up-right) rectangles on a plane. Each rectangle is specified by the center point (mass center), length of each side (represented by cv::Size2f structure) and the rotation angle in degrees.
C++:
 
RotatedRect::
RotatedRect
(
)
C++:
 
RotatedRect::
RotatedRect
(const Point2f& 
center, const Size2f& 
size, float 
angle
)
C++:
 
RotatedRect::
RotatedRect
(const CvBox2D& 
box
)
Parameters:
center – The rectangle mass center.
size – Width and height of the rectangle.
angle – The rotation angle in a clockwise direction. When the angle is 0, 90, 180, 270 etc., the rectangle becomes an up-right rectangle.
box – The rotated rectangle parameters as the obsolete CvBox2D structure.
C++:
 void 
RotatedRect::
points
(Point2f 
pts[]
)
 const //! returns 4 vertices of the rectangle
C++:
 Rect 
RotatedRect::
boundingRect
(
)
 const
C++:
 
RotatedRect::
operator CvBox2D
(
)
 const
Parameters:
pts – The points array for storing rectangle vertices.
The sample below demonstrates how to use RotatedRect:
Mat image(200, 200, CV_8UC3, Scalar(0));
RotatedRect rRect = RotatedRect(Point2f(100,100), Size2f(100,50), 30);

Point2f vertices[4];
rRect.points(vertices);//        
for (int i = 0; i < 4; i++)
    line(image, vertices[i], vertices[(i+1)%4], Scalar(0,255,0));

Rect brect = rRect.boundingRect();
rectangle(image, brect, Scalar(255,0,0));

imshow("rectangles", image);
waitKey(0);

3、opencvコード
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include 
#include 
#include 

using namespace cv;
using namespace std;

Mat src; Mat src_gray;
int thresh = 100;
int max_thresh = 255;
RNG rng(12345);

/// Function header
void thresh_callback(int, void* );

/** @function main */
int main( int argc, char** argv )
{
  ///      
  src = imread( argv[1], 1 );

  ///          
  cvtColor( src, src_gray, CV_BGR2GRAY );
  blur( src_gray, src_gray, Size(3,3) );

  ///     
  char* source_window = "Source";
  namedWindow( source_window, CV_WINDOW_AUTOSIZE );
  imshow( source_window, src );

  createTrackbar( " Threshold:", "Source", &thresh, max_thresh, thresh_callback );
  thresh_callback( 0, 0 );

  waitKey(0);
  return(0);
}

/** @function thresh_callback */
void thresh_callback(int, void* )
{
  Mat threshold_output;
  vector > contours;
  vector hierarchy;

  ///        
  threshold( src_gray, threshold_output, thresh, 255, THRESH_BINARY );
  ///     
  findContours( threshold_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

  ///                     
  vector minRect( contours.size() );
  vector minEllipse( contours.size() );

  for( int i = 0; i < contours.size(); i++ )
     { minRect[i] = minAreaRect( Mat(contours[i]) );
       if( contours[i].size() > 5 )
         { minEllipse[i] = fitEllipse( Mat(contours[i]) ); }
     }

  ///                   
  Mat drawing = Mat::zeros( threshold_output.size(), CV_8UC3 );
  for( int i = 0; i< contours.size(); i++ )
     {
       Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
       // contour
       drawContours( drawing, contours, i, color, 1, 8, vector(), 0, Point() );
       // ellipse
       ellipse( drawing, minEllipse[i], color, 2, 8 );
       // rotated rectangle
       Point2f rect_points[4]; minRect[i].points( rect_points );
       for( int j = 0; j < 4; j++ )
          line( drawing, rect_points[j], rect_points[(j+1)%4], color, 1, 8 );
     }

  ///         
  namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
  imshow( "Contours", drawing );
}

Opencvソース:
#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"

#include 

using namespace cv;
using namespace std;

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