aihot  2017-05-20 10:50:41  OpenCV |   查看评论   

OpenCV的代码提取:旋转(rotate)函数的实现

OpenCV中并没有直接提供实现rotate的函数,这里通过getRotationMatrix2D和warpAffine函数实现rotate,并增加了一个crop参数,用来判断是否进行crop。目前支持uchar和float两种类型,经测试,与OpenCV3.1结果完全一致。

实现代码rotate.hpp:

  1. // fbc_cv是免费软件,并且使用与OpenCV相同的许可证    
  2. #ifndef FBC_CV_ROTATE_HPP_  
  3. #define FBC_CV_ROTATE_HPP_  
  4.   
  5. /* reference: include/opencv2/imgproc.hpp 
  6.               modules/imgproc/src/imgwarp.cpp 
  7. */  
  8.   
  9. #include "core/mat.hpp"  
  10. #include "warpAffine.hpp"  
  11.   
  12. namespace fbc {  
  13.   
  14. // 计算2D旋转的仿射矩阵  
  15. // 正值表示逆时针旋转(坐标原点假定为左上角)  
  16. FBC_EXPORTS int getRotationMatrix2D(Point2f center, double angle, double scale, Mat_<double, 1>& dst);  
  17.   
  18. // 将旋转应用于图像
  19. // 该功能无法正常工作
  20. // 支持类型:uchar / float
  21. template<typename _Tp, int chs>  
  22. int rotate(const Mat_<_Tp, chs>& src, Mat_<_Tp, chs>& dst, Point2f center, double angle,  
  23.     bool crop = trueint flags = INTER_LINEAR, int borderMode = BORDER_CONSTANT, const Scalar& borderValue = Scalar())  
  24. {  
  25.     FBC_Assert(typeid(float).name() == typeid(_Tp).name() || typeid(uchar).name() == typeid(_Tp).name());  
  26.     FBC_Assert(src.data != NULL && src.rows > 0 && src.cols > 0);  
  27.   
  28.     Mat_<double, 1> rot_matrix(2, 3);  
  29.     getRotationMatrix2D(center, angle, 1.0, rot_matrix);  
  30.   
  31.     if (crop) {  
  32.         if (dst.data == NULL) {  
  33.             dst = Mat_<_Tp, chs>(src.rows, src.cols);  
  34.         }  
  35.     } else {  
  36.         Rect bbox = RotatedRect(center, Size2f(src.cols, src.rows), angle).boundingRect();  
  37.   
  38.         double* p = (double*)rot_matrix.data;  
  39.         p[2] += bbox.width / 2.0 - center.x;  
  40.         p[5] += bbox.height / 2.0 - center.y;  
  41.   
  42.         if (dst.rows != bbox.height || dst.cols != bbox.width) {  
  43.             dst = Mat_<_Tp, chs>(bbox.height, bbox.width);  
  44.         }  
  45.     }  
  46.   
  47.     warpAffine(src, dst, rot_matrix, flags, borderMode, borderValue);  
  48.   
  49.     return 0;  
  50. }  
  51.   
  52. // 命名空间fbc
  53.   
  54. #endif // FBC_CV_ROTATE_HPP_  

 1/4    1 2 3 4 下一页 尾页
 

除特别注明外,本站所有文章均为 赢咖4注册 原创,转载请注明出处来自OpenCV的代码提取:旋转(rotate)函数的实现

留言与评论(共有 0 条评论)
   
验证码:
[lianlun]1[/lianlun]