将Mat参数从Python传递给c ++方法

时间:2015-03-10 16:00:42

标签: python c++ swig

我试图从我的 opencv python 代码

将参数传递给(c ++ opencv)

我完成的步骤:

  1. 使用SWIG为我的opencv C ++程序生成.so文件
  2. 编写了一个示例 Opencv Python程序
  3. A. 为我的opencv 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

    4.C ++代码

    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <opencv2/imgproc/imgproc.hpp>
    #include <iostream>
    
    using namespace cv;
    using namespace std;
    
    int sample(Mat image,Mat &out)
    {
     int z=0;
        if(! image.data )                              // Check for invalid input
       {
           cout <<  "Could not open or find the image" << std::endl ;
    
    
       }
      cvtColor(image, out,COLOR_BGR2GRAY);
                                     // Wait for a keystroke in the window
        return z;
    }
    

    5.Created Wrapper文件(opencvtest_wrap.cxx)和生成的.so文件。

    6.Sample python代码调用C ++代码。

     import cv2 , numpy
        import opencvtest
    image = cv2.imread('messi5.jpg',0)
        res=opencvtest.sample(image , image1)
        cv2.imshow('Source Image',image)
        cv2.waitKey(0)
        cv2.namedWindow('dst_rt', cv2.WINDOW_NORMAL)
        cv2.resizeWindow('dst_rt', window_width, window_height)
        cv2.imshow('dst_rt', image1)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
        print res
    

    当我尝试在我的机器上执行sample.py时,我收到此错误

    追踪(最近一次呼叫最后一次):

    文件&#34; sample1.py&#34;,第15行,in     res = opencvtest.sample(image,image1)

    TypeError:在方法&#39;示例&#39;,参数1的类型&#39; cv :: Mat&#39;

    任何一位专家都可以帮我解决问题。

1 个答案:

答案 0 :(得分:1)

什么是image?我认为你必须使用来自C ++的类型:opencvtest.Mat