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

测试代码test_dilate.cpp:

  1. #include "test_dilate.hpp"  
  2. #include <assert.h>  
  3.   
  4. #include <dilate.hpp>  
  5. #include <opencv2/opencv.hpp>  
  6.   
  7. int test_getStructuringElement()  
  8. {  
  9.     for (int elem = 0; elem < 3; elem++) {  
  10.         for (int size = 0; size < 10; size++) {  
  11.             int type;  
  12.             if (elem == 0){ type = fbc::MORPH_RECT; }  
  13.             else if (elem == 1){ type = fbc::MORPH_CROSS; }  
  14.             else if (elem == 2) { type = fbc::MORPH_ELLIPSE; }  
  15.   
  16.             fbc::Mat_<uchar, 1> element(2*size+1, 2*size+1);  
  17.             fbc::getStructuringElement(element, type, fbc::Size(2 * size + 1, 2 * size + 1), fbc::Point(size, size));  
  18.   
  19.             int type_;  
  20.             if (elem == 0){ type_ = cv::MORPH_RECT; }  
  21.             else if (elem == 1){ type_ = cv::MORPH_CROSS; }  
  22.             else if (elem == 2) { type_ = cv::MORPH_ELLIPSE; }  
  23.   
  24.             cv::Mat element_ = cv::getStructuringElement(type_, cv::Size(2 * size + 1, 2 * size + 1), cv::Point(size, size));  
  25.   
  26.             assert(element.rows == element_.rows && element.cols == element.cols && element.step == element_.step);  
  27.             for (int y = 0; y < element.rows; y++) {  
  28.                 const fbc::uchar* p1 = element.ptr(y);  
  29.                 const uchar* p2 = element_.ptr(y);  
  30.   
  31.                 for (int x = 0; x < element.step; x++) {  
  32.                     assert(p1[x] == p2[x]);  
  33.                     if (size == 5)  
  34.                         fprintf(stderr, "%d  ", p1[x]);  
  35.                 }  
  36.                 if (size == 5)  
  37.                     fprintf(stderr, "\n");  
  38.             }  
  39.   
  40.             if (size == 5)  
  41.                 fprintf(stderr, "\n");  
  42.         }  
  43.     }  
  44.   
  45.     return 0;  
  46. }  
  47.   
  48. int test_dilate_uchar()  
  49. {  
  50.     cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  51.     if (!matSrc.data) {  
  52.         std::cout << "read image fail" << std::endl;  
  53.         return -1;  
  54.     }  
  55.   
  56.     int width = matSrc.cols;  
  57.     int height = matSrc.rows;  
  58.   
  59.     for (int elem = 0; elem < 3; elem++) {  
  60.         for (int size = 0; size < 10; size++) {  
  61.             for (int iterations = 1; iterations < 5; iterations++) {  
  62.                 int type;  
  63.                 if (elem == 0){ type = fbc::MORPH_RECT; }  
  64.                 else if (elem == 1){ type = fbc::MORPH_CROSS; }  
  65.                 else if (elem == 2) { type = fbc::MORPH_ELLIPSE; }  
  66.   
  67.                 fbc::Mat_<uchar, 1> element(2 * size + 1, 2 * size + 1);  
  68.                 fbc::getStructuringElement(element, type, fbc::Size(2 * size + 1, 2 * size + 1), fbc::Point(size, size));  
  69.   
  70.                 int type_;  
  71.                 if (elem == 0){ type_ = cv::MORPH_RECT; }  
  72.                 else if (elem == 1){ type_ = cv::MORPH_CROSS; }  
  73.                 else if (elem == 2) { type_ = cv::MORPH_ELLIPSE; }  
  74.   
  75.                 cv::Mat element_ = cv::getStructuringElement(type_, cv::Size(2 * size + 1, 2 * size + 1), cv::Point(size, size));  
  76.   
  77.                 assert(element.rows == element_.rows && element.cols == element.cols && element.step == element_.step);  
  78.                 for (int y = 0; y < element.rows; y++) {  
  79.                     const fbc::uchar* p1 = element.ptr(y);  
  80.                     const uchar* p2 = element_.ptr(y);  
  81.   
  82.                     for (int x = 0; x < element.step; x++) {  
  83.                         assert(p1[x] == p2[x]);  
  84.                     }  
  85.                 }  
  86.   
  87.                 fbc::Mat3BGR mat1(height, width, matSrc.data);  
  88.                 fbc::Mat3BGR mat2(height, width);  
  89.                 fbc::dilate(mat1, mat2, element, fbc::Point(-1, -1), iterations, 0, fbc::Scalar::all(128));  
  90.   
  91.                 cv::Mat mat1_(height, width, CV_8UC3, matSrc.data);  
  92.                 cv::Mat mat2_;  
  93.                 cv::dilate(mat1_, mat2_, element_, cv::Point(-1, -1), iterations, 0, cv::Scalar::all(128));  
  94.   
  95.                 assert(mat2.rows == mat2_.rows && mat2.cols == mat2_.cols && mat2.step == mat2_.step);  
  96.                 for (int y = 0; y < mat2.rows; y++) {  
  97.                     const fbc::uchar* p1 = mat2.ptr(y);  
  98.                     const uchar* p2 = mat2_.ptr(y);  
  99.   
  100.                     for (int x = 0; x < mat2.step; x++) {  
  101.                         assert(p1[x] == p2[x]);  
  102.                     }  
  103.                 }  
  104.             }  
  105.         }  
  106.     }  
  107.   
  108.     return 0;  
  109. }  
  110.   
  111. int test_dilate_float()  
  112. {  
  113.     cv::Mat matSrc = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  114.     if (!matSrc.data) {  
  115.         std::cout << "read image fail" << std::endl;  
  116.         return -1;  
  117.     }  
  118.     cv::cvtColor(matSrc, matSrc, CV_BGR2GRAY);  
  119.     matSrc.convertTo(matSrc, CV_32FC1);  
  120.   
  121.     int width = matSrc.cols;  
  122.     int height = matSrc.rows;  
  123.   
  124.     for (int elem = 0; elem < 3; elem++) {  
  125.         for (int size = 0; size < 10; size++) {  
  126.             for (int iterations = 1; iterations < 5; iterations++) {  
  127.                 int type;  
  128.                 if (elem == 0){ type = fbc::MORPH_RECT; }  
  129.                 else if (elem == 1){ type = fbc::MORPH_CROSS; }  
  130.                 else if (elem == 2) { type = fbc::MORPH_ELLIPSE; }  
  131.   
  132.                 fbc::Mat_<uchar, 1> element(2 * size + 1, 2 * size + 1);  
  133.                 fbc::getStructuringElement(element, type, fbc::Size(2 * size + 1, 2 * size + 1), fbc::Point(size, size));  
  134.   
  135.                 int type_;  
  136.                 if (elem == 0){ type_ = cv::MORPH_RECT; }  
  137.                 else if (elem == 1){ type_ = cv::MORPH_CROSS; }  
  138.                 else if (elem == 2) { type_ = cv::MORPH_ELLIPSE; }  
  139.   
  140.                 cv::Mat element_ = cv::getStructuringElement(type_, cv::Size(2 * size + 1, 2 * size + 1), cv::Point(size, size));  
  141.   
  142.                 assert(element.rows == element_.rows && element.cols == element.cols && element.step == element_.step);  
  143.                 for (int y = 0; y < element.rows; y++) {  
  144.                     const fbc::uchar* p1 = element.ptr(y);  
  145.                     const uchar* p2 = element_.ptr(y);  
  146.   
  147.                     for (int x = 0; x < element.step; x++) {  
  148.                         assert(p1[x] == p2[x]);  
  149.                     }  
  150.                 }  
  151.   
  152.                 fbc::Mat_<float, 1> mat1(height, width, matSrc.data);  
  153.                 fbc::Mat_<float, 1> mat2(height, width);  
  154.                 fbc::dilate(mat1, mat2, element, fbc::Point(-1, -1), iterations, 0, fbc::Scalar::all(128));  
  155.   
  156.                 cv::Mat mat1_(height, width, CV_32FC1, matSrc.data);  
  157.                 cv::Mat mat2_;  
  158.                 cv::dilate(mat1_, mat2_, element_, cv::Point(-1, -1), iterations, 0, cv::Scalar::all(128));  
  159.   
  160.                 assert(mat2.rows == mat2_.rows && mat2.cols == mat2_.cols && mat2.step == mat2_.step);  
  161.                 for (int y = 0; y < mat2.rows; y++) {  
  162.                     const fbc::uchar* p1 = mat2.ptr(y);  
  163.                     const uchar* p2 = mat2_.ptr(y);  
  164.   
  165.                     for (int x = 0; x < mat2.step; x++) {  
  166.                         assert(p1[x] == p2[x]);  
  167.                     }  
  168.                 }  
  169.             }  
  170.         }  
  171.     }  
  172.   
  173.     return 0;  
  174. }  
 

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

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