@@ -27,15 +27,45 @@ void show_image(cv::Mat image) {
2727 cv::waitKey (0 );
2828}
2929
30- constexpr int width = 10 ;
31- constexpr int height = 10 ;
32- constexpr int channel = 3 ;
30+ constexpr int kWidth = 12 ;
31+ constexpr int kHeight = 10 ;
32+ constexpr int kChannel = 3 ;
33+
34+
35+ // Return the depth of the matrice element
36+ // cf. https://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#a8da9f853b6f3a29d738572fd1ffc44c0
37+ std::string checkDepth (const cv::Mat& mat) {
38+ int depth = mat.depth ();
39+ switch (depth) {
40+ case CV_8U:
41+ return " CV_8U" ;
42+ case CV_8S:
43+ return " CV_8S" ;
44+ case CV_16U:
45+ return " CV_16U" ;
46+ case CV_16S:
47+ return " CV_16S" ;
48+ case CV_32S:
49+ return " CV_32S" ;
50+ case CV_32F:
51+ return " CV_32F" ;
52+ case CV_64F:
53+ return " CV_64F" ;
54+ default :
55+ throw std::runtime_error (" Unknown element type" );
56+ }
57+ }
58+
59+ std::tuple<int , int , int > getShape (const cv::Mat& mat) {
60+ cv::Size s = mat.size ();
61+ return {s.height , s.width , mat.channels ()};
62+ }
3363
3464
3565// Generate buffer for a matrix 10x10x3
3666std::vector<uint16_t > generateBuffer () {
3767 std::vector<uint16_t > matrice;
38- const uint16_t size = width * height * channel ;
68+ const uint16_t size = kWidth * kHeight * kChannel ;
3969 matrice.reserve (size);
4070 for (uint16_t i = 0 ; i < size; ++i) {
4171 matrice.push_back (i);
@@ -48,53 +78,47 @@ std::vector<uint16_t> generateBuffer() {
4878// Example of function that return a matrix by value
4979cv::Mat generateMatrix () {
5080 const std::vector<uint16_t > buffer = generateBuffer ();
51- return cv::Mat (buffer, true ).reshape (3 , {10 , 10 });
81+ return cv::Mat (buffer, true ).reshape (kChannel , {kHeight , kWidth });
5282}
5383
5484// Example of function that take a matrix as argument
5585bool checkMatrixContent (const cv::Mat& mat) {
56- // I don't compare buffer from the matrix with expected buffer to correctly manage slicing .
86+ // I don't compare buffer from the matrix with expected buffer to correctly manage non contiguous matrices .
5787 const std::vector<uint16_t > expectedBuffer = generateBuffer ();
88+
89+ auto [height, width, channel] = getShape (mat);
90+ assert (height * width * channel == expectedBuffer.size ());
91+
5892 bool match = true ;
59- for (int j = 0 ; j < height; ++j ) {
60- for (int i = 0 ; i < width; ++i ) {
61- const cv::Vec3w values = mat. at <cv::Vec3w>( cv::Point (i,j));
62- match &= values ( 0 ) == expectedBuffer[j * width * 3 + i * 3 + 0 ] ;
63- match &= values (1 ) == expectedBuffer[j * width * 3 + i * 3 + 1 ];
64- match &= values ( 2 ) == expectedBuffer[j * width * 3 + i * 3 + 2 ];
93+ for (uint16_t i = 0 ; i < height; ++i ) {
94+ for (uint16_t j = 0 ; j < width; ++j ) {
95+ for ( uint16_t c = 0 ; c < channel; ++c) {
96+ const cv::Vec3w values = mat. at <cv::Vec3w>( cv::Point (j,i)) ;
97+ match &= values (c ) == expectedBuffer[i * width * channel + j * channel + c ];
98+ }
6599 }
66100 }
101+
67102 return match;
68103}
69104
70- // Return the depth of the matrice element
71- // cf. https://docs.opencv.org/master/d3/d63/classcv_1_1Mat.html#a8da9f853b6f3a29d738572fd1ffc44c0
72- std::string checkDepth (const cv::Mat& mat) {
73- int depth = mat.depth ();
74- switch (depth) {
75- case CV_8U:
76- return " CV_8U" ;
77- case CV_8S:
78- return " CV_8S" ;
79- case CV_16U:
80- return " CV_16U" ;
81- case CV_16S:
82- return " CV_16S" ;
83- case CV_32S:
84- return " CV_32S" ;
85- case CV_32F:
86- return " CV_32F" ;
87- case CV_64F:
88- return " CV_64F" ;
89- default :
90- throw std::runtime_error (" Unknown element type" );
105+ // Convert the matrice in a vector to compare
106+ std::vector<uint16_t > getContentAsVector (const cv::Mat &mat) {
107+ auto [height, width, channel] = getShape (mat);
108+ int size = height*width*channel;
109+ std::vector<uint16_t > buffer;
110+ buffer.reserve (size);
111+ for (uint16_t i = 0 ; i < height; ++i) {
112+ for (uint16_t j = 0 ; j < width; ++j) {
113+ for (uint16_t c = 0 ; c < channel; ++c) {
114+ const cv::Vec3w values = mat.at <cv::Vec3w>(cv::Point (i,j));
115+ buffer.push_back (values (channel));
116+ }
117+ }
91118 }
119+ return buffer;
92120}
93121
94- std::tuple<int , int , int > getShape (const cv::Mat& mat) {
95- cv::Size s = mat.size ();
96- return {s.width , s.height , mat.channels ()};
97- }
98122
99123cv::Mat passthru (cv::Mat image) {
100124 return image;
0 commit comments