天天看點

warpAffine函數旋轉、仿射變換保持完整圖檔

https://blog.csdn.net/u013105205/article/details/78826789 這篇部落格講的非常清楚(轉載自該部落格)

對其中最後可展現完整的代碼做簡單記錄(該代碼也摘自上邊連結的最後一段代碼):

Mat img = imread(IMG_PATH);//讀入圖像

//定義仿射變換的中心、角度、尺度
center=Point2f(img.cols / 2.0, img.rows / 2.0);//原文說要獲得完整結果圖像,這裡變換之前的中心必須為原圖的中心
degree=-7;
scale=1;

//獲得變換矩陣
rot = getRotationMatrix2D(center, degree, scale);
Mat rimg;
warpAffine(img, rimg, rot, img.size());

// 擷取變換之後的 區域,這個很重要,不然的話,變換之後的圖像顯示不全
Rect bbox;
bbox = RotatedRect(center, Size(scale*img.cols, scale*img.rows), degree).boundingRect();

// 對變換矩陣的最後一列做修改,重新定義變換的 中心
rot.at<double>(0, 2) += bbox.width / 2 - center.x;
rot.at<double>(1, 2) += bbox.height / 2 - center.y;

Mat dst;
warpAffine(img, dst, rot, bbox.size());//此時的dst就是可以顯示完整的圖像了
imshow("dst", dst);
waitKey(0);

           

記錄内容如有不妥之處,還望指出!