結構分析與形狀識別(外接矩形 旋轉角度)
阿新 • • 發佈:2019-02-03
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; }
RotatedRect是一個儲存旋轉矩形的類,通常用來儲存最小外包矩形函式minAreaRect( )和橢圓擬合函式fitEllipse( )返回的結果。儲存的值,完全取決在於函式的返回#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); }
之前用到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 );