Skip to content

Commit 8066bed

Browse files
committed
feat: cleaning
1 parent a3222df commit 8066bed

File tree

7 files changed

+117
-7
lines changed

7 files changed

+117
-7
lines changed

.gitignore

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
/.vscode
2+
3+
__pycache__
14
*.pyc
25
*.egg-info
36
*.so
4-
__pycache__
5-
/.vscode
7+
68
/build
79
/build*
810
/tmp

Readme.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
Binding between cv::Mat and np.array. And a small code example of how it work. the code work for `OpenCV 2.4`, `OpenCV 3+` and `OpenCV 4+`
44

5-
The code in this repository create a simple binding, function in c++ are implemented in `example.cpp` file and the script that use them is `test.py`.
5+
The code in this repository create a simple binding, function in c++ are implemented in `example.cpp` file and the script that use them is `example.py`.
66

77
```bash
88
/project folder
@@ -35,10 +35,11 @@ mkdir build
3535
cd build
3636
# configure make with vcpkg toolchain
3737
cmake .. -DCMAKE_TOOLCHAIN_FILE=${VCPKG_DIR}/scripts/buildsystems/vcpkg.cmake
38+
# on Windows : cmake.exe .. -DCMAKE_TOOLCHAIN_FILE="$Env:VCPKG_DIR/scripts/buildsystems/vcpkg.cmake"
3839
# generate the example.so library
39-
make
40+
cmake --build . --config Release
4041
# move example.so library in example folder
41-
make install
42+
cmake --install . --config Release
4243
```
4344

4445
## Generation with setup.py
@@ -47,7 +48,7 @@ make install
4748
### install pybind11
4849

4950
```
50-
pip3 install pybind11
51+
python3 -m pip3 install pybind11
5152
```
5253

5354
### Compile

test.py renamed to example.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import copy
44

55
# Read from c++
6-
a = eb.read_image("test.png")
6+
a = eb.read_image("tests/images/test.png")
77
print('init a: 0x%x' % id(a))
88
eb.show_image(a) # work
99

tests/cpp/test.cpp

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include <pybind11/pybind11.h>
2+
#include <pybind11/stl.h>
3+
4+
5+
#include <opencv2/core/core.hpp>
6+
#include <opencv2/highgui/highgui.hpp>
7+
#include <opencv2/imgproc.hpp>
8+
9+
#include <iostream>
10+
#include <string>
11+
12+
#include "ndarray_converter.h"
13+
14+
namespace py = pybind11;
15+
16+
void show_image(cv::Mat image) {
17+
cv::imshow("image_from_Cpp", image);
18+
cv::waitKey(0);
19+
}
20+
21+
cv::Mat read_image(std::string image_name) {
22+
#if CV_MAJOR_VERSION < 4
23+
cv::Mat image = cv::imread(image_name, CV_LOAD_IMAGE_COLOR);
24+
#else
25+
cv::Mat image = cv::imread(image_name, cv::IMREAD_COLOR);
26+
#endif
27+
return image;
28+
}
29+
30+
cv::Mat passthru(cv::Mat image) {
31+
return image;
32+
}
33+
34+
cv::Mat cloneimg(cv::Mat image) {
35+
return image.clone();
36+
}
37+
38+
class AddClass {
39+
public:
40+
AddClass(int value) : value(value) {}
41+
42+
cv::Mat add(cv::Mat input) {
43+
return input + this->value;
44+
}
45+
46+
private:
47+
int value;
48+
};
49+
50+
PYBIND11_MODULE(example, m) {
51+
52+
NDArrayConverter::init_numpy();
53+
54+
m.def("read_image", &read_image, "A function that read an image",
55+
py::arg("image"));
56+
57+
m.def("show_image", &show_image, "A function that show an image",
58+
py::arg("image"));
59+
60+
m.def("passthru", &passthru, "Passthru function", py::arg("image"));
61+
m.def("clone", &cloneimg, "Clone function", py::arg("image"));
62+
63+
py::class_<AddClass>(m, "AddClass")
64+
.def(py::init<int>())
65+
.def("add", &AddClass::add);
66+
}
File renamed without changes.

tests/test.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import numpy as np
2+
import test_module.test as test
3+
import copy
4+
5+
# Read from c++
6+
a = test.read_image("test.png")
7+
print('init a: 0x%x' % id(a))
8+
test.show_image(a) # work
9+
10+
# Check continuous problem from old version
11+
b = a[:, :, 0]
12+
test.show_image(b) # work no more continous problem
13+
print('diff b: 0x%x' % id(b))
14+
15+
c = copy.deepcopy(b)
16+
test.show_image(c) # still works
17+
print('diff c: 0x%x' % id(c))
18+
19+
20+
# Proves that it's still the same thing
21+
d = test.passthru(a)
22+
print('same d: 0x%x' % id(b))
23+
24+
# Make a copy
25+
e = test.clone(d)
26+
print('diff e: 0x%x' % id(e))
27+
28+
29+
# different allocator
30+
f = np.zeros(shape=(100, 100), dtype=np.uint8)
31+
print('\ninit e: 0x%x' % id(f))
32+
33+
g = test.passthru(f)
34+
print('same f: 0x%x' % id(g))
35+
36+
37+
# example of class
38+
my_class = test.AddClass(1)
39+
h = my_class.add(f)
40+
print(f[0, 0]) # expected 0
41+
print(h[0, 0]) # expected 1

tests/test_module/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)