aihot  2017-05-22 18:45:49  OpenCV |   查看评论   

测试代码test_remap.cpp:
  1. #include "test_remap.hpp"  
  2. #include <assert.h>  
  3. #include <opencv2/opencv.hpp>  
  4. #include <remap.hpp>  
  5.   
  6. static void update_map(const cv::Mat& src, cv::Mat& map_x, cv::Mat& map_y, int ind_)  
  7. {  
  8.     int ind = ind_ % 4;  
  9.   
  10.     for (int j = 0; j < src.rows; j++) {  
  11.         for (int i = 0; i < src.cols; i++) {  
  12.             switch (ind) {  
  13.                 case 0:  
  14.                     if (i > src.cols*0.25 && i < src.cols*0.75 && j > src.rows*0.25 && j < src.rows*0.75) {  
  15.                         map_x.at<float>(j, i) = 2 * (i - src.cols*0.25) + 0.5;  
  16.                         map_y.at<float>(j, i) = 2 * (j - src.rows*0.25) + 0.5;  
  17.                     } else {  
  18.                         map_x.at<float>(j, i) = 0;  
  19.                         map_y.at<float>(j, i) = 0;  
  20.                     }  
  21.                     break;  
  22.                 case 1:  
  23.                     map_x.at<float>(j, i) = i;  
  24.                     map_y.at<float>(j, i) = src.rows - j;  
  25.                     break;  
  26.                 case 2:  
  27.                     map_x.at<float>(j, i) = src.cols - i;  
  28.                     map_y.at<float>(j, i) = j;  
  29.                     break;  
  30.                 case 3:  
  31.                     map_x.at<float>(j, i) = src.cols - i;  
  32.                     map_y.at<float>(j, i) = src.rows - j;  
  33.                     break;  
  34.             } // 开关结束
  35.         }  
  36.     }  
  37. }  
  38.   
  39. int test_remap_uchar()  
  40. {  
  41.     cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  42.     if (!matSrc.data) {  
  43.         std::cout << "read image fail" << std::endl;  
  44.         return -1;  
  45.     }  
  46.   
  47.     for (int ind = 0; ind < 4; ind++) {  
  48.         for (int interpolation = 0; interpolation < 5; interpolation++) {  
  49.             for (int borderType = 0; borderType < 6; borderType++) {  
  50.                 cv::Mat map_x, map_y;  
  51.                 map_x.create(matSrc.size(), CV_32FC1);  
  52.                 map_y.create(matSrc.size(), CV_32FC1);  
  53.   
  54.                 update_map(matSrc, map_x, map_y, ind);  
  55.   
  56.                 int width = matSrc.cols;  
  57.                 int height = matSrc.rows;  
  58.   
  59.                 fbc::Mat_<fbc::uchar, 3> mat1(height, width, matSrc.data);  
  60.                 fbc::Mat_<float, 1> mapX(height, width, map_x.data);  
  61.                 fbc::Mat_<float, 1> mapY(height, width, map_y.data);  
  62.                 fbc::Mat_<fbc::uchar, 3> mat2(height, width);  
  63.   
  64.                 fbc::remap(mat1, mat2, mapX, mapY, interpolation, borderType, fbc::Scalar::all(0));  
  65.   
  66.                 cv::Mat mat2_ = cv::Mat(height, width, CV_8UC3);  
  67.                 cv::remap(matSrc, mat2_, map_x, map_y, interpolation, borderType, cv::Scalar::all(0));  
  68.   
  69.                 assert(mat2.step == mat2_.step);  
  70.                 for (int y = 0; y < mat2.rows; y++) {  
  71.                     const fbc::uchar* p = mat2.ptr(y);  
  72.                     const uchar* p_ = mat2_.ptr(y);  
  73.   
  74.                     for (int x = 0; x < mat2.step; x++) {  
  75.                         assert(p[x] == p_[x]);  
  76.                     }  
  77.                 }  
  78.             }  
  79.         }  
  80.     }  
  81.   
  82.     return 0;  
  83. }  
  84.   
  85. int test_remap_float()  
  86. {  
  87.     cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  88.     if (!matSrc.data) {  
  89.         std::cout << "read image fail" << std::endl;  
  90.         return -1;  
  91.     }  
  92.     matSrc.convertTo(matSrc, CV_32FC3);  
  93.   
  94.     for (int ind = 0; ind < 4; ind++) {  
  95.         for (int interpolation = 0; interpolation < 5; interpolation++) {  
  96.             for (int borderType = 0; borderType < 6; borderType++) {  
  97.                 cv::Mat map_x, map_y;  
  98.                 map_x.create(matSrc.size(), CV_32FC1);  
  99.                 map_y.create(matSrc.size(), CV_32FC1);  
  100.   
  101.                 update_map(matSrc, map_x, map_y, ind);  
  102.   
  103.                 int width = matSrc.cols;  
  104.                 int height = matSrc.rows;  
  105.   
  106.                 fbc::Mat_<float, 3> mat1(height, width, matSrc.data);  
  107.                 fbc::Mat_<float, 1> mapX(height, width, map_x.data);  
  108.                 fbc::Mat_<float, 1> mapY(height, width, map_y.data);  
  109.                 fbc::Mat_<float, 3> mat2(height, width);  
  110.   
  111.                 fbc::remap(mat1, mat2, mapX, mapY, interpolation, borderType, fbc::Scalar::all(0));  
  112.   
  113.                 cv::Mat mat2_ = cv::Mat(height, width, CV_32FC3);  
  114.                 cv::remap(matSrc, mat2_, map_x, map_y, interpolation, borderType, cv::Scalar::all(0));  
  115.   
  116.                 assert(mat2.step == mat2_.step);  
  117.                 for (int y = 0; y < mat2.rows; y++) {  
  118.                     const fbc::uchar* p = mat2.ptr(y);  
  119.                     const uchar* p_ = mat2_.ptr(y);  
  120.   
  121.                     for (int x = 0; x < mat2.step; x++) {  
  122.                         assert(p[x] == p_[x]);  
  123.                     }  
  124.                 }  
  125.             }  
  126.         }  
  127.     }  
  128.   
  129.     return 0;  
  130. }  

 

 

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

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