我试图从我的opencv python代码中返回(c ++ opencv Mat)
我完成的步骤:
一个。使用SWIG生成我的opencv C ++程序的.so文件
B中。写了一个示例Opencv Python程序
A.为我的opencv C ++代码生成.so:
opencvtest.cpp
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int my(Mat image)
{
cv::Mat out;
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
}
cvtColor(image, out,COLOR_BGR2GRAY);
int z=10;
return z;
}
opencvtest.i
%module opencvtest
%{
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv/cv.h>
extern cv::Mat my(cv::Mat image);
%}
%include "typemaps.i"
%typemap(jstype) cv::Mat& "org.opencv.core.Mat"
%typemap(jstype) cv::Mat "org.opencv.core.Mat"
%typemap(javain) cv::Mat "$javainput.getNativeObjAddr()"
%typemap(javain) cv::Mat& "$javainput.getNativeObjAddr()"
%typemap(jtype) cv::Mat& "long"
%typemap(jni) cv::Mat& "jlong"
extern cv::Mat my(cv::Mat image);
完成以下命令,为我的C ++程序成功生成.so文件:
1.swig -c++ -python opencvtest.i
2.g++ -fpic -c opencvtest.cpp opencvtest_wrap.cxx -I/usr/include/python2.7 -I/usr/local/include -I/usr/local/include/opencv -I/usr/local/include/opencv2
3.g++ -shared opencvtest.o opencvtest_wrap.o -o _opencvtest.so -L/usr/local/lib /usr/local/lib/libopencv_calib3d.so /usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_features2d.so /usr/local/lib/libopencv_flann.so /usr/local/lib/libopencv_highgui.so /usr/local/lib/libopencv_imgcodecs.so /usr/local/lib/libopencv_imgproc.so /usr/local/lib/libopencv_ml.so /usr/local/lib/libopencv_objdetect.so /usr/local/lib/libopencv_photo.so /usr/local/lib/libopencv_shape.so /usr/local/lib/libopencv_stitching.so /usr/local/lib/libopencv_superres.so /usr/local/lib/libopencv_ts.a /usr/local/lib/libopencv_video.so /usr/local/lib/libopencv_videoio.so /usr/local/lib/libopencv_videostab.so
B中。写了一个示例Opencv Python程序来返回一个opencv c ++ Mat:
Sample.py
from __future__ import division
import cv2 , numpy
import opencvtest
img = cv2.imread('/home/hubino/Desktop/python/MyPic.jpg')
screen_res = 1280, 720
scale_width = screen_res[0] / img.shape[1]
scale_height = screen_res[1] / img.shape[0]
scale = min(scale_width, scale_height)
window_width = int(img.shape[1] * scale)
window_height = int(img.shape[0] * scale)
res=opencvtest.sample(img)
cv2.imshow('Source Image',img)
cv2.waitKey(0)
cv2.namedWindow('dst_rt', cv2.WINDOW_NORMAL)
cv2.resizeWindow('dst_rt', window_width, window_height)
cv2.imshow('dst_rt', res)
cv2.waitKey(0)
cv2.destroyAllWindows()
当我尝试在我的机器上执行sample.py时,我收到此错误
追踪(最近一次通话): 文件&#34; sample1.py&#34;,第15行,在res = opencvtest.my(img) TypeError:在方法&#39; my&#39;,类型&#39; cv :: Mat&#39;
中的参数1任何一位专家都可以帮我解决问题。
答案 0 :(得分:0)
在c ++代码中,您使用的是cv :: Mat数据类型。在python中你也使用openCV,我可以想象你也期望得到相同的数据类型。 但在python中,openCV使用numpy数组作为图像格式。
在下面的链接中,他们谈到了c ++下的numpy数据格式。我可以想象你使用这个数据格式在python和C ++之间进行通信。在C ++中,你需要进行从numpy到cv :: Mat的转换。
https://scipy-lectures.github.io/advanced/interfacing_with_c/interfacing_with_c.html
答案 1 :(得分:0)
发帖以防万一有人遇到相同的问题:
使用SWIG和https://github.com/renatoGarcia/opencv-swig来处理我最终通过的cv :: Mat
my_lib.hpp
#include <opencv2/core.hpp>
#include <iostream>
cv::Mat getImage(cv::Mat& image)
{
std::cout << "image size " << image.rows << " " << image.cols << std::endl;
return image;
}
my_lib.i
%module my_lib
%include <opencv.i>
%cv_instantiate_all_defaults
%{
#include "my_lib.hpp"
#include <opencv2/core.hpp>
#include <iostream>
%}
%include "my_lib.hpp"
test.py(将opencv-swig库与脚本放置在同一文件夹中)
import my_lib
import cv2
import numpy as np
import sys
sys.path.append('./opencv-swig-master/test/build/')
import mat
img = cv2.imread("test.jpg")
new_img = my_lib.getImage(mat.Mat.from_array(img))
new_img = np.asarray(new_img)
cv2.imwrite("test_out.jpg", new_img)
这些命令
swig -I<path to opencv-swig-master/lib/> -I<path to opencv2 include folder> -c++ -python my_lib.i
g++ -shared -fpic my_lib_wrap.cxx $(pkg-config --cflags --libs python3) $(pkg-config --libs opencv) -o _my_lib.so