之前一直這裏很迷,現在做一個整理測試,下圖搬運。
先看一下RotatedRect屬性定義:
class CV_EXPORTS RotatedRect
{
public: //構造函數
RotatedRect();
RotatedRect(const Point2f& center, const Size2f& size, float angle);
RotatedRect(const CvBox2D& box);
void points(Point2f pts[]) const; //!返回矩形的4個頂點
Rect boundingRect() const; //返回包含旋轉矩形的最小矩形
operator CvBox2D() const; //!轉換到舊式的cvbox2d結構
Point2f center; //矩形的質心
Size2f size; //矩形的邊長
float angle; //旋轉角度,當角度爲0、90、180、270等時,矩形就成了一個直立的矩形
};
其中opencv奇葩的角度定義如下:
其實觀點點就是我們定義的長和寬和opencv定義的輪廓長寬是不一樣的,所以需要轉化我認知的長於寬,傾斜角度也進行轉化。
//旋轉校正
float angle_rotation;
if ((box.size.width / box.size.height) < 1)
angle_rotation = 90 + box.angle;//正數,逆時針旋轉
else
angle_rotation = box.angle; //負數,順時針旋轉
double scale = 0.78;//縮放比例
Mat rot_m = getRotationMatrix2D(box.center,angle_rotation,scale);//獲得旋轉矩陣
warpAffine(dstImg, dstImg, rot_m, dstImg.size());//仿射變換
參考資料: