7

Background situation

I'm trying to use the OpenCV Stitching module via the Python bindings, but I'm getting an error:

import cv2
stitcher = cv2.createStitcher(False)

imageL = cv2.imread("imageL.jpg")
imageC = cv2.imread("imageC.jpg")
imageR = cv2.imread("imageR.jpg")

stitcher.stitch((imageL, imageC))

error: /home/user/OpenCV3.1.0/opencv/modules/python/src2/cv2.cpp:163: error: (-215) The data should normally be NULL! in function allocate

Similar people suffering this:

The problem at hand

So I decided to use a official C++ OpenCV stitching example and use Python to call it using Boost.Python. However, I'm still unable to figure out how to properly use Boost.Python + numpy-opencv-converter to handle the C++ Mat vs Numpy array conversion.

¿How do I call the numpy-opencv-converter? I've only got Boost.Python in place, and when running my python function to call the C++ file I got this (expected) outcome:

$ python python_caller.py 
Traceback (most recent call last):
  File "python_caller.py", line 10, in <module>
    visualize(A)
Boost.Python.ArgumentError: Python argument types in
    testing.visualize(numpy.ndarray)
did not match C++ signature:
    visualize(cv::Mat)

Thanks.

PD: I'm in Ubuntu 14.04, Python 2.7.4 using OpenCV 3.1.0 compiled from sources and inside a virtualenv.


These are the files I'm using.

testing.cpp:

#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <boost/python.hpp>

using namespace cv;

int main(){}

Mat visualize(const cv::Mat input_image)
{
    cv::Mat image;
    image = input_image;

    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);
    waitKey(0);

    return image;
}

using namespace boost::python;
BOOST_PYTHON_MODULE(testing) // file name
{   
    def("visualize", visualize); //function name
}

python_caller.py:

import cv2
import numpy as np
from testing import visualize

A = cv2.imread("imageL.jpg")

visualize(A)

Makefile:

CFLAGS=`pkg-config --cflags opencv`

LDFLAGS=`pkg-config --libs opencv`

testing.so: testing.o
    g++ -shared -Wl,--export-dynamic -o testing.so testing.o -L/usr/lib -lboost_python -L/usr/lib/python2.7/config -lpython2.7 -L/usr/lib/x86_64-linux-gnu/ -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab 

testing.o: testing.cpp
    g++ -I/usr/include/python2.7 -I/usr/include -fPIC -c testing.cpp
Community
  • 1
  • 1
Manuel
  • 478
  • 8
  • 20

1 Answers1

7

You need to convert the Python NDArray <=> C++ cv::Mat. I can recommend this GitHub Repo. It contains an example that should fit to your needs. I am using the converter on Ubuntu 15.10 with Python 2.7/3.4 and OpenCV 3.1.

Fabian
  • 3,139
  • 2
  • 23
  • 49
  • 1
    Let me know if I can help you with code snippets, examples or something like that, but I think the example on github is very good (I had just cloned it and replaced the methods) – Fabian Aug 26 '16 at 18:49
  • I'm adding a simple function to the python_module.cpp file, also doing the include of opencv files and defining the function in the boost.python place. But I'm getting a error "ImportError: ./pbcvt.so: undefined symbol: _ZN2cv11namedWindowERKNS_6StringEi" – Manuel Aug 26 '16 at 20:33
  • This is my function: `cv::Mat visualize(cv::Mat input_image){ cv::Mat image; image = input_image; cv::namedWindow("Display Image", cv::WINDOW_AUTOSIZE ); cv::imshow("Display Image", image); cv::waitKey(0); return image; }` – Manuel Aug 26 '16 at 20:38
  • Hm I will try to reproduce your error tomorrow.. What happens if you try a simpler method first like `cv::Mat test(cv::Mat p_image) { return p_image/2 }`? – Fabian Aug 26 '16 at 22:42
  • 1
    It could be that you have to edit the CMakeLists.txt in line 27 `find_package(OpenCV COMPONENTS core REQUIRED)` and add more components like `find_package(OpenCV COMPONENTS core imgproc highgui REQUIRED)` – Fabian Aug 26 '16 at 22:49
  • I was just wondering if I should add more OpenCV components, I will try this in a couple of days and I'll let you know, thanks. – Manuel Aug 27 '16 at 00:55
  • You were right, that is what it was missing; finally it works. Thank you! – Manuel Aug 29 '16 at 21:30