minAreaRect函数

1、minAreaRect函数

函数作用:

主要求得包含点集最小面积的矩形,,这个矩形是可以有偏转角度的,可以与图像的边界不平行

2、minAreaRect函数调用形式

C++: RotatedRect minAreaRect(InputArray points)

InputArray points:表示输入的点集

输出是矩形的四个点坐标

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\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 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;
}


你可能感兴趣的:(minAreaRect函数)