• Stars
    star
    199
  • Rank 196,105 (Top 4 %)
  • Language
    C++
  • License
    Apache License 2.0
  • Created almost 8 years ago
  • Updated almost 3 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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

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.

More Repositories

1

DockerFiles

Some useful dockerfiles for DeepLearning and Computer Vision
Makefile
27
star
2

mxnet_example_shared_weight

small examples to test shared layer
Python
11
star
3

cxxsubs

Basic C++ Subcommand/Verbs support for command line. It use cxxopts for each subcommand parsing.
C++
3
star
4

image-viewer-rs

Image Viewer that allow comparison side-by-side and stacked.
Rust
3
star
5

stryke

An Orc C++ template library build on top of Orc C++ library.
C++
2
star
6

simple-http-server-cpp

A simple example of a http server using uwebsockets
C++
2
star
7

openFrameworks-template

Template repository to show a minimal project including openFramework
C++
1
star
8

rpi-led-controller

WIP: Server to interacte with RPi Sense Hat Led via web interface
JavaScript
1
star
9

AoC-2020-cpp

My Code for Advent of Code 2020
C++
1
star
10

bg-generation-penrose

Program to generate Penrose tiling
C++
1
star
11

KoTH-pandemic

Simple C++ controller to manage bot for king of the hill game
C++
1
star
12

redis-monitor

A simple monitor tools with a UI interface via Web page.
C++
1
star
13

card-drawing

Small script to calculate card drawing probability (MTG)
Python
1
star
14

bg-generation-conway

html page to create pixelized background
HTML
1
star
15

include-graph

Tools to write include dependency graph from c++ code in chords graph
C++
1
star
16

bg-generation

Fun script to generate Background
Python
1
star
17

voyelles-pendues-engine

Engine part for the game voyelle-pendues
C++
1
star
18

KoTH-snake

[WIP] Simple C++ server to manage bot for king of the hill snake-like game
C++
1
star
19

time-tracker

Minimalist command line tool to track time spend
C++
1
star
20

voyelles-pendues-ui

UI part for the game voyelle-pendues
QML
1
star
21

cookiecutter-cpp-vscode

CMake
1
star
22

bg-generation-triangle

Generation of fractal triangle pattern and save it in svg.
C++
1
star
23

AoC-2022

My Code for Advent of Code 2022
Rust
1
star