aihot  2017-05-22 21:02:56  OpenCV |   查看评论   

split测试代码:

  1. #include <assert.h>  
  2. #include <vector>  
  3. #include <core/mat.hpp>  
  4. #include <split.hpp>  
  5.   
  6. #include <opencv2/opencv.hpp>  
  7.   
  8. #include "test_split.hpp"  
  9.   
  10. int test_split_uchar()  
  11. {  
  12.     cv::Mat mat = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  13.     if (!mat.data) {  
  14.         std::cout << "read image fail" << std::endl;  
  15.         return -1;  
  16.     }  
  17.     //cv::cvtColor(mat, mat, CV_BGR2GRAY);  
  18.   
  19.     int chs = mat.channels();  
  20.     int width = mat.cols;  
  21.     int height = mat.rows;  
  22.   
  23.     fbc::Mat_<fbc::uchar, 3> mat1(height, width, mat.data);  
  24.     std::vector<fbc::Mat_<fbc::uchar, 1>> vecMat2;  
  25.     fbc::Mat_<fbc::uchar, 1>* mat2 = new fbc::Mat_<fbc::uchar, 1>[chs];  
  26.     for (int i = 0; i < chs; i++) {  
  27.         mat2[i] = fbc::Mat_<fbc::uchar, 1>(height, width);  
  28.         vecMat2.push_back(mat2[i]);  
  29.     }  
  30.   
  31.     fbc::split(mat1, vecMat2);  
  32.   
  33.     cv::Mat mat1_(height, width, CV_8UC3, mat.data);  
  34.     std::vector<cv::Mat> vecMat2_;  
  35.     cv::split(mat1_, vecMat2_);  
  36.   
  37.     assert(vecMat2.size() == vecMat2_.size());  
  38.     for (int i = 0; i < vecMat2.size(); i++) {  
  39.         assert(vecMat2[i].rows == vecMat2_[i].rows && vecMat2[i].cols == vecMat2_[i].cols);  
  40.         assert(vecMat2[i].step == vecMat2_[i].step);  
  41.         assert(vecMat2[i].channels == vecMat2_[i].channels());  
  42.   
  43.         for (int y = 0; y < vecMat2[i].rows; y++) {  
  44.             const fbc::uchar* p = vecMat2[i].ptr(y);  
  45.             const uchar* p_ = vecMat2_[i].ptr(y);  
  46.   
  47.             for (int x = 0; x < vecMat2[i].step; x++) {  
  48.                 assert(p[x] == p_[x]);  
  49.             }  
  50.         }  
  51.     }  
  52.   
  53.     delete[] mat2;  
  54.   
  55.     return 0;  
  56. }  
  57.   
  58. int test_split_float()  
  59. {  
  60.     cv::Mat mat = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  61.     if (!mat.data) {  
  62.         std::cout << "read image fail" << std::endl;  
  63.         return -1;  
  64.     }  
  65.     mat.convertTo(mat, CV_32FC3);  
  66.     //cv::cvtColor(mat, mat, CV_BGR2GRAY);  
  67.   
  68.     int chs = mat.channels();  
  69.     int width = mat.cols;  
  70.     int height = mat.rows;  
  71.   
  72.     fbc::Mat_<float, 3> mat1(height, width, mat.data);  
  73.     std::vector<fbc::Mat_<float, 1>> vecMat2;  
  74.     fbc::Mat_<float, 1>* mat2 = new fbc::Mat_<float, 1>[chs];  
  75.     for (int i = 0; i < chs; i++) {  
  76.         mat2[i] = fbc::Mat_<float, 1>(height, width);  
  77.         vecMat2.push_back(mat2[i]);  
  78.     }  
  79.   
  80.     fbc::split(mat1, vecMat2);  
  81.   
  82.     cv::Mat mat1_(height, width, CV_32FC3, mat.data);  
  83.     std::vector<cv::Mat> vecMat2_;  
  84.     cv::split(mat1_, vecMat2_);  
  85.   
  86.     assert(vecMat2.size() == vecMat2_.size());  
  87.     for (int i = 0; i < vecMat2.size(); i++) {  
  88.         assert(vecMat2[i].rows == vecMat2_[i].rows && vecMat2[i].cols == vecMat2_[i].cols);  
  89.         assert(vecMat2[i].step == vecMat2_[i].step);  
  90.         assert(vecMat2[i].channels == vecMat2_[i].channels());  
  91.   
  92.         for (int y = 0; y < vecMat2[i].rows; y++) {  
  93.             const fbc::uchar* p = vecMat2[i].ptr(y);  
  94.             const uchar* p_ = vecMat2_[i].ptr(y);  
  95.   
  96.             for (int x = 0; x < vecMat2[i].step; x++) {  
  97.                 assert(p[x] == p_[x]);  
  98.             }  
  99.         }  
  100.     }  
  101.   
  102.     delete[] mat2;  
  103.   
  104.     return 0;  
  105. }  
  106.  
 

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

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