Skip to content

Commit 8101cde

Browse files
committed
feat: change width to differe from height
1 parent 1644200 commit 8101cde

File tree

4 files changed

+72
-43
lines changed

4 files changed

+72
-43
lines changed

CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ SET(SOURCES
4848

4949
#**************************************************************************************************
5050
# Set compiler **************************************************************************************************
51-
SET(CMAKE_CXX_FLAGS "-std=c++11 -Wall -O3 -fPIC")
51+
if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
52+
add_compile_options(/std:c++latest /Oy /utf-8)
53+
else()
54+
add_compile_options(-std=c++2a -fPIC -O3)
55+
endif()
5256

5357
#**************************************************************************************************
5458
# Linker **************************************************************************************************

tests/cpp/test.cpp

Lines changed: 61 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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
3666
std::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
4979
cv::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
5585
bool 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

99123
cv::Mat passthru(cv::Mat image) {
100124
return image;

tests/test_binding.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,24 @@
55

66
def test_pass_py2cpp():
77
mat = generate_matrix()
8-
assert(mat.shape == (10, 10, 3))
8+
assert(mat.shape == (10, 12, 3))
99
assert(mat.flags['C_CONTIGUOUS'])
1010
assert(mat.dtype == np.uint16)
1111
assert(tm.check_matrix_content(mat))
12+
assert(tm.get_shape(mat) == (10, 12, 3))
1213

1314

1415
def test_pass_cpp2py():
1516
mat = tm.generate_matrix()
16-
assert(mat.shape == (10, 10, 3))
17+
assert(mat.shape == (10, 12, 3))
1718
assert(mat.flags['C_CONTIGUOUS'])
1819
assert(mat.dtype == np.uint16)
1920
assert(check_matrix_content(mat))
2021

2122

2223
def test_passthough_cpp2cpp():
2324
mat = tm.generate_matrix()
24-
assert(mat.shape == (10, 10, 3))
25+
assert(mat.shape == (10, 12, 3))
2526
assert(mat.flags['C_CONTIGUOUS'])
2627
assert(check_matrix_content(mat))
2728
assert(tm.check_matrix_content(mat))
@@ -31,7 +32,7 @@ def test_passthough_py2py():
3132
mat = generate_matrix()
3233
returned_mat = tm.passthru(mat)
3334
assert(returned_mat.flags['C_CONTIGUOUS'])
34-
assert(returned_mat.shape == (10, 10, 3))
35+
assert(returned_mat.shape == (10, 12, 3))
3536
assert(check_matrix_content(returned_mat))
3637

3738

tests/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33

44
def generate_matrix():
5-
return np.arange(10*10*3, dtype=np.uint16).reshape((10, 10, 3))
5+
return np.arange(10*12*3, dtype=np.uint16).reshape((10, 12, 3))
66

77

88
def check_matrix_content(mat):

0 commit comments

Comments
 (0)