Skip to content

Demo about how to implement high-performance algorithm based OpenCV as extension for NumPy.

License

Notifications You must be signed in to change notification settings

lumina37/py-cffi-bridge-npy-and-ocv-demo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Python-cffi: Bridge NumPy and OpenCV Demo

This demo shows the bidirectional conversion between NumPy ndarray (uint8, not more than 3 channels) and cv::Mat (CV_8U). I hope it could be a draft guideline about how to implement high-performance algorithm based OpenCV as extension for NumPy.

The demo also applies lots of next-generation Python-Cpp hybrid project organizing methods.

For the Cpp side, the complex compilation and source seeking are entirely managed by CMake.

For the Python side, the project follows PEP-518, with a pyproject.toml containing all project metadata.

Then the Python package managing tool rye and the extension building tool scikit-build-core work as glue to bridge both sides together.

Yet, I must admit that it is extremely hard to debug the C-Extension for Python due to the intricate linker settings and poor IDE support.

Before Build

Install OpenCV first. This demo is only tested against BUILD_SHARED_LIBS=ON mode on Windows-x64 platform.

Note for Windows

If you are using Windows platform, please make sure the OpenCV DLLs can be found through the enviornment varible PATH.

Choose a Picture

Choose a picture, copy and paste it to the working directory, then rename it to test.jpg.

Build and Run

Using rye

rye pin 3.12
rye sync
rye run python main.py

Using pip

pip install -e .
python main.py

DO NOT miss the dot after -e

Expect Behaviour

GUI window pops out and shows the grayscaled image.

Reference

The original converter can be found here.

About

Demo about how to implement high-performance algorithm based OpenCV as extension for NumPy.

Topics

Resources

License

Stars

Watchers

Forks