吐嘈OpenCV的图像旋转功能 >_<7

  實在出乎我的意料!OpenCV竟然連這么簡單的功能都沒有封裝!還要讓本大爺自己動手寫!強烈要求OpenCV下一個版本添加本功能!

函數功能和這個網頁一樣,只不過這個作者寫的太糟了,我把它變得簡潔了一點  ^_^

 1 void rotate(const Mat& src, Mat& dst, float angle)
 2 {
 3     CV_Assert(!src.empty());
 4 
 5     float radian = angle /180.0 * PI;
 6 
 7     int uniSize = max(src.cols, src.rows) * 2;
 8     int dx = (uniSize - src.cols) / 2;
 9     int dy = (uniSize - src.rows) / 2;
10 
11     copyMakeBorder(src, dst, dy, dy, dx, dx, BORDER_CONSTANT);
12 
13     //旋轉中心
14     Point2f center(dst.cols/2, dst.rows/2);
15     Mat affine_matrix = getRotationMatrix2D( center, angle, 1.0 );
16 
17     warpAffine(dst, dst, affine_matrix, dst.size());
18 
19     float sinVal = fabs(sin(radian));
20     float cosVal = fabs(cos(radian));
21 
22     //旋轉后的圖像大小
23     Size targetSize(src.cols * cosVal + src.rows * sinVal,
24                  src.cols * sinVal + src.rows * cosVal);
25 
26     //剪掉四周边框
27     int x = (dst.cols - targetSize.width) / 2;
28     int y = (dst.rows - targetSize.height) / 2;
29 
30     Rect rect(x, y, targetSize.width, targetSize.height);
31     dst = Mat(dst, rect);
32 }

 

你可能感兴趣的:(opencv)