NumPy ndarray ⇋ OpenCV Mat conversion, that just works.
Switch branches/tags
Nothing to show
Clone or download
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore Add new examples Jul 11, 2014
LICENSE.txt MIT License Nov 30, 2014
Makefile python3 support Sep 1, 2014 Update README Dec 1, 2014
conversion.cpp python3 support Sep 1, 2014
conversion.h python3 support Sep 1, 2014
examples.cpp Add new examples Jul 11, 2014 Update testcase name Dec 1, 2014

NumPy ndarray ⇋ OpenCV Mat conversion, that just works.


  • class NDArrayConverter: The converter class

  • NDArrayConverter::NDArrayConverter(): Constructor

  • cv::Mat NDArrayConverter::toMat(const PyObject* o): Convert a NumPy ndarray to a cv::Mat.

    • o is the object representing the Python representation of the ndarray.

    • Returns a cv::Mat which is the OpenCV representation of o.

  • PyObject* NDArrayConverter::toNDArray(const cv::Mat& mat): Convert a cv::Mat to a NumPy ndarray.

    • mat is the cv::Mat to convert.

    • Returns a PyObject* that is the Python representation of an ndarray.


examples.cpp contains

  • An implementation of matrix multiplication mul() that takes two ndarray objects, converts them to cv::Mat, multiplies them and returns the result as an ndarray.

  • An image binarization function binarize() that takes an ndarray containing a grayscale image and a threshold value, converts the image to a cv::Mat and thresholds(binarizes) it. It then returns the result as a ndarray.

  • An image display function display(ndarray) that just takes any image as an ndarray object and displays it.

All of these functions are callable from Python.

The plumbing(passing of values to and from Python) is handled by Boost::Python. contains some testcases that call the aforementioned C++ functions from Python.

Building and testing

After installing Boost::Python and NumPy(maybe the devel package),

$ make
$ make test

This builds and tests the project. Specifically, it builds the core converter in conversion.o and a set of examples in, which is a Python module containing a few examples of using the converter. contains a test-suite to test (and demonstrate) the converter.