python 调用 c函数 numpy 数组_从Python调用C函数并将OpenCV Mat转换为Numpy数组

Background situation

我试图通过Python绑定使用OpenCV Stitching模块,但是我收到一个错误:

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))

错误:/home/user/OpenCV3.1.0/opencv/modules/python/src2/cv2.cpp:163:错误:( - 215)数据通常应为NULL!在函数分配

类似的人遭受这种情况

The problem at hand

所以我决定使用官方的C OpenCV拼接示例并使用Python使用Boost.Python来调用它 . 但是,我仍然无法弄清楚如何正确使用Boost.Python numpy-opencv-converter来处理C Mat vs Numpy数组转换 .

¿How do I call the numpy-opencv-converter? 我只有Boost.Python,当运行我的python函数来调用C文件时,我得到了这个(预期的)结果:

$ python python_caller.py

Traceback (most recent call last):

File "python_caller.py", line 10, in

visualize(A)

Boost.Python.ArgumentError: Python argument types in

testing.visualize(numpy.ndarray)

did not match C++ signature:

visualize(cv::Mat)

谢谢 .

PD:我在Ubuntu 14.04,Python 2.7.4使用OpenCV 3.1.0从源代码编译并在virtualenv内部编译 .

这些是我正在使用的文件 .

testing.cpp:

#include

#include

#include

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

你可能感兴趣的:(python,调用,c函数,numpy,数组)