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

测试代码test_erode.cpp:

  1. #include "test_erode.hpp"  
  2. #include <assert.h>  
  3.   
  4. #include <erode.hpp>  
  5. #include <opencv2/opencv.hpp>  
  6.   
  7. int test_erode_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.   
  15.     int width = matSrc.cols;  
  16.     int height = matSrc.rows;  
  17.   
  18.     for (int elem = 0; elem < 3; elem++) {  
  19.         for (int size = 0; size < 10; size++) {  
  20.             for (int iterations = 1; iterations < 5; iterations++) {  
  21.                 int type;  
  22.                 if (elem == 0){ type = fbc::MORPH_RECT; }  
  23.                 else if (elem == 1){ type = fbc::MORPH_CROSS; }  
  24.                 else if (elem == 2) { type = fbc::MORPH_ELLIPSE; }  
  25.   
  26.                 fbc::Mat_<uchar, 1> element(2 * size + 1, 2 * size + 1);  
  27.                 fbc::getStructuringElement(element, type, fbc::Size(2 * size + 1, 2 * size + 1), fbc::Point(size, size));  
  28.   
  29.                 int type_;  
  30.                 if (elem == 0){ type_ = cv::MORPH_RECT; }  
  31.                 else if (elem == 1){ type_ = cv::MORPH_CROSS; }  
  32.                 else if (elem == 2) { type_ = cv::MORPH_ELLIPSE; }  
  33.   
  34.                 cv::Mat element_ = cv::getStructuringElement(type_, cv::Size(2 * size + 1, 2 * size + 1), cv::Point(size, size));  
  35.   
  36.                 assert(element.rows == element_.rows && element.cols == element.cols && element.step == element_.step);  
  37.                 for (int y = 0; y < element.rows; y++) {  
  38.                     const fbc::uchar* p1 = element.ptr(y);  
  39.                     const uchar* p2 = element_.ptr(y);  
  40.   
  41.                     for (int x = 0; x < element.step; x++) {  
  42.                         assert(p1[x] == p2[x]);  
  43.                     }  
  44.                 }  
  45.   
  46.                 fbc::Mat3BGR mat1(height, width, matSrc.data);  
  47.                 fbc::Mat3BGR mat2(height, width);  
  48.                 fbc::erode(mat1, mat2, element, fbc::Point(-1, -1), iterations, 0, fbc::Scalar::all(128));  
  49.   
  50.                 cv::Mat mat1_(height, width, CV_8UC3, matSrc.data);  
  51.                 cv::Mat mat2_;  
  52.                 cv::erode(mat1_, mat2_, element_, cv::Point(-1, -1), iterations, 0, cv::Scalar::all(128));  
  53.   
  54.                 assert(mat2.rows == mat2_.rows && mat2.cols == mat2_.cols && mat2.step == mat2_.step);  
  55.                 for (int y = 0; y < mat2.rows; y++) {  
  56.                     const fbc::uchar* p1 = mat2.ptr(y);  
  57.                     const uchar* p2 = mat2_.ptr(y);  
  58.   
  59.                     for (int x = 0; x < mat2.step; x++) {  
  60.                         assert(p1[x] == p2[x]);  
  61.                     }  
  62.                 }  
  63.             }  
  64.         }  
  65.     }  
  66.   
  67.     return 0;  
  68. }  
  69.   
  70. int test_erode_float()  
  71. {  
  72.     cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  73.     if (!matSrc.data) {  
  74.         std::cout << "read image fail" << std::endl;  
  75.         return -1;  
  76.     }  
  77.     cv::cvtColor(matSrc, matSrc, CV_BGR2GRAY);  
  78.     matSrc.convertTo(matSrc, CV_32FC1);  
  79.   
  80.     int width = matSrc.cols;  
  81.     int height = matSrc.rows;  
  82.   
  83.     for (int elem = 0; elem < 3; elem++) {  
  84.         for (int size = 0; size < 10; size++) {  
  85.             for (int iterations = 1; iterations < 5; iterations++) {  
  86.                 int type;  
  87.                 if (elem == 0){ type = fbc::MORPH_RECT; }  
  88.                 else if (elem == 1){ type = fbc::MORPH_CROSS; }  
  89.                 else if (elem == 2) { type = fbc::MORPH_ELLIPSE; }  
  90.   
  91.                 fbc::Mat_<uchar, 1> element(2 * size + 1, 2 * size + 1);  
  92.                 fbc::getStructuringElement(element, type, fbc::Size(2 * size + 1, 2 * size + 1), fbc::Point(size, size));  
  93.   
  94.                 int type_;  
  95.                 if (elem == 0){ type_ = cv::MORPH_RECT; }  
  96.                 else if (elem == 1){ type_ = cv::MORPH_CROSS; }  
  97.                 else if (elem == 2) { type_ = cv::MORPH_ELLIPSE; }  
  98.   
  99.                 cv::Mat element_ = cv::getStructuringElement(type_, cv::Size(2 * size + 1, 2 * size + 1), cv::Point(size, size));  
  100.   
  101.                 assert(element.rows == element_.rows && element.cols == element.cols && element.step == element_.step);  
  102.                 for (int y = 0; y < element.rows; y++) {  
  103.                     const fbc::uchar* p1 = element.ptr(y);  
  104.                     const uchar* p2 = element_.ptr(y);  
  105.   
  106.                     for (int x = 0; x < element.step; x++) {  
  107.                         assert(p1[x] == p2[x]);  
  108.                     }  
  109.                 }  
  110.   
  111.                 fbc::Mat_<float, 1> mat1(height, width, matSrc.data);  
  112.                 fbc::Mat_<float, 1> mat2(height, width);  
  113.                 fbc::erode(mat1, mat2, element, fbc::Point(-1, -1), iterations, 0, fbc::Scalar::all(128));  
  114.   
  115.                 cv::Mat mat1_(height, width, CV_32FC1, matSrc.data);  
  116.                 cv::Mat mat2_;  
  117.                 cv::erode(mat1_, mat2_, element_, cv::Point(-1, -1), iterations, 0, cv::Scalar::all(128));  
  118.   
  119.                 assert(mat2.rows == mat2_.rows && mat2.cols == mat2_.cols && mat2.step == mat2_.step);  
  120.                 for (int y = 0; y < mat2.rows; y++) {  
  121.                     const fbc::uchar* p1 = mat2.ptr(y);  
  122.                     const uchar* p2 = mat2_.ptr(y);  
  123.   
  124.                     for (int x = 0; x < mat2.step; x++) {  
  125.                         assert(p1[x] == p2[x]);  
  126.                     }  
  127.                 }  
  128.             }  
  129.         }  
  130.     }  
  131.   
  132.     return 0;  
  133. }  
 

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

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