aihot  2017-05-23 23:26:42  云计算 |   查看评论   
测试代码test_cvtColor.cpp:
  1. #include <assert.h>  
  2. #include <core/mat.hpp>  
  3. #include <cvtColor.hpp>  
  4.   
  5. #include <opencv2/opencv.hpp>  
  6.   
  7. #include "test_cvtColor.hpp"  
  8.   
  9.   
  10. int test_cvtColor_RGB2RGB()  
  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.   
  18.     int width = mat.cols;  
  19.     int height = mat.rows;  
  20.   
  21.     // uchar  
  22.     fbc::Mat3BGR mat1(height, width, mat.data);  
  23.     fbc::Mat3BGR mat2(mat1);  
  24.     fbc::Mat_<uchar, 4> mat3(height, width);  
  25.     fbc::cvtColor(mat2, mat3, fbc::CV_BGR2BGRA);  
  26.   
  27.     cv::Mat mat1_(height, width, CV_8UC3, mat.data);  
  28.     cv::Mat mat2_;  
  29.     mat1_.copyTo(mat2_);  
  30.     cv::Mat mat3_(height, width, CV_8UC4);  
  31.     cv::cvtColor(mat2_, mat3_, CV_BGR2BGRA);  
  32.   
  33.     assert(mat3.step == mat3_.step);  
  34.     for (int y = 0; y < mat3.rows; y++) {  
  35.         const fbc::uchar* p = mat3.ptr(y);  
  36.         const uchar* p_ = mat3_.ptr(y);  
  37.   
  38.         for (int x = 0; x < mat3.step; x++) {  
  39.             assert(p[x] == p_[x]);  
  40.         }  
  41.     }  
  42.   
  43.     // float  
  44.     cv::Mat matf;  
  45.     mat.convertTo(matf, CV_32FC3);  
  46.   
  47.     fbc::Mat_<float, 3> mat4(height, width, matf.data);  
  48.     fbc::Mat_<float, 3> mat5(mat4);  
  49.     fbc::Mat_<float, 4> mat6(height, width);  
  50.     fbc::cvtColor(mat5, mat6, fbc::CV_BGR2BGRA);  
  51.   
  52.     cv::Mat mat4_(height, width, CV_32FC3, matf.data);  
  53.     cv::Mat mat5_;  
  54.     mat4_.copyTo(mat5_);  
  55.     cv::Mat mat6_(height, width, CV_32FC4);  
  56.     cv::cvtColor(mat5_, mat6_, CV_BGR2BGRA);  
  57.   
  58.     assert(mat6.step == mat6_.step);  
  59.     for (int y = 0; y < mat6.rows; y++) {  
  60.         const fbc::uchar* p = mat6.ptr(y);  
  61.         const uchar* p_ = mat6_.ptr(y);  
  62.   
  63.         for (int x = 0; x < mat6.step; x++) {  
  64.             assert(p[x] == p_[x]);  
  65.         }  
  66.     }  
  67.   
  68.     return 0;  
  69. }  
  70.   
  71. int test_cvtColor_RGB2Gray()  
  72. {  
  73.     cv::Mat mat = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  74.     if (!mat.data) {  
  75.         std::cout << "read image fail" << std::endl;  
  76.         return -1;  
  77.     }  
  78.   
  79.     int width = mat.cols;  
  80.     int height = mat.rows;  
  81.   
  82.     // uchar  
  83.     fbc::Mat3BGR mat1(height, width, mat.data);  
  84.     fbc::Mat3BGR mat2(mat1);  
  85.     fbc::Mat1Gray mat3(height, width);  
  86.     fbc::cvtColor(mat2, mat3, fbc::CV_BGR2GRAY);  
  87.   
  88.     cv::Mat mat1_(height, width, CV_8UC3, mat.data);  
  89.     cv::Mat mat2_;  
  90.     mat1_.copyTo(mat2_);  
  91.     cv::Mat mat3_(height, width, CV_8UC1);  
  92.     cv::cvtColor(mat2_, mat3_, CV_BGR2GRAY);  
  93.   
  94.     assert(mat3.step == mat3_.step);  
  95.     for (int y = 0; y < mat3.rows; y++) {  
  96.         const fbc::uchar* p = mat3.ptr(y);  
  97.         const uchar* p_ = mat3_.ptr(y);  
  98.   
  99.         for (int x = 0; x < mat3.step; x++) {  
  100.             assert(p[x] == p_[x]);  
  101.         }  
  102.     }  
  103.   
  104.     // float  
  105.     cv::Mat matf;  
  106.     mat.convertTo(matf, CV_32FC3);  
  107.   
  108.     fbc::Mat_<float, 3> mat4(height, width, matf.data);  
  109.     fbc::Mat_<float, 3> mat5(mat4);  
  110.     fbc::Mat_<float, 1> mat6(height, width);  
  111.     fbc::cvtColor(mat5, mat6, fbc::CV_BGR2GRAY);  
  112.   
  113.     cv::Mat mat4_(height, width, CV_32FC3, matf.data);  
  114.     cv::Mat mat5_;  
  115.     mat4_.copyTo(mat5_);  
  116.     cv::Mat mat6_(height, width, CV_32FC1);  
  117.     cv::cvtColor(mat5_, mat6_, CV_BGR2GRAY);  
  118.   
  119.     assert(mat6.step == mat6_.step);  
  120.     for (int y = 0; y < mat6.rows; y++) {  
  121.         const fbc::uchar* p = mat6.ptr(y);  
  122.         const uchar* p_ = mat6_.ptr(y);  
  123.   
  124.         for (int x = 0; x < mat6.step; x++) {  
  125.             assert(p[x] == p_[x]);  
  126.         }  
  127.     }  
  128.   
  129.     return 0;  
  130. }  
  131.   
  132. int test_cvtColor_Gray2RGB()  
  133. {  
  134.     cv::Mat mat = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  135.     if (!mat.data) {  
  136.         std::cout << "read image fail" << std::endl;  
  137.         return -1;  
  138.     }  
  139.     cv::cvtColor(mat, mat, CV_BGR2GRAY);  
  140.   
  141.     int width = mat.cols;  
  142.     int height = mat.rows;  
  143.   
  144.     // uchar  
  145.     fbc::Mat1Gray mat1(height, width, mat.data);  
  146.     fbc::Mat1Gray mat2(mat1);  
  147.     fbc::Mat3BGR mat3(height, width);  
  148.     fbc::cvtColor(mat2, mat3, fbc::CV_GRAY2BGR);  
  149.   
  150.     cv::Mat mat1_(height, width, CV_8UC1, mat.data);  
  151.     cv::Mat mat2_;  
  152.     mat1_.copyTo(mat2_);  
  153.     cv::Mat mat3_(height, width, CV_8UC3);  
  154.     cv::cvtColor(mat2_, mat3_, CV_GRAY2BGR);  
  155.   
  156.     assert(mat3.step == mat3_.step);  
  157.     for (int y = 0; y < mat3.rows; y++) {  
  158.         const fbc::uchar* p = mat3.ptr(y);  
  159.         const uchar* p_ = mat3_.ptr(y);  
  160.   
  161.         for (int x = 0; x < mat3.step; x++) {  
  162.             assert(p[x] == p_[x]);  
  163.         }  
  164.     }  
  165.   
  166.     // float  
  167.     cv::Mat matf;  
  168.     mat.convertTo(matf, CV_32FC1);  
  169.   
  170.     fbc::Mat_<float, 1> mat4(height, width, matf.data);  
  171.     fbc::Mat_<float, 1> mat5(mat4);  
  172.     fbc::Mat_<float, 3> mat6(height, width);  
  173.     fbc::cvtColor(mat5, mat6, fbc::CV_GRAY2BGR);  
  174.   
  175.     cv::Mat mat4_(height, width, CV_32FC1, matf.data);  
  176.     cv::Mat mat5_;  
  177.     mat4_.copyTo(mat5_);  
  178.     cv::Mat mat6_(height, width, CV_32FC3);  
  179.     cv::cvtColor(mat5_, mat6_, CV_GRAY2BGR);  
  180.   
  181.     assert(mat6.step == mat6_.step);  
  182.     for (int y = 0; y < mat6.rows; y++) {  
  183.         const fbc::uchar* p = mat6.ptr(y);  
  184.         const uchar* p_ = mat6_.ptr(y);  
  185.   
  186.         for (int x = 0; x < mat6.step; x++) {  
  187.             assert(p[x] == p_[x]);  
  188.         }  
  189.     }  
  190.   
  191.     return 0;  
  192. }  
  193.   
  194. int test_cvtColor_RGB2YCrCb()  
  195. {  
  196.     cv::Mat mat = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  197.     if (!mat.data) {  
  198.         std::cout << "read image fail" << std::endl;  
  199.         return -1;  
  200.     }  
  201.   
  202.     int width = mat.cols;  
  203.     int height = mat.rows;  
  204.   
  205.     // uchar  
  206.     fbc::Mat3BGR mat1(height, width, mat.data);  
  207.     fbc::Mat3BGR mat2(mat1);  
  208.     fbc::Mat_<uchar, 3> mat3(height, width);  
  209.     fbc::cvtColor(mat2, mat3, fbc::CV_BGR2YCrCb);  
  210.   
  211.     cv::Mat mat1_(height, width, CV_8UC3, mat.data);  
  212.     cv::Mat mat2_;  
  213.     mat1_.copyTo(mat2_);  
  214.     cv::Mat mat3_(height, width, CV_8UC3);  
  215.     cv::cvtColor(mat2_, mat3_, CV_BGR2YCrCb);  
  216.   
  217.     assert(mat3.step == mat3_.step);  
  218.     for (int y = 0; y < mat3.rows; y++) {  
  219.         const fbc::uchar* p = mat3.ptr(y);  
  220.         const uchar* p_ = mat3_.ptr(y);  
  221.   
  222.         for (int x = 0; x < mat3.step; x++) {  
  223.             assert(p[x] == p_[x]);  
  224.         }  
  225.     }  
  226.   
  227.     // float  
  228.     cv::Mat matf;  
  229.     mat.convertTo(matf, CV_32FC3);  
  230.   
  231.     fbc::Mat_<float, 3> mat4(height, width, matf.data);  
  232.     fbc::Mat_<float, 3> mat5(mat4);  
  233.     fbc::Mat_<float, 3> mat6(height, width);  
  234.     fbc::cvtColor(mat5, mat6, fbc::CV_BGR2YCrCb);  
  235.   
  236.     cv::Mat mat4_(height, width, CV_32FC3, matf.data);  
  237.     cv::Mat mat5_;  
  238.     mat4_.copyTo(mat5_);  
  239.     cv::Mat mat6_(height, width, CV_32FC3);  
  240.     cv::cvtColor(mat5_, mat6_, CV_BGR2YCrCb);  
  241.   
  242.     assert(mat6.step == mat6_.step);  
  243.     for (int y = 0; y < mat6.rows; y++) {  
  244.         const fbc::uchar* p = mat6.ptr(y);  
  245.         const uchar* p_ = mat6_.ptr(y);  
  246.   
  247.         for (int x = 0; x < mat6.step; x++) {  
  248.             assert(p[x] == p_[x]);  
  249.         }  
  250.     }  
  251.   
  252.     return 0;  
  253. }  
  254.   
  255. int test_cvtColor_YCrCb2RGB()  
  256. {  
  257.     cv::Mat mat = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  258.     if (!mat.data) {  
  259.         std::cout << "read image fail" << std::endl;  
  260.         return -1;  
  261.     }  
  262.     cv::cvtColor(mat, mat, CV_BGR2YCrCb);  
  263.   
  264.     int width = mat.cols;  
  265.     int height = mat.rows;  
  266.   
  267.     // uchar  
  268.     fbc::Mat_<uchar, 3> mat1(height, width, mat.data);  
  269.     fbc::Mat_<uchar, 3> mat2(mat1);  
  270.     fbc::Mat3BGR mat3(height, width);  
  271.     fbc::cvtColor(mat2, mat3, fbc::CV_YCrCb2BGR);  
  272.   
  273.     cv::Mat mat1_(height, width, CV_8UC3, mat.data);  
  274.     cv::Mat mat2_;  
  275.     mat1_.copyTo(mat2_);  
  276.     cv::Mat mat3_(height, width, CV_8UC3);  
  277.     cv::cvtColor(mat2_, mat3_, CV_YCrCb2BGR);  
  278.   
  279.     assert(mat3.step == mat3_.step);  
  280.     for (int y = 0; y < mat3.rows; y++) {  
  281.         const fbc::uchar* p = mat3.ptr(y);  
  282.         const uchar* p_ = mat3_.ptr(y);  
  283.   
  284.         for (int x = 0; x < mat3.step; x++) {  
  285.             assert(p[x] == p_[x]);  
  286.         }  
  287.     }  
  288.   
  289.     // float  
  290.     cv::Mat matf;  
  291.     mat.convertTo(matf, CV_32FC3);  
  292.   
  293.     fbc::Mat_<float, 3> mat4(height, width, matf.data);  
  294.     fbc::Mat_<float, 3> mat5(mat4);  
  295.     fbc::Mat_<float, 3> mat6(height, width);  
  296.     fbc::cvtColor(mat5, mat6, fbc::CV_YCrCb2BGR);  
  297.   
  298.     cv::Mat mat4_(height, width, CV_32FC3, matf.data);  
  299.     cv::Mat mat5_;  
  300.     mat4_.copyTo(mat5_);  
  301.     cv::Mat mat6_(height, width, CV_32FC3);  
  302.     cv::cvtColor(mat5_, mat6_, CV_YCrCb2BGR);  
  303.   
  304.     assert(mat6.step == mat6_.step);  
  305.     for (int y = 0; y < mat6.rows; y++) {  
  306.         const fbc::uchar* p = mat6.ptr(y);  
  307.         const uchar* p_ = mat6_.ptr(y);  
  308.   
  309.         for (int x = 0; x < mat6.step; x++) {  
  310.             assert(p[x] == p_[x]);  
  311.         }  
  312.     }  
  313.   
  314.     return 0;  
  315. }  
  316.   
  317. int test_cvtColor_RGB2XYZ()  
  318. {  
  319.     cv::Mat mat = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  320.     if (!mat.data) {  
  321.         std::cout << "read image fail" << std::endl;  
  322.         return -1;  
  323.     }  
  324.   
  325.     int width = mat.cols;  
  326.     int height = mat.rows;  
  327.   
  328.     // uchar  
  329.     fbc::Mat_<uchar, 3> mat1(height, width, mat.data);  
  330.     fbc::Mat_<uchar, 3> mat2(mat1);  
  331.     fbc::Mat_<uchar, 3> mat3(height, width);  
  332.     fbc::cvtColor(mat2, mat3, fbc::CV_BGR2XYZ);  
  333.   
  334.     cv::Mat mat1_(height, width, CV_8UC3, mat.data);  
  335.     cv::Mat mat2_;  
  336.     mat1_.copyTo(mat2_);  
  337.     cv::Mat mat3_(height, width, CV_8UC3);  
  338.     cv::cvtColor(mat2_, mat3_, CV_BGR2XYZ);  
  339.   
  340.     assert(mat3.step == mat3_.step);  
  341.     for (int y = 0; y < mat3.rows; y++) {  
  342.         const fbc::uchar* p = mat3.ptr(y);  
  343.         const uchar* p_ = mat3_.ptr(y);  
  344.   
  345.         for (int x = 0; x < mat3.step; x++) {  
  346.             assert(p[x] == p_[x]);  
  347.         }  
  348.     }  
  349.   
  350.     // float  
  351.     cv::Mat matf;  
  352.     mat.convertTo(matf, CV_32FC3);  
  353.   
  354.     fbc::Mat_<float, 3> mat4(height, width, matf.data);  
  355.     fbc::Mat_<float, 3> mat5(mat4);  
  356.     fbc::Mat_<float, 3> mat6(height, width);  
  357.     fbc::cvtColor(mat5, mat6, fbc::CV_BGR2XYZ);  
  358.   
  359.     cv::Mat mat4_(height, width, CV_32FC3, matf.data);  
  360.     cv::Mat mat5_;  
  361.     mat4_.copyTo(mat5_);  
  362.     cv::Mat mat6_(height, width, CV_32FC3);  
  363.     cv::cvtColor(mat5_, mat6_, CV_BGR2XYZ);  
  364.   
  365.     assert(mat6.step == mat6_.step);  
  366.     for (int y = 0; y < mat6.rows; y++) {  
  367.         const fbc::uchar* p = mat6.ptr(y);  
  368.         const uchar* p_ = mat6_.ptr(y);  
  369.   
  370.         for (int x = 0; x < mat6.step; x++) {  
  371.             assert(p[x] == p_[x]);  
  372.         }  
  373.     }  
  374.   
  375.     return 0;  
  376. }  
  377.   
  378. int test_cvtColor_XYZ2RGB()  
  379. {  
  380.     cv::Mat mat = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  381.     if (!mat.data) {  
  382.         std::cout << "read image fail" << std::endl;  
  383.         return -1;  
  384.     }  
  385.     cv::cvtColor(mat, mat, CV_BGR2XYZ);  
  386.   
  387.     int width = mat.cols;  
  388.     int height = mat.rows;  
  389.   
  390.     // uchar  
  391.     fbc::Mat_<uchar, 3> mat1(height, width, mat.data);  
  392.     fbc::Mat_<uchar, 3> mat2(mat1);  
  393.     fbc::Mat_<uchar, 3> mat3(height, width);  
  394.     fbc::cvtColor(mat2, mat3, fbc::CV_XYZ2BGR);  
  395.   
  396.     cv::Mat mat1_(height, width, CV_8UC3, mat.data);  
  397.     cv::Mat mat2_;  
  398.     mat1_.copyTo(mat2_);  
  399.     cv::Mat mat3_(height, width, CV_8UC3);  
  400.     cv::cvtColor(mat2_, mat3_, CV_XYZ2BGR);  
  401.   
  402.     assert(mat3.step == mat3_.step);  
  403.     for (int y = 0; y < mat3.rows; y++) {  
  404.         const fbc::uchar* p = mat3.ptr(y);  
  405.         const uchar* p_ = mat3_.ptr(y);  
  406.   
  407.         for (int x = 0; x < mat3.step; x++) {  
  408.             assert(p[x] == p_[x]);  
  409.         }  
  410.     }  
  411.   
  412.     // float  
  413.     cv::Mat matf;  
  414.     mat.convertTo(matf, CV_32FC3);  
  415.   
  416.     fbc::Mat_<float, 3> mat4(height, width, matf.data);  
  417.     fbc::Mat_<float, 3> mat5(mat4);  
  418.     fbc::Mat_<float, 3> mat6(height, width);  
  419.     fbc::cvtColor(mat5, mat6, fbc::CV_XYZ2BGR);  
  420.   
  421.     cv::Mat mat4_(height, width, CV_32FC3, matf.data);  
  422.     cv::Mat mat5_;  
  423.     mat4_.copyTo(mat5_);  
  424.     cv::Mat mat6_(height, width, CV_32FC3);  
  425.     cv::cvtColor(mat5_, mat6_, CV_XYZ2BGR);  
  426.   
  427.     assert(mat6.step == mat6_.step);  
  428.     for (int y = 0; y < mat6.rows; y++) {  
  429.         const fbc::uchar* p = mat6.ptr(y);  
  430.         const uchar* p_ = mat6_.ptr(y);  
  431.   
  432.         for (int x = 0; x < mat6.step; x++) {  
  433.             assert(p[x] == p_[x]);  
  434.         }  
  435.     }  
  436.   
  437.     return 0;  
  438. }  
  439.   
  440. int test_cvtColor_RGB2HSV()  
  441. {  
  442.     cv::Mat mat = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  443.     if (!mat.data) {  
  444.         std::cout << "read image fail" << std::endl;  
  445.         return -1;  
  446.     }  
  447.   
  448.     int code[] = { fbc::CV_BGR2HSV, fbc::CV_BGR2HLS };  
  449.   
  450.     for (int i = 0; i < 2; i++) {  
  451.         int width = mat.cols;  
  452.         int height = mat.rows;  
  453.   
  454.         // uchar  
  455.         fbc::Mat_<uchar, 3> mat1(height, width, mat.data);  
  456.         fbc::Mat_<uchar, 3> mat2(mat1);  
  457.         fbc::Mat_<uchar, 3> mat3(height, width);  
  458.         fbc::cvtColor(mat2, mat3, code[i]);  
  459.   
  460.         cv::Mat mat1_(height, width, CV_8UC3, mat.data);  
  461.         cv::Mat mat2_;  
  462.         mat1_.copyTo(mat2_);  
  463.         cv::Mat mat3_(height, width, CV_8UC3);  
  464.         cv::cvtColor(mat2_, mat3_, code[i]);  
  465.   
  466.         assert(mat3.step == mat3_.step);  
  467.         for (int y = 0; y < mat3.rows; y++) {  
  468.             const fbc::uchar* p = mat3.ptr(y);  
  469.             const uchar* p_ = mat3_.ptr(y);  
  470.   
  471.             for (int x = 0; x < mat3.step; x++) {  
  472.                 assert(p[x] == p_[x]);  
  473.             }  
  474.         }  
  475.   
  476.         // float  
  477.         cv::Mat matf;  
  478.         mat.convertTo(matf, CV_32FC3);  
  479.   
  480.         fbc::Mat_<float, 3> mat4(height, width, matf.data);  
  481.         fbc::Mat_<float, 3> mat5(mat4);  
  482.         fbc::Mat_<float, 3> mat6(height, width);  
  483.         fbc::cvtColor(mat5, mat6, code[i]);  
  484.   
  485.         cv::Mat mat4_(height, width, CV_32FC3, matf.data);  
  486.         cv::Mat mat5_;  
  487.         mat4_.copyTo(mat5_);  
  488.         cv::Mat mat6_(height, width, CV_32FC3);  
  489.         cv::cvtColor(mat5_, mat6_, code[i]);  
  490.   
  491.         assert(mat6.step == mat6_.step);  
  492.         for (int y = 0; y < mat6.rows; y++) {  
  493.             const fbc::uchar* p = mat6.ptr(y);  
  494.             const uchar* p_ = mat6_.ptr(y);  
  495.   
  496.             for (int x = 0; x < mat6.step; x++) {  
  497.                 assert(p[x] == p_[x]);  
  498.             }  
  499.         }  
  500.     }  
  501.   
  502.     return 0;  
  503. }  
  504.   
  505. int test_cvtColor_HSV2RGB()  
  506. {  
  507.     cv::Mat mat = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  508.     if (!mat.data) {  
  509.         std::cout << "read image fail" << std::endl;  
  510.         return -1;  
  511.     }  
  512.   
  513.     int code[] = { fbc::CV_HSV2BGR, fbc::CV_HLS2BGR };  
  514.     int code1[] = { fbc::CV_BGR2HSV, fbc::CV_BGR2HLS };  
  515.   
  516.     for (int i = 0; i < 2; i++) {  
  517.         cv::cvtColor(mat, mat, code1[i]);  
  518.   
  519.         int width = mat.cols;  
  520.         int height = mat.rows;  
  521.   
  522.         // uchar  
  523.         fbc::Mat_<uchar, 3> mat1(height, width, mat.data);  
  524.         fbc::Mat_<uchar, 3> mat2(mat1);  
  525.         fbc::Mat_<uchar, 3> mat3(height, width);  
  526.         fbc::cvtColor(mat2, mat3, code[i]);  
  527.   
  528.         cv::Mat mat1_(height, width, CV_8UC3, mat.data);  
  529.         cv::Mat mat2_;  
  530.         mat1_.copyTo(mat2_);  
  531.         cv::Mat mat3_(height, width, CV_8UC3);  
  532.         cv::cvtColor(mat2_, mat3_, code[i]);  
  533.   
  534.         assert(mat3.step == mat3_.step);  
  535.         for (int y = 0; y < mat3.rows; y++) {  
  536.             const fbc::uchar* p = mat3.ptr(y);  
  537.             const uchar* p_ = mat3_.ptr(y);  
  538.   
  539.             for (int x = 0; x < mat3.step; x++) {  
  540.                 assert(p[x] == p_[x]);  
  541.             }  
  542.         }  
  543.   
  544.         // float  
  545.         cv::Mat matf;  
  546.         mat.convertTo(matf, CV_32FC3);  
  547.   
  548.         fbc::Mat_<float, 3> mat4(height, width, matf.data);  
  549.         fbc::Mat_<float, 3> mat5(mat4);  
  550.         fbc::Mat_<float, 3> mat6(height, width);  
  551.         fbc::cvtColor(mat5, mat6, code[i]);  
  552.   
  553.         cv::Mat mat4_(height, width, CV_32FC3, matf.data);  
  554.         cv::Mat mat5_;  
  555.         mat4_.copyTo(mat5_);  
  556.         cv::Mat mat6_(height, width, CV_32FC3);  
  557.         cv::cvtColor(mat5_, mat6_, code[i]);  
  558.   
  559.         assert(mat6.step == mat6_.step);  
  560.         for (int y = 0; y < mat6.rows; y++) {  
  561.             const fbc::uchar* p = mat6.ptr(y);  
  562.             const uchar* p_ = mat6_.ptr(y);  
  563.   
  564.             for (int x = 0; x < mat6.step; x++) {  
  565.                 assert(p[x] == p_[x]);  
  566.             }  
  567.         }  
  568.     }  
  569.   
  570.     return 0;  
  571. }  
  572.   
  573. int test_cvtColor_RGB2Lab()  
  574. {  
  575.     cv::Mat mat = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  576.     if (!mat.data) {  
  577.         std::cout << "read image fail" << std::endl;  
  578.         return -1;  
  579.     }  
  580.   
  581.     int code[] = { fbc::CV_BGR2Lab, fbc::CV_BGR2Luv };  
  582.   
  583.     for (int i = 0; i < 2; i++) {  
  584.         int width = mat.cols;  
  585.         int height = mat.rows;  
  586.   
  587.         // uchar  
  588.         fbc::Mat_<uchar, 3> mat1(height, width, mat.data);  
  589.         fbc::Mat_<uchar, 3> mat2(mat1);  
  590.         fbc::Mat_<uchar, 3> mat3(height, width);  
  591.         fbc::cvtColor(mat2, mat3, code[i]);  
  592.   
  593.         cv::Mat mat1_(height, width, CV_8UC3, mat.data);  
  594.         cv::Mat mat2_;  
  595.         mat1_.copyTo(mat2_);  
  596.         cv::Mat mat3_(height, width, CV_8UC3);  
  597.         cv::cvtColor(mat2_, mat3_, code[i]);  
  598.   
  599.         assert(mat3.step == mat3_.step);  
  600.         for (int y = 0; y < mat3.rows; y++) {  
  601.             const fbc::uchar* p = mat3.ptr(y);  
  602.             const uchar* p_ = mat3_.ptr(y);  
  603.   
  604.             for (int x = 0; x < mat3.step; x++) {  
  605.                 assert(p[x] == p_[x]);  
  606.             }  
  607.         }  
  608.   
  609.         // float  
  610.         cv::Mat matf;  
  611.         mat.convertTo(matf, CV_32FC3);  
  612.   
  613.         fbc::Mat_<float, 3> mat4(height, width, matf.data);  
  614.         fbc::Mat_<float, 3> mat5(mat4);  
  615.         fbc::Mat_<float, 3> mat6(height, width);  
  616.         fbc::cvtColor(mat5, mat6, code[i]);  
  617.   
  618.         cv::Mat mat4_(height, width, CV_32FC3, matf.data);  
  619.         cv::Mat mat5_;  
  620.         mat4_.copyTo(mat5_);  
  621.         cv::Mat mat6_(height, width, CV_32FC3);  
  622.         cv::cvtColor(mat5_, mat6_, code[i]);  
  623.   
  624.         assert(mat6.step == mat6_.step);  
  625.         for (int y = 0; y < mat6.rows; y++) {  
  626.             const fbc::uchar* p = mat6.ptr(y);  
  627.             const uchar* p_ = mat6_.ptr(y);  
  628.   
  629.             for (int x = 0; x < mat6.step; x++) {  
  630.                 assert(p[x] == p_[x]);  
  631.             }  
  632.         }  
  633.     }  
  634. }  
  635.   
  636. int test_cvtColor_Lab2RGB()  
  637. {  
  638.     cv::Mat mat = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  639.     if (!mat.data) {  
  640.         std::cout << "read image fail" << std::endl;  
  641.         return -1;  
  642.     }  
  643.   
  644.     int code[] = { fbc::CV_Lab2BGR, fbc::CV_Luv2BGR };  
  645.     int code1[] = { fbc::CV_BGR2Lab, fbc::CV_BGR2Luv };  
  646.   
  647.     for (int i = 0; i < 2; i++) {  
  648.         cv::cvtColor(mat, mat, code1[i]);  
  649.   
  650.         int width = mat.cols;  
  651.         int height = mat.rows;  
  652.   
  653.         // uchar  
  654.         fbc::Mat_<uchar, 3> mat1(height, width, mat.data);  
  655.         fbc::Mat_<uchar, 3> mat2(mat1);  
  656.         fbc::Mat_<uchar, 3> mat3(height, width);  
  657.         fbc::cvtColor(mat2, mat3, code[i]);  
  658.   
  659.         cv::Mat mat1_(height, width, CV_8UC3, mat.data);  
  660.         cv::Mat mat2_;  
  661.         mat1_.copyTo(mat2_);  
  662.         cv::Mat mat3_(height, width, CV_8UC3);  
  663.         cv::cvtColor(mat2_, mat3_, code[i]);  
  664.   
  665.         assert(mat3.step == mat3_.step);  
  666.         for (int y = 0; y < mat3.rows; y++) {  
  667.             const fbc::uchar* p = mat3.ptr(y);  
  668.             const uchar* p_ = mat3_.ptr(y);  
  669.   
  670.             for (int x = 0; x < mat3.step; x++) {  
  671.                 assert(p[x] == p_[x]);  
  672.             }  
  673.         }  
  674.   
  675.         // float  
  676.         cv::Mat matf;  
  677.         mat.convertTo(matf, CV_32FC3);  
  678.   
  679.         fbc::Mat_<float, 3> mat4(height, width, matf.data);  
  680.         fbc::Mat_<float, 3> mat5(mat4);  
  681.         fbc::Mat_<float, 3> mat6(height, width);  
  682.         fbc::cvtColor(mat5, mat6, code[i]);  
  683.   
  684.         cv::Mat mat4_(height, width, CV_32FC3, matf.data);  
  685.         cv::Mat mat5_;  
  686.         mat4_.copyTo(mat5_);  
  687.         cv::Mat mat6_(height, width, CV_32FC3);  
  688.         cv::cvtColor(mat5_, mat6_, code[i]);  
  689.   
  690.         assert(mat6.step == mat6_.step);  
  691.         for (int y = 0; y < mat6.rows; y++) {  
  692.             const fbc::uchar* p = mat6.ptr(y);  
  693.             const uchar* p_ = mat6_.ptr(y);  
  694.   
  695.             for (int x = 0; x < mat6.step; x++) {  
  696.                 assert(p[x] == p_[x]);  
  697.             }  
  698.         }  
  699.     }  
  700.   
  701.     return 0;  
  702. }  
  703.   
  704. int test_cvtColor_YUV2BGR()  
  705. {  
  706.     cv::Mat mat = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  707.     if (!mat.data) {  
  708.         std::cout << "read image fail" << std::endl;  
  709.         return -1;  
  710.     }  
  711.   
  712.     cv::cvtColor(mat, mat, CV_BGR2YUV_I420);  
  713.   
  714.     int width = mat.cols;  
  715.     int height = mat.rows;  
  716.     int newHeight = height * 2 / 3;  
  717.   
  718.     // uchar  
  719.     fbc::Mat_<uchar, 1> mat1(height, width, mat.data);  
  720.     fbc::Mat_<uchar, 1> mat2(mat1);  
  721.     fbc::Mat_<uchar, 3> mat3(newHeight, width);  
  722.     fbc::cvtColor(mat2, mat3, fbc::CV_YUV2BGR_I420);  
  723.   
  724.     cv::Mat mat1_(height, width, CV_8UC1, mat.data);  
  725.     cv::Mat mat2_;  
  726.     mat1_.copyTo(mat2_);  
  727.     cv::Mat mat3_(newHeight, width, CV_8UC3);  
  728.     cv::cvtColor(mat2_, mat3_, CV_YUV2BGR_I420);  
  729.   
  730.     assert(mat3.step == mat3_.step);  
  731.     for (int y = 0; y < mat3.rows; y++) {  
  732.         const fbc::uchar* p = mat3.ptr(y);  
  733.         const uchar* p_ = mat3_.ptr(y);  
  734.   
  735.         for (int x = 0; x < mat3.step; x++) {  
  736.             assert(p[x] == p_[x]);  
  737.         }  
  738.     }  
  739.   
  740.     return 0;  
  741. }  
  742.   
  743. int test_cvtColor_BGR2YUV()  
  744. {  
  745.     cv::Mat mat = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  746.     if (!mat.data) {  
  747.         std::cout << "read image fail" << std::endl;  
  748.         return -1;  
  749.     }  
  750.   
  751.     int width = mat.cols;  
  752.     int height = mat.rows;  
  753.     int newHeight = height / 2 * 3;  
  754.   
  755.     // uchar  
  756.     fbc::Mat_<uchar, 3> mat1(height, width, mat.data);  
  757.     fbc::Mat_<uchar, 3> mat2(mat1);  
  758.     fbc::Mat_<uchar, 1> mat3(newHeight, width);  
  759.     fbc::cvtColor(mat2, mat3, fbc::CV_BGR2YUV_YV12);  
  760.   
  761.     cv::Mat mat1_(height, width, CV_8UC3, mat.data);  
  762.     cv::Mat mat2_;  
  763.     mat1_.copyTo(mat2_);  
  764.     cv::Mat mat3_(newHeight, width, CV_8UC1);  
  765.     cv::cvtColor(mat2_, mat3_, CV_BGR2YUV_YV12);  
  766.   
  767.     assert(mat3.step == mat3_.step);  
  768.     for (int y = 0; y < mat3.rows; y++) {  
  769.         const fbc::uchar* p = mat3.ptr(y);  
  770.         const uchar* p_ = mat3_.ptr(y);  
  771.   
  772.         for (int x = 0; x < mat3.step; x++) {  
  773.             assert(p[x] == p_[x]);  
  774.         }  
  775.     }  
  776.   
  777.     return 0;  
  778. }  
  779.   
  780. int test_cvtColor_YUV2Gray()  
  781. {  
  782.     cv::Mat mat = cv::imread("E:/GitCode/OpenCV_Test/test_images/lena.png", 1);  
  783.     if (!mat.data) {  
  784.         std::cout << "read image fail" << std::endl;  
  785.         return -1;  
  786.     }  
  787.     cv::cvtColor(mat, mat, CV_BGRA2YUV_I420);  
  788.   
  789.     int width = mat.cols;  
  790.     int height = mat.rows;  
  791.     int newHeight = height * 2 / 3;  
  792.   
  793.     // uchar  
  794.     fbc::Mat_<uchar, 1> mat1(height, width, mat.data);  
  795.     fbc::Mat_<uchar, 1> mat2(mat1);  
  796.     fbc::Mat_<uchar, 1> mat3(newHeight, width);  
  797.     fbc::cvtColor(mat2, mat3, fbc::CV_YUV2GRAY_420);  
  798.   
  799.     cv::Mat mat1_(height, width, CV_8UC1, mat.data);  
  800.     cv::Mat mat2_;  
  801.     mat1_.copyTo(mat2_);  
  802.     cv::Mat mat3_(newHeight, width, CV_8UC1);  
  803.     cv::cvtColor(mat2_, mat3_, CV_YUV2GRAY_420);  
  804.   
  805.     assert(mat3.step == mat3_.step);  
  806.     for (int y = 0; y < mat3.rows; y++) {  
  807.         const fbc::uchar* p = mat3.ptr(y);  
  808.         const uchar* p_ = mat3_.ptr(y);  
  809.   
  810.         for (int x = 0; x < mat3.step; x++) {  
  811.             assert(p[x] == p_[x]);  
  812.         }  
  813.     }  
  814.   
  815.     return 0;  
  816. }  

 

 

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

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