Ask Your Question
1

Pass cv2.Mat (python) object to cv::Mat (c++) with SWIG

asked 2014-10-03 09:15:11 -0600

carlosb gravatar image

updated 2014-10-03 09:16:08 -0600

I am trying to pass from Python a cv2 Mat object to my SWIG wrapper for C++. My module in C++ is this:.

#ifndef SYMBOL_TRACKER
#define SYMBOL_TRACKER
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <memory>
#include <cmath>

#include "core.h"
#include "symbol_descriptor.h"
#include "symbol_matcher.h"

namespace cvc {

    class SymbolTracker: public  core::Tracker  {
        private:
            /* load configuration */
            void loadConfiguration();
            void initialization();

        public:
            SymbolTracker(std::vector<std::string> modelImageFiles
                ,std::string orbFile_=cvc::ORB_FILE
                ,std::string flannFile_=cvc::FLANN_FILE) ;
            core::ResultResponse process(cv::Mat & colorFrame, cv::Mat & grayFrame);

    };


}

#endif

the SWIG file:

/* File : tracker.i */
%module symbol_tracker
%include "std_string.i"
%include "std_vector.i"

namespace std {
   %template(vectors) vector<std::string>;
};

%{
#include "symbol_tracker.h"
%}

/* Let's just grab the original header file here */
/*%include "core.h" */
%include "core.h"
%include "symbol_tracker.h"

And the python script to test the wrapper:

#!/bin/sh
import symbol_tracker
import numpy as np
import cv2

img = cv2.imread('test_resources/model_image.bmp')
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

listModels = symbol_tracker.vectors()
listModels.push_back(symbol_tracker.MODEL_IMAGE_FILE)

tracker = symbol_tracker.SymbolTracker(listModels,symbol_tracker.ORB_FILE,symbol_tracker.FLANN_FILE)

grayImage = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
response = tracker.process(img,grayImage)

I get an error to use an incompatible argument:

Traceback (most recent call last):
  File "symbol_tracker_test.py", line 17, in <module>
    response = tracker.process(img,grayImage)
TypeError: in method 'SymbolTracker_process', argument 2 of type 'cv::Mat &'

But I don't know how I could parse my grayImage and img argument in order to call correctly the function...

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-08-10 07:36:34 -0600

Swig does not convert the opencv datatypes from python to C++. For that, some interfacing/mapping has to be done. Opencv-SWIG library does this job for you and this instruction make things easy.

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-10-03 09:15:11 -0600

Seen: 2,649 times

Last updated: Oct 03 '14