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

测试代码test_rotate.cpp:

 

  1. #include "test_rotate.hpp"  
  2. #include <assert.h>  
  3. #include <opencv2/opencv.hpp>  
  4. #include <rotate.hpp>  
  5.   
  6. int test_getRotationMatrix2D()  
  7. {  
  8.     cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  9.     if (!matSrc.data) {  
  10.         std::cout << "read image fail" << std::endl;  
  11.         return -1;  
  12.     }  
  13.   
  14.     double angle = -50.0;  
  15.     double scale = 0.6;  
  16.   
  17.     fbc::Point2f center = fbc::Point2f(matSrc.cols / 2, matSrc.rows / 2);  
  18.     fbc::Mat_<double, 1> mat_rot(2, 3);  
  19.     fbc::getRotationMatrix2D(center, angle, scale, mat_rot);  
  20.   
  21.     // 计算相对于图像中心的旋转矩阵
  22.     cv::Point center_ = cv::Point(matSrc.cols / 2, matSrc.rows / 2);  
  23.     // 获取具有上述规格的旋转矩阵
  24.     cv::Mat mat_rot_ = cv::getRotationMatrix2D(center_, angle, scale);  
  25.   
  26.     assert(mat_rot.cols == mat_rot_.cols && mat_rot.rows == mat_rot_.rows);  
  27.     assert(mat_rot.step == mat_rot_.step);  
  28.     for (int y = 0; y < mat_rot.rows; y++) {  
  29.         const fbc::uchar* p = mat_rot.ptr(y);  
  30.         const uchar* p_ = mat_rot_.ptr(y);  
  31.   
  32.         for (int x = 0; x < mat_rot.step; x++) {  
  33.             assert(p[x] == p_[x]);  
  34.         }  
  35.     }  
  36.   
  37.     return 0;  
  38. }  
  39.   
  40. int test_rotate_uchar()  
  41. {  
  42.     cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  43.     if (!matSrc.data) {  
  44.         std::cout << "read image fail" << std::endl;  
  45.         return -1;  
  46.     }  
  47.   
  48.     double angle = -50.0;  
  49.   
  50.     for (int interpolation = 0; interpolation < 5; interpolation++) {  
  51.         fbc::Point2f center = fbc::Point2f(matSrc.cols / 2.0, matSrc.rows / 2.0);  
  52.         fbc::Mat_<uchar, 3> mat(matSrc.rows, matSrc.cols, matSrc.data);  
  53.         fbc::Mat_<uchar, 3> rotate_dst;  
  54.         fbc::rotate(mat, rotate_dst, center, angle, true, interpolation);  
  55.   
  56.         // 计算相对于图像中心的旋转矩阵
  57.         cv::Point2f center_ = cv::Point2f(matSrc.cols / 2.0, matSrc.rows / 2.0);  
  58.   
  59.         // 获取具有上述规格的旋转矩阵
  60.         cv::Mat mat_rot_ = getRotationMatrix2D(center_, angle, 1.0);  
  61.         cv::Mat rotate_dst_;  
  62.   
  63.         cv::warpAffine(matSrc, rotate_dst_, mat_rot_, matSrc.size(), interpolation);  
  64.   
  65.         assert(rotate_dst.step == rotate_dst_.step && rotate_dst.rows == rotate_dst_.rows);  
  66.         for (int y = 0; y < rotate_dst.rows; y++) {  
  67.             const fbc::uchar* p = rotate_dst.ptr(y);  
  68.             const uchar* p_ = rotate_dst_.ptr(y);  
  69.   
  70.             for (int x = 0; x < rotate_dst.step; x++) {  
  71.                 assert(p[x] == p_[x]);  
  72.             }  
  73.         }  
  74.     }  
  75.   
  76.     return 0;  
  77. }  
  78.   
  79. int test_rotate_float()  
  80. {  
  81.     cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  82.     if (!matSrc.data) {  
  83.         std::cout << "read image fail" << std::endl;  
  84.         return -1;  
  85.     }  
  86.     cv::cvtColor(matSrc, matSrc, CV_BGR2GRAY);  
  87.     matSrc.convertTo(matSrc, CV_32FC1);  
  88.   
  89.     double angle = -50.0;  
  90.   
  91.     for (int interpolation = 0; interpolation < 5; interpolation++) {  
  92.         fbc::Point2f center = fbc::Point2f(matSrc.cols / 2.0, matSrc.rows / 2.0);  
  93.         fbc::Mat_<float, 1> mat(matSrc.rows, matSrc.cols, matSrc.data);  
  94.         fbc::Mat_<float, 1> rotate_dst;  
  95.         fbc::rotate(mat, rotate_dst, center, angle, true, interpolation);  
  96.   
  97.         // 计算相对于图像中心的旋转矩阵
  98.         cv::Point2f center_ = cv::Point2f(matSrc.cols / 2.0, matSrc.rows / 2.0);  
  99.   
  100.         // 获取具有上述规格的旋转矩阵
  101.         cv::Mat mat_rot_ = getRotationMatrix2D(center_, angle, 1.0);  
  102.         cv::Mat rotate_dst_;  
  103.   
  104.         cv::warpAffine(matSrc, rotate_dst_, mat_rot_, matSrc.size(), interpolation);  
  105.   
  106.         assert(rotate_dst.step == rotate_dst_.step && rotate_dst.rows == rotate_dst_.rows);  
  107.         for (int y = 0; y < rotate_dst.rows; y++) {  
  108.             const fbc::uchar* p = rotate_dst.ptr(y);  
  109.             const uchar* p_ = rotate_dst_.ptr(y);  
  110.   
  111.             for (int x = 0; x < rotate_dst.step; x++) {  
  112.                 assert(p[x] == p_[x]);  
  113.             }  
  114.         }  
  115.     }  
  116.   
  117.     return 0;  
  118. }  
  119.   
  120. int test_rotate_without_crop()  
  121. {  
  122.     cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/1.jpg", 1);  
  123.     if (!matSrc.data) {  
  124.         std::cout << "read image fail" << std::endl;  
  125.         return -1;  
  126.     }  
  127.   
  128.     double angle = -50.0;  
  129.     double scale = 0.6;  
  130.   
  131.     fbc::Point2f center = fbc::Point2f(matSrc.cols / 2.0, matSrc.rows / 2.0);  
  132.     fbc::Mat_<uchar, 3> mat(matSrc.rows, matSrc.cols, matSrc.data);  
  133.     fbc::Mat_<uchar, 3> rotate_dst;  
  134.     fbc::rotate(mat, rotate_dst, center, angle, true/*false*/, 2, 0, fbc::Scalar(128, 255, 0));  
  135.   
  136.     cv::Mat mat_save(rotate_dst.rows, rotate_dst.cols, CV_8UC3, rotate_dst.data);  
  137.     cv::imwrite("E:/GitCode/OpenCV_Test/test_images/1_rotate2.jpg", mat_save);  
  138.   
  139.     return 0;  
  140. }  

 

以下分别是源图像,调用rotate函数,生成的crop和非crop的结果图像:

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

 

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

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