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

测试代码test_morphologyEx.cpp:

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

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

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