结构分析与形状识别(外接矩形 旋转角度)

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.  
};
RotatedRect::points(Point2f pt[]) const  
{  
    double _angle = angle*CV_PI/180.;  
    float b = (float)cos(_angle)*0.5f;  
    float a = (float)sin(_angle)*0.5f;  
  
    pt[0].x = center.x - a*size.height - b*size.width;  
    pt[0].y = center.y + b*size.height - a*size.width;  
    pt[1].x = center.x + a*size.height - b*size.width;  
    pt[1].y = center.y - b*size.height - a*size.width;  
    pt[2].x = 2*center.x - pt[0].x;  
    pt[2].y = 2*center.y - pt[0].y;  
    pt[3].x = 2*center.x - pt[1].x;  
    pt[3].y = 2*center.y - pt[1].y;  
}                      
#include"iostream"  
#include"opencv2/opencv.hpp"  
  
using namespace std;  
using namespace cv;  
  
int main()  
{  
    Mat image(200, 200, CV_8UC3, Scalar(0));  
    RotatedRect rRect(Point2f(100, 100), Size2f(100, 50), 30);  
  
    Point2f vertices[4];      //定义矩形的4个顶点  
    rRect.points(vertices);   //计算矩形的4个顶点  
    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);  
}  
RotatedRect是一个存储旋转矩形的类,通常用来存储最小外包矩形函数minAreaRect( )和椭圆拟合函数fitEllipse( )返回的结果。存储的值,完全取决在于函数的返回

            

之前用到OpenCV最小外接矩形去表示一个类椭圆形的高度,特此记录备查。
对给定的 2D 点集,寻找最小面积的包围矩形,使用函数:
CvBox2D  cvMinAreaRect2( const CvArr* points, CvMemStorage* storage=NULL ); 

   points 
   点序列或点集数组 
   storage 
   可选的临时存储仓 
  函数 cvMinAreaRect2 通过建立凸外形并且旋转外形以寻找给定 2D 点集的最小面积的包围矩形。
其中返回的2D盒子定义如下:
1 typedef struct CvBox2D 
2 { 
3     CvPoint2D32f center; /* 盒子的中心 */ 
4     CvSize2D32f size; /* 盒子的长和宽 */ 
5     float angle; /* 水平轴与第一个边的夹角,用弧度表示*/ 
6 }CvBox2D; 

注意夹角 angle 是水平轴逆时针旋转,与碰到的第一个边(不管是高还是宽)的夹角。如下图 
                                  
  可用函数 cvBoxPoints(box[count], point); 寻找盒子的顶点
1  void cvBoxPoints( CvBox2D box, CvPoint2D32f pt[4] ) 
2  { 
3      double angle = box.angle*CV_PI/180. 
4      float a = (float)cos(angle)*0.5f; 
5      float b = (float)sin(angle)*0.5f; 
6  
7      pt[0].x = box.center.x - a*box.size.height - b*box.size.width; 
8      pt[0].y = box.center.y + b*box.size.height - a*box.size.width; 
9      pt[1].x = box.center.x + a*box.size.height - b*box.size.width; 
10     pt[1].y = box.center.y - b*box.size.height - a*box.size.width; 
11     pt[2].x = 2*box.center.x - pt[0].x; 
12     pt[2].y = 2*box.center.y - pt[0].y; 
13     pt[3].x = 2*box.center.x - pt[1].x; 
14     pt[3].y = 2*box.center.y - pt[1].y; 
15 } 
简单证明此函数的计算公式:
 
   计算x,由图可得到三个方程式: pt[1].x - pt[0].x = width*sin(angle) 
                             pt[2].x - pt[1].x = height*cos(angle) 
                             pt[2].x - pt[0].x = 2(box.center.x - pt[0].x)
   联立方程可解得函数里的计算式,算 y 略。
写了个函数绘制CvBox2D
1  void DrawBox(CvBox2D box,IplImage* img) 
2  { 
3      CvPoint2D32f point[4]; 
4      int i; 
5      for ( i=0; i<4; i++) 
6      { 
7          point[i].x = 0; 
8          point[i].y = 0; 
9      } 
10     cvBoxPoints(box, point); //计算二维盒子顶点 
11     CvPoint pt[4]; 
12     for ( i=0; i<4; i++) 
13     { 
14         pt[i].x = (int)point[i].x; 
15         pt[i].y = (int)point[i].y; 
16     } 
17     cvLine( img, pt[0], pt[1],CV_RGB(255,0,0), 2, 8, 0 ); 
18     cvLine( img, pt[1], pt[2],CV_RGB(255,0,0), 2, 8, 0 ); 
19     cvLine( img, pt[2], pt[3],CV_RGB(255,0,0), 2, 8, 0 ); 
20     cvLine( img, pt[3], pt[0],CV_RGB(255,0,0), 2, 8, 0 ); 



你可能感兴趣的:(图像匹配,图像分析)