Returning a Opencv Mat type from my Python Code

拥有回忆 提交于 2019-12-11 11:16:31

问题


I tried to return the (c++ opencv Mat) from my opencv python code

Steps i done:

A. Generated .so file for my opencv C++ program by using SWIG

B. Wrote a sample Opencv Python Program

A.Generating .so for my opencv C++ Code:

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

Done the following Commands ,Generated the .so file Successfully for my C++ Program:

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. Wrote a sample Opencv Python Program to return a 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()

I am getting this error when i tried to execute sample.py on my machine

Traceback (most recent call last): File "sample1.py", line 15, in res=opencvtest.my(img) TypeError: in method 'my', argument 1 of type 'cv::Mat'

Any one of the expert can help me to clear the issue.


回答1:


In the c++ code you are using a cv::Mat datatype. As in python you also use openCV, I can imagine that you also expect to get the same datatype. But in python openCV uses an numpy array as image format.

In the following link, they speak about a numpy dataformat under c++. I can imagine that you use this dataformat to communicate between python and C++. In C++ you need to do the conversion from numpy to cv::Mat then.

https://scipy-lectures.github.io/advanced/interfacing_with_c/interfacing_with_c.html




回答2:


Posting just in case someone will face the same issue:

Using SWIG and https://github.com/renatoGarcia/opencv-swig to deal with passing cv::Mat I ended up with

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 (with opencv-swig library placed at the same folder as script)

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)

And these commands

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


来源:https://stackoverflow.com/questions/28939962/returning-a-opencv-mat-type-from-my-python-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!