Coder Social home page Coder Social logo

s-t-a-n / pybind11_opencv_numpy Goto Github PK

View Code? Open in Web Editor NEW

This project forked from edmbernard/pybind11_opencv_numpy

0.0 1.0 0.0 80 KB

Implementation of cv::Mat conversion to numpy.array for pybind11

License: Apache License 2.0

C++ 55.59% Python 34.93% CMake 9.48%

pybind11_opencv_numpy's Introduction

pybind11_opencv_numpy

Binding between cv::Mat and np.array. And a small code example of how it work. The code work for OpenCV 3+ and OpenCV 4+

The code in this repository create a simple binding, function in c++ are implemented in tests/cpp/test.cpp file and python script that use them are in the tests folder like tests/test_binding.py.

/project folder
├── CMakeLists.txt
├── setup.py
├── ndarray_converter.cpp  # Effective implementation of the binding
├── ndarray_converter.h    # Header for binding functions
└── ...

There is 3 way to build the project, more information can be found on pybind11 website here :

  1. build with cmake : the basic, compiled library will be generated in build folder
  2. build with setup.py and cmake : install your module in python and dependency are managed by cmake/vcpkg (opencv and pybind11)
  3. Not Supported Yet build with setup.py and setuptools : install your module in python and dependency are managed by python package (opencv and pybind11)

Build with cmake

Note : This method support opencv 3 and opencv 4.

Step 1 : Install dependencies

We use vcpkg to install pybind11 library

./vcpkg install pybind11 opencv4

Step 2 : Define the environnement variable to VCPKG Directory

on Linux

export VCPKG_DIR=/path/to/vcpkg/dir

on Windows

$Env:VCPKG_DIR="/path/to/vcpkg/dir"

Note : vcpkg dir should contain the following folder : scripts/buildsystems/vcpkg.cmake

Step 3 : Compile

mkdir build
cd build
# configure make with vcpkg toolchain
cmake .. -DCMAKE_TOOLCHAIN_FILE=${VCPKG_DIR}/scripts/buildsystems/vcpkg.cmake
# on Windows : cmake.exe .. -DCMAKE_TOOLCHAIN_FILE="$Env:VCPKG_DIR/scripts/buildsystems/vcpkg.cmake"
# generate your library, in this poject it's the test_module
cmake --build . --config Release

Numpy header

In case of error like: 'numpy/ndarrayobject.h' : No such file or directory

You have to be sure numpy is install on the computer either with python -m pip install numpy or sudo apt-get install python-numpy for linux. Even if numpy is installed, CMake was not able to find correctly numpy header during configuration. Probably because numpy is install on the user package. Its possible the explicitly set the directory with the following command :

cmake .. -DCMAKE_TOOLCHAIN_FILE=${VCPKG_DIR}/scripts/buildsystems/vcpkg.cmake -DNUMPY_INCLUDE_DIR="${PYTHON_USER_DIR}/LocalCache/local-packages/Python39/site-packages/numpy/core/include/"

Numpy header location can be found with the following command :

python -c "import numpy as np; print(np.get_include())"

Build with setup.py and cmake

Note : This method support opencv 3 and opencv 4.

You should first follow step 1 and 2 from the Build with cmake paragraph

Step 3 : Compile

OpenCV 3+ , OpenCV 4+

python3 -m pip install .

Execute Tests

Tests use the binding module generate by cmake.

python -m pytest

Limitation

Currently, the conversion between cv::Mat and Numpy.ndarray is done by copy.

pybind11_opencv_numpy's People

Contributors

edmbernard avatar virtuald avatar sloretz avatar vietanhdev avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.