LoginSignup
0
0

More than 1 year has passed since last update.

SWIGで C++ の opencv コードのPythonラッパーを作成

Posted at

SWIGインストール

Archlinuxの場合

sudo pacman -S swig

ビルド

参考 swig-and-c-shared-library

ラッパーファイル生成

OpenCVをインクルードしてるmylib.hに対してmylib.iを用意
<string>など標準ライブラリをインクルードしてるならその対応の.iも入れておく (string-arguments-are-not-recognized-by-swig)

%module mylib

// Make mylib_wrap.cxx include this header:
%{
#include "mylib.h"
%}

%include <std_string.i>
%include "mylib.h"

.iをコンパイルして

swig -c++ -python mylib.i

mylib_wrap.cxx,mylib.pyが生成される

コンパイルとリンク

生成された.cxxファイルをコンパイルして Python と Opencv をリンクする

mylib.pyimport _mylibでネイティブを呼び出すのでオブジェクトファイルの名前は _mylib.so にする

g++ -shared -fPIC -L. mylib_wrap.cxx  -o _mylib.so  `pkg-config --libs --cflags python3` `pkg-config --libs --cflags opencv4`

試しにpythonで呼び出す

import mylib
a=mylib.SomeClass()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0