背景情况呼叫从Python的C++函数和OpenCV的垫转换为numpy的阵列
我试图通过Python绑定使用OpenCV的拼接模块,但我得到一个错误:
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
相似的人患此:
- https://stackoverflow.com/a/36646256/1253729
- How to stitch images from a UAV using opencv python with Stitcher class
- https://github.com/opencv/opencv/issues/6969
手头的问题
所以我决定使用一个正式的C++ OpenCV的拼接例子,使用Python使用Boost.Python的调用它。但是,我仍然无法弄清楚如何正确使用Boost.Python + numpy-opencv-converter来处理C++ Mat和Numpy数组的转换。
¿我怎样称之为numpy-opencv转换器?我只带了Boost.Python的到位,并运行我的Python功能时调用C++文件,我得到这个(预期)结果:
$ 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)
感谢。
PD:我在Ubuntu 14.04中,Python 2.7.4使用从源代码和virtualenv编译的OpenCV 3.1.0。
这些是我正在使用的文件。
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
我正在研究此问题,谢谢,一旦我开始工作,我会将其标记为已接受的答案。 – mclzc
让我知道,如果我可以帮助你的代码片段,例子或类似的东西,但我认为在github上的例子是非常好的(我刚刚克隆它,并取代了方法) – Fabian
我添加一个简单的函数python_module.cpp文件,还做了opencv文件的包含并在boost.python的地方定义了函数。但是我收到一个错误“ImportError:./pbcvt。所以:未定义的符号:_ZN2cv11namedWindowERKNS_6StringEi” – mclzc