aihot  2017-05-21 11:04:15  OpenCV |   查看评论   

测试代码test_warpAffine.cpp:

  1. #include "test_warpAffine.hpp"  
  2. #include <assert.h>  
  3. #include <opencv2/opencv.hpp>  
  4. #include <core/mat.hpp>  
  5. #include <warpAffine.hpp>  
  6.   
  7. int test_getAffineTransform()  
  8. {  
  9.     cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  10.     if (!matSrc.data) {  
  11.         std::cout << "read image fail" << std::endl;  
  12.         return -1;  
  13.     }  
  14.   
  15.     fbc::Point2f srcTri[3];  
  16.     fbc::Point2f dstTri[3];  
  17.   
  18.     // 设置你的3点来计算仿射变换
  19.     srcTri[0] = fbc::Point2f(0, 0);  
  20.     srcTri[1] = fbc::Point2f(matSrc.cols - 1, 0);  
  21.     srcTri[2] = fbc::Point2f(0, matSrc.rows - 1);  
  22.   
  23.     dstTri[0] = fbc::Point2f(matSrc.cols*0.0, matSrc.rows*0.33);  
  24.     dstTri[1] = fbc::Point2f(matSrc.cols*0.85, matSrc.rows*0.25);  
  25.     dstTri[2] = fbc::Point2f(matSrc.cols*0.15, matSrc.rows*0.7);  
  26.   
  27.     // 获得仿射变换
  28.     fbc::Mat_<double, 1> warp_mat(2, 3);  
  29.     int ret = fbc::getAffineTransform(srcTri, dstTri, warp_mat);  
  30.     assert(ret == 0);  
  31.   
  32.     cv::Point2f srcTri_[3];  
  33.     cv::Point2f dstTri_[3];  
  34.   
  35.     // 设置你的3点来计算仿射变换
  36.     srcTri_[0] = cv::Point2f(0, 0);  
  37.     srcTri_[1] = cv::Point2f(matSrc.cols - 1, 0);  
  38.     srcTri_[2] = cv::Point2f(0, matSrc.rows - 1);  
  39.   
  40.     dstTri_[0] = cv::Point2f(matSrc.cols*0.0, matSrc.rows*0.33);  
  41.     dstTri_[1] = cv::Point2f(matSrc.cols*0.85, matSrc.rows*0.25);  
  42.     dstTri_[2] = cv::Point2f(matSrc.cols*0.15, matSrc.rows*0.7);  
  43.   
  44.     // 获得仿射变换
  45.     cv::Mat warp_mat_(2, 3, CV_64FC1);  
  46.     warp_mat_ = cv::getAffineTransform(srcTri_, dstTri_);  
  47.   
  48.     assert(warp_mat.cols == warp_mat_.cols && warp_mat.rows == warp_mat_.rows);  
  49.     assert(warp_mat.step == warp_mat_.step);  
  50.     for (int y = 0; y < warp_mat.rows; y++) {  
  51.         const fbc::uchar* p = warp_mat.ptr(y);  
  52.         const uchar* p_ = warp_mat_.ptr(y);  
  53.   
  54.         for (int x = 0; x < warp_mat.step; x++) {  
  55.             assert(p[x] == p_[x]);  
  56.         }  
  57.     }  
  58.   
  59.     return 0;  
  60. }  
  61.   
  62. int test_warpAffine_uchar()  
  63. {  
  64.     cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  65.     if (!matSrc.data) {  
  66.         std::cout << "read image fail" << std::endl;  
  67.         return -1;  
  68.     }  
  69.   
  70.     for (int interpolation = 0; interpolation < 5; interpolation++) {  
  71.         fbc::Point2f srcTri[3];  
  72.         fbc::Point2f dstTri[3];  
  73.   
  74.         // 设置你的3点来计算仿射变换
  75.         srcTri[0] = fbc::Point2f(0, 0);  
  76.         srcTri[1] = fbc::Point2f(matSrc.cols - 1, 0);  
  77.         srcTri[2] = fbc::Point2f(0, matSrc.rows - 1);  
  78.   
  79.         dstTri[0] = fbc::Point2f(matSrc.cols*0.0, matSrc.rows*0.33);  
  80.         dstTri[1] = fbc::Point2f(matSrc.cols*0.85, matSrc.rows*0.25);  
  81.         dstTri[2] = fbc::Point2f(matSrc.cols*0.15, matSrc.rows*0.7);  
  82.   
  83.         // 获得仿射变换
  84.         fbc::Mat_<double, 1> warp_mat(2, 3);  
  85.         int ret = fbc::getAffineTransform(srcTri, dstTri, warp_mat);  
  86.         assert(ret == 0);  
  87.   
  88.         fbc::Mat_<uchar, 3> mat(matSrc.rows, matSrc.cols, matSrc.data);  
  89.         fbc::Mat_<uchar, 3> warp_dst;  
  90.         warp_dst.zeros(mat.rows, mat.cols);  
  91.   
  92.         fbc::warpAffine(mat, warp_dst, warp_mat, interpolation);  
  93.   
  94.         cv::Point2f srcTri_[3];  
  95.         cv::Point2f dstTri_[3];  
  96.   
  97.         // 设置你的3点来计算仿射变换
  98.         srcTri_[0] = cv::Point2f(0, 0);  
  99.         srcTri_[1] = cv::Point2f(matSrc.cols - 1, 0);  
  100.         srcTri_[2] = cv::Point2f(0, matSrc.rows - 1);  
  101.   
  102.         dstTri_[0] = cv::Point2f(matSrc.cols*0.0, matSrc.rows*0.33);  
  103.         dstTri_[1] = cv::Point2f(matSrc.cols*0.85, matSrc.rows*0.25);  
  104.         dstTri_[2] = cv::Point2f(matSrc.cols*0.15, matSrc.rows*0.7);  
  105.   
  106.         // 获得仿射变换
  107.         cv::Mat warp_mat_(2, 3, CV_64FC1);  
  108.         warp_mat_ = cv::getAffineTransform(srcTri_, dstTri_);  
  109.   
  110.         // 将dst图像设置为与src相同的类型和大小
  111.         cv::Mat warp_dst_ = cv::Mat::zeros(matSrc.rows, matSrc.cols, matSrc.type());  
  112.         cv::Mat mat_;  
  113.         matSrc.copyTo(mat_);  
  114.   
  115.         // 将刚刚找到的Affine Transform应用到src图像
  116.         cv::warpAffine(mat_, warp_dst_, warp_mat_, warp_dst_.size(), interpolation);  
  117.   
  118.         assert(warp_mat.cols == warp_mat_.cols && warp_mat.rows == warp_mat_.rows);  
  119.         assert(warp_mat.step == warp_mat_.step);  
  120.         for (int y = 0; y < warp_mat.rows; y++) {  
  121.             const fbc::uchar* p = warp_mat.ptr(y);  
  122.             const uchar* p_ = warp_mat_.ptr(y);  
  123.   
  124.             for (int x = 0; x < warp_mat.step; x++) {  
  125.                 assert(p[x] == p_[x]);  
  126.             }  
  127.         }  
  128.     }  
  129.   
  130.     return 0;  
  131. }  
  132.   
  133. int test_warpAffine_float()  
  134. {  
  135.     cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  136.     if (!matSrc.data) {  
  137.         std::cout << "read image fail" << std::endl;  
  138.         return -1;  
  139.     }  
  140.     cv::cvtColor(matSrc, matSrc, CV_BGR2GRAY);  
  141.     matSrc.convertTo(matSrc, CV_32FC1);  
  142.   
  143.     for (int interpolation = 0; interpolation < 5; interpolation++) {  
  144.         fbc::Point2f srcTri[3];  
  145.         fbc::Point2f dstTri[3];  
  146.   
  147.         // 设置你的3点来计算仿射变换
  148.         srcTri[0] = fbc::Point2f(0, 0);  
  149.         srcTri[1] = fbc::Point2f(matSrc.cols - 1, 0);  
  150.         srcTri[2] = fbc::Point2f(0, matSrc.rows - 1);  
  151.   
  152.         dstTri[0] = fbc::Point2f(matSrc.cols*0.0, matSrc.rows*0.33);  
  153.         dstTri[1] = fbc::Point2f(matSrc.cols*0.85, matSrc.rows*0.25);  
  154.         dstTri[2] = fbc::Point2f(matSrc.cols*0.15, matSrc.rows*0.7);  
  155.   
  156.         // 获得仿射变换
  157.         fbc::Mat_<double, 1> warp_mat(2, 3);  
  158.         int ret = fbc::getAffineTransform(srcTri, dstTri, warp_mat);  
  159.         assert(ret == 0);  
  160.   
  161.         fbc::Mat_<float, 1> mat(matSrc.rows, matSrc.cols, matSrc.data);  
  162.         fbc::Mat_<float, 1> warp_dst;  
  163.         warp_dst.zeros(mat.rows, mat.cols);  
  164.   
  165.         fbc::warpAffine(mat, warp_dst, warp_mat, interpolation);  
  166.   
  167.         cv::Point2f srcTri_[3];  
  168.         cv::Point2f dstTri_[3];  
  169.   
  170.         // 设置你的3点来计算仿射变换
  171.         srcTri_[0] = cv::Point2f(0, 0);  
  172.         srcTri_[1] = cv::Point2f(matSrc.cols - 1, 0);  
  173.         srcTri_[2] = cv::Point2f(0, matSrc.rows - 1);  
  174.   
  175.         dstTri_[0] = cv::Point2f(matSrc.cols*0.0, matSrc.rows*0.33);  
  176.         dstTri_[1] = cv::Point2f(matSrc.cols*0.85, matSrc.rows*0.25);  
  177.         dstTri_[2] = cv::Point2f(matSrc.cols*0.15, matSrc.rows*0.7);  
  178.   
  179.         // 获得仿射变换
  180.         cv::Mat warp_mat_(2, 3, CV_64FC1);  
  181.         warp_mat_ = cv::getAffineTransform(srcTri_, dstTri_);  
  182.   
  183.         // 将dst图像设置为与src相同的类型和大小
  184.         cv::Mat warp_dst_ = cv::Mat::zeros(matSrc.rows, matSrc.cols, matSrc.type());  
  185.         cv::Mat mat_;  
  186.         matSrc.copyTo(mat_);  
  187.   
  188.         // 将刚刚找到的Affine Transform应用到src图像
  189.         cv::warpAffine(mat_, warp_dst_, warp_mat_, warp_dst_.size(), interpolation);  
  190.   
  191.         assert(warp_mat.cols == warp_mat_.cols && warp_mat.rows == warp_mat_.rows);  
  192.         assert(warp_mat.step == warp_mat_.step);  
  193.         for (int y = 0; y < warp_mat.rows; y++) {  
  194.             const fbc::uchar* p = warp_mat.ptr(y);  
  195.             const uchar* p_ = warp_mat_.ptr(y);  
  196.   
  197.             for (int x = 0; x < warp_mat.step; x++) {  
  198.                 assert(p[x] == p_[x]);  
  199.             }  
  200.         }  
  201.     }  
  202.   
  203.     return 0;  
  204. }  

 

 

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

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