aihot  2017-05-19 23:28:33  OpenCV |   查看评论   

测试代码test_warpPerspective.cpp:

 

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

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

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