https://github.com/elerac/nanobind_opencv
nanobind typecaster for opencv types (i.e., cv::Mat_, cv::Matx, cv::Vec)
https://github.com/elerac/nanobind_opencv
cpp nanobind opencv python
Last synced: about 2 months ago
JSON representation
nanobind typecaster for opencv types (i.e., cv::Mat_, cv::Matx, cv::Vec)
- Host: GitHub
- URL: https://github.com/elerac/nanobind_opencv
- Owner: elerac
- License: mit
- Created: 2023-04-03T16:18:27.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2023-05-19T07:32:03.000Z (about 2 years ago)
- Last Synced: 2025-03-28T17:02:00.036Z (2 months ago)
- Topics: cpp, nanobind, opencv, python
- Language: C++
- Homepage:
- Size: 16.6 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# nanobind - opencv typecaster
This repository provides a [nanobind](https://github.com/wjakob/nanobind) typecaster for opencv types.
The supported types are:
- `cv::Mat_<_Tp>`
- `cv::Matx<_Tp, m, n>`
- `cv::Vec<_Tp, n>`## Build
```bash
pip3 install .
```## Example
In C++:
```cpp
#include
#include
#include
#include "cv_typecaster.h"namespace nb = nanobind;
using namespace nb::literals;template
void inspect(const cv::Mat_<_Tp> mat)
{
std::cout << "[C++] Inspect cv::Mat_<_Tp>" << std::endl;
std::cout << " rows: " << mat.rows << std::endl;
std::cout << " cols: " << mat.cols << std::endl;
std::cout << " channels: " << mat.channels() << std::endl;
std::cout << " type: " << cv::typeToString(mat.type()) << std::endl;
}NB_MODULE(_nanobind_opencv_example_impl, m)
{
m.def("inspect", &inspect, nb::arg("mat").noconvert());
}
```In Python:
```python
import numpy as np
from nanobind_opencv_example import inspect# Prepare numpy data
array = np.random.rand(128, 256).astype(np.float32)# Inspect numpy data in Python
print("[Py] Inspect np.ndarray")
print(" shape: ", array.shape)
print(" dtype: ", array.dtype)# Pass numpy data to C++ and inspect it as cv::Mat_<_Tp>
inspect(array)
```After running the above example, the output should be:
```
$ python3 test.py
[Py] Inspect np.ndarray
shape: (128, 256)
dtype: float32
[C++] Inspect cv::Mat_<_Tp>
rows: 128
cols: 256
channels: 1
type: CV_32FC1
```