Code Monkey home page Code Monkey logo

opencv-ndarray-conversion's Introduction

NumPy ndarray ⇋ OpenCV Mat conversion, that just works.

API

  • 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

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.

test.py 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 examples.so, which is a Python module containing a few examples of using the converter. test.py contains a test-suite to test (and demonstrate) the converter.

opencv-ndarray-conversion's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

opencv-ndarray-conversion's Issues

After all job is done ,always recive Segmentation fault.

#0 0x00007ffff5bb325a in PyErr_Occurred ()
from /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0
#1 0x00007fffcd77c237 in ?? ()
from /usr/local/lib/python2.7/dist-packages/numpy/core/multiarray.so
#2 0x00007fffcd72f04e in ?? ()
from /usr/local/lib/python2.7/dist-packages/numpy/core/multiarray.so
#3 0x00000000007000ff in NumpyAllocator::deallocate (
this=0xac33f8 <g_numpyAllocator>, refcount=0x7fff461b08a0)

ImportError when running the test

Hi! I've been getting the following error after running make and make test:

ImportError: ./examples.so: undefined symbol: _ZNK5boost6python7objects21py_function_impl_base9max_arityEv

Some info about my configuration:
Ubuntu 12.04
g++ 4.6.3
Python 2.7.3
boost 1.48

Fixed it by changing the lines 20-22 in the Make file into:
$(TARGET).so: $(TARGET).o conversion.o
$(CXX) -shared -Wl,-soname,$(TARGET).so $(TARGET).o conversion.o -o $(TARGET).so $(LDFLAGS)
$(OPENCV_LDFLAGS) $(OPENCV_LIBS) $(BOOST_LIBS) $(PYTHON_LIBS) \

(Following the answer from http://stackoverflow.com/questions/1780003/import-error-on-boost-python-hello-program)

Maybe it helps someone, thanks for sharing the code!

Best,
Petar

Handling grayscale images

Any idea how to tweak your code to handle grayscale images? Currently I get a "MemoryError" if I pass one.

Running test.py and creating test.c and test.h

Hi , I am trying to create test.c and test.h by converting test.py to test.pyx . So that i can use conversion.cpp and conversion.h in my function .
I tried in anaconda prompt to run the command "-m test" in python shell. But i am not able to run the make file . Can you please help me/ or tell me the steps to generate test.c and test.
Thanks in advance

fail to convert 3 channel RGB image from python to opencv

Hi , I come across 2 problems,

1. rgb image could not be convert from python to opencv.

Yet when then image is gray , it works:

import cv2
img=cv2.imread("img.png");

import myLib#my libs as  follows 
cv3=myLib.cv3()
cv3.show(img,"imageTitle") 

But rgb image fails.
Yet, it seems that export from opencv to python works even if image is RGB:

imgExport=cv3.getSrcImage()

2. cv::Mat could not be reconginzed in python

error was reoport that Mat could not be reconginzed every time when I call :

cv3.srcImage

would you please give some direction on how to slove them ? Bellow is part of CPP code.

class cv3 {
    public: 
    cv::Mat srcImage;

    void loadImage(const char* srcPath  ){
        srcImage =cv::imread(srcPath ); 
    } 

    void getSrcImage(){
        return srcImage ;
    }

    void show(const cv::Mat& img, std::string title ="showImage"){
        cv::imshow(title,img);
        cv::waitKey(0);
        cv::destroyWindow(title);
    } 
}   




// Wrap a few functions and classes for testing purposes
namespace fs { namespace python {


    BOOST_PYTHON_MODULE(cv3)
    {
        // Main types export
        fs::python::init_and_export_converters();
        py::scope scope = py::scope(); 



    py::class_<cv3>("cv3")   
    .add_property("srcImage", & cv3::srcImage,& cv3::srcImage,  "srcImage")
         .def("loadImage", &cv3::loadImage)
         .def("getSrcImage", &cv3::getSrcImage) 

         .def("show", &cv3::show,( py::arg("img"),py::arg("title")= std::string("showImage")  )  ) 
          ;

    }

  } // namespace python
} // namespace fs

Update to OpenCV 3 maybe?

Basically i get the following error:

`conversion.cpp: In member function ‘cv::Mat NDArrayConverter::toMat(const PyObject*)’:
conversion.cpp:202:11: error: ‘class cv::Mat’ has no member named ‘refcount’
m.refcount = refcountFromPyObject(o);
^
conversion.cpp: In member function ‘PyObject* NDArrayConverter::toNDArray(const cv::Mat&)’:
conversion.cpp:223:12: error: ‘class cv::Mat’ has no member named ‘refcount’
if(!p->refcount || p->allocator != &g_numpyAllocator)
^
conversion.cpp:230:36: error: ‘class cv::Mat’ has no member named ‘refcount’
return pyObjectFromRefcount(p->refcount);

`
Which is caused for the absence of refcount which needs an update to OpenCV 3.0

Move to a better build system.

For platform and Python version dependent issues, it will be nice to move to a better build system from the current simple Makefile. Candidates are:

  • Cmake (cross-platform, but the user needs to install this)
  • Scons (same as above; nicer syntax [Python])
  • GNU Autoconf (Available on almost all *nix systems)

make test not working

After I had cloned your repository, I ran 'make' and everything was fine - no errors.
But for 'make test', I get the following output:-

env python2 -m test
Traceback (most recent call last):
File "/usr/lib/python2.7/runpy.py", line 162, in _run_module_as_main
"main", fname, loader, pkg_name)
File "/usr/lib/python2.7/runpy.py", line 72, in _run_code
exec code in run_globals
File "/home/prassanna/Development/workspace/opencv-ndarray-conversion/test.py", line 7, in
raise ImportError('Could not import the examples module. Did you run make?')
ImportError: Could not import the examples module. Did you run make?
make: *** [test] Error 1

My earlier make output is here : https://gist.github.com/4338f288aec0cabe2b2a.git

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.