aihot  2017-05-18 19:17:53  OpenCV |   查看评论   

测试代码test_threshold.cpp:

 
  1. #include "test_threshold.hpp"  
  2. #include <assert.h>  
  3.   
  4. #include <threshold.hpp>  
  5. #include <opencv2/opencv.hpp>  
  6.   
  7. int test_threshold_uchar()  
  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.     cv::cvtColor(matSrc, matSrc, CV_BGR2GRAY);  
  15.   
  16.     int width = matSrc.cols;  
  17.     int height = matSrc.rows;  
  18.     int types[8] = {0, 1, 2, 3, 4, 7, 8, 16};  
  19.   
  20.     for (int i = 0; i < 8; i++) {  
  21.         if (types[i] == 7) continue;  
  22.         double thresh = 135.0;  
  23.         double maxval = 255.0;  
  24.   
  25.         fbc::Mat_<uchar, 1> mat1(height, width, matSrc.data);  
  26.         fbc::Mat_<uchar, 1> mat2(height, width);  
  27.         fbc::threshold(mat1, mat2, thresh, maxval, types[i]);  
  28.   
  29.         cv::Mat mat1_(height, width, CV_8UC1, matSrc.data);  
  30.         cv::Mat mat2_;  
  31.         cv::threshold(mat1_, mat2_, thresh, maxval, types[i]);  
  32.   
  33.         assert(mat2.rows == mat2_.rows && mat2.cols == mat2_.cols && mat2.step == mat2_.step);  
  34.         for (int y = 0; y < mat2.rows; y++) {  
  35.             const fbc::uchar* p1 = mat2.ptr(y);  
  36.             const uchar* p2 = mat2_.ptr(y);  
  37.   
  38.             for (int x = 0; x < mat2.step; x++) {  
  39.                 assert(p1[x] == p2[x]);  
  40.             }  
  41.         }  
  42.     }  
  43.   
  44.     return 0;  
  45. }  
  46.   
  47. int test_threshold_float()  
  48. {  
  49.     cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  50.     if (!matSrc.data) {  
  51.         std::cout << "read image fail" << std::endl;  
  52.         return -1;  
  53.     }  
  54.     cv::cvtColor(matSrc, matSrc, CV_BGR2GRAY);  
  55.     matSrc.convertTo(matSrc, CV_32FC1);  
  56.   
  57.     int width = matSrc.cols;  
  58.     int height = matSrc.rows;  
  59.     int types[6] = { 0, 1, 2, 3, 4, 7 };  
  60.   
  61.     for (int i = 0; i < 6; i++) {  
  62.         if (types[i] == 7) continue;  
  63.         double thresh = 135.0;  
  64.         double maxval = 255.0;  
  65.   
  66.         fbc::Mat_<float, 1> mat1(height, width, matSrc.data);  
  67.         fbc::Mat_<float, 1> mat2(height, width);  
  68.         fbc::threshold(mat1, mat2, thresh, maxval, types[i]);  
  69.   
  70.         cv::Mat mat1_(height, width, CV_32FC1, matSrc.data);  
  71.         cv::Mat mat2_;  
  72.         cv::threshold(mat1_, mat2_, thresh, maxval, types[i]);  
  73.   
  74.         assert(mat2.rows == mat2_.rows && mat2.cols == mat2_.cols && mat2.step == mat2_.step);  
  75.         for (int y = 0; y < mat2.rows; y++) {  
  76.             const fbc::uchar* p1 = mat2.ptr(y);  
  77.             const uchar* p2 = mat2_.ptr(y);  
  78.   
  79.             for (int x = 0; x < mat2.step; x++) {  
  80.                 assert(p1[x] == p2[x]);  
  81.             }  
  82.         }  
  83.     }  
  84.   
  85.     return 0;  
  86. }  
 

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

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