Returning A Opencv Mat Type From My Python Code
Solution 1:
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
Solution 2:
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
Post a Comment for "Returning A Opencv Mat Type From My Python Code"