# OpenCV-SWIG
[![CI.badge]][CI.link]
[![LICENSE.badge]][LICENSE.link]
## A SWIG library for OpenCV types
This library is a collection of [SWIG](http://swig.org/) interface files with rules to
wrap [OpenCV](https://opencv.org/) C++ types as [Python](https://www.python.org/) classes.
The OpenCV itself has a Python binding exposing its API, this library is not that.
## Hello, world!
The use case for this library is when someone writing a C++ library, which uses OpenCV
types on its public API, wants create a Python binding to that library.
Let's suppose you have built a C++ library named `MyLib` having a `my_lib.hpp` header:
```C++
#ifndef MY_LIB_HPP_INCLUDED
#define MY_LIB_HPP_INCLUDED
#include <opencv2/core.hpp>
#include <iostream>
inline auto moveTo(cv::Point const& p) -> void
{
std::cout << "cv::Point moved" << std::endl;
}
inline auto getImage() -> cv::Mat3b
{
return cv::Mat3b(3, 5);
}
#endif /* MY_LIB_HPP_INCLUDED */
```
To generate a `MyLib` Python binding all you need is a `my_lib.i` SWIG file as:
```swig
%module my_lib
%include <opencv.i>
%cv_instantiate_all_defaults
%{
#include "my_lib.hpp"
%}
%include "my_lib.hpp"
```
and a `CMakeLists.txt`:
```cmake
cmake_minimum_required(VERSION 3.0)
cmake_policy(SET CMP0074 NEW)
cmake_policy(SET CMP0078 NEW)
cmake_policy(SET CMP0086 NEW)
project(MyLib)
find_package(OpenCV-SWIG REQUIRED)
find_package(SWIG REQUIRED COMPONENTS python)
find_package(Boost REQUIRED)
find_package(OpenCV REQUIRED core)
find_package(Python REQUIRED COMPONENTS Interpreter Development)
include(UseSWIG)
set_property(SOURCE my_lib.i PROPERTY CPLUSPLUS ON)
swig_add_library(my_lib LANGUAGE python SOURCES my_lib.i my_lib.hpp)
set_property(
TARGET my_lib
PROPERTY SWIG_INCLUDE_DIRECTORIES
${OpenCV-SWIG_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
)
target_include_directories(my_lib
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
${Python_INCLUDE_DIRS}
${OpenCV_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)
target_link_libraries(my_lib
${OpenCV_LIBRARIES}
)
```
After calling:
```shell
mkdir build
cd build
cmake .. -DOpenCV-SWIG_ROOT=</path/to/OpenCV-SWIG/install/dir/if/not/on/system/dir>
make
```
A Python module `my_lib.py` will be created on the current directory, and the code bellow
should work just fine:
```Python
import my_lib
import numpy as np
import cv2
p0 = my_lib.Point(13, 17)
my_lib.moveTo(p0)
cv_img = my_lib.getImage()
np_arr = cv2.blur(np.asarray(cv_img), (3, 3))
cv_img2 = my_lib.Mat3b.from_array(np_arr)
```
## Install
To install, execute on the package root dir:
```shell
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=</path/to/install/dir>
make install
```
### Nix
If using [Nix](https://nixos.org/), any Git commit can be used as `buildInputs` with:
```text
opencv-swig = pkgs.callPackage (
fetchTarball https://github.com/renatoGarcia/opencv-swig/archive/<git-commit-hash>.tar.gz
) {};
```
To built the [Hello, World!](#hello-world) example, a `shell.nix` as below will set a
correct environment:
```nix
let
pkgs = import <nixpkgs> {};
opencv-swig = pkgs.callPackage (
fetchTarball https://github.com/renatoGarcia/opencv-swig/archive/v1.0.1.tar.gz
) {};
in pkgs.mkShell {
buildInputs = [
opencv-swig
pkgs.boost
pkgs.swig
pkgs.cmake
pkgs.pythonPackages.opencv
];
}
```
## Supported OpenCV versions
This library is known to work with all versions of OpenCV since 2.4.11. Before that no
tests were performed.
Currently all new commits are tested against OpenCV versions 3.3, 3.4, 4.0, 4.1, 4.2 and
4.3. Any new release beyond that range is expected to work, and new tests will be added
when due, but no guarantee can be done beforehand.
When used with an OpenCV version outside of that range, a warning like *"Warning 972:
Using an untested OpenCV version"* will be emitted. To silent it call SWIG with the flag
`-w972`.
[CI.badge]: https://github.com/renatoGarcia/opencv-swig/workflows/CI/badge.svg
[CI.link]: https://github.com/renatoGarcia/opencv-swig/actions?query=workflow%3ACI
[LICENSE.badge]: https://img.shields.io/badge/licence-BSD-blue
[LICENSE.link]: https://raw.githubusercontent.com/renatoGarcia/opencv-swig/master/LICENSE
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
hkhkhk_34160.zip (469个子文件)
libcrypto.so.1.0.0 2.37MB
libssl.so.1.0.0 9B
libopenal.so.1 1.87MB
libz.so.1 7B
libiconv.so.2 12B
AUTHORS 151B
feature_tests.bin 12KB
CMakeDetermineCompilerABI_CXX.bin 8KB
CMakeDetermineCompilerABI_C.bin 8KB
login.bmp 1KB
tree.bmp 822B
camera.bmp 822B
play.bmp 822B
logout.bmp 246B
CMakeCCompilerId.c 18KB
feature_tests.c 688B
hkread.cbp 786B
cmake.check_cache 85B
设备网络SDK使用手册.chm 9.41MB
播放库编程指南_linux V7.3.3.chm 487KB
.classpath 483B
Makefile.cmake 7KB
CMakeCXXCompiler.cmake 5KB
CMakeCCompiler.cmake 2KB
cmake_install.cmake 1KB
DependInfo.cmake 735B
CMakeDirectoryInformation.cmake 622B
CMakeSystem.cmake 398B
cmake_clean.cmake 245B
qtclientdemo.cpp 91KB
playback.cpp 80KB
realplay.cpp 58KB
cchannelparams.cpp 43KB
cremoteparams.cpp 28KB
calarmparamsdlg.cpp 22KB
devicepan.cpp 21KB
cadvancednetparams.cpp 20KB
CMakeCXXCompilerId.cpp 17KB
audiotransfer.cpp 16KB
ConfigParams.cpp 16KB
cipparams.cpp 15KB
cuserparams.cpp 15KB
playback.cpp 13KB
logsearch.cpp 12KB
cmotion.cpp 12KB
charddiskparams.cpp 12KB
serialtransfer.cpp 11KB
publicfuc.cpp 10KB
devicestate.cpp 10KB
cserialparams.cpp 10KB
devicedata.cpp 9KB
chidealarm.cpp 9KB
calarminhandletype.cpp 9KB
channeldata.cpp 8KB
cvilost.cpp 8KB
HKIPcamera.cpp 8KB
importconfig.cpp 8KB
ptzcruise.cpp 8KB
cconfigureparams.cpp 7KB
exceptionparams.cpp 7KB
catmparams.cpp 7KB
remoteupdate.cpp 7KB
cdlgremoterecordschedule.cpp 6KB
GetStream.cpp 6KB
catmnetparams.cpp 6KB
deploy.cpp 6KB
czonedst.cpp 5KB
timing.cpp 5KB
cshelter.cpp 5KB
treeitem.cpp 5KB
formatdisk.cpp 5KB
ptzpreset.cpp 4KB
Alarm.cpp 4KB
calarminptz.cpp 4KB
main.cpp 4KB
treemodel.cpp 3KB
cpppcfgdlg.cpp 3KB
cnfsparams.cpp 3KB
addnode.cpp 3KB
consoleMain.cpp 3KB
Voice.cpp 3KB
FramePlayWnd.cpp 3KB
shutdowndevice.cpp 3KB
ccompressaudio.cpp 3KB
rebootdevice.cpp 3KB
resetdevice.cpp 3KB
crtspparams.cpp 2KB
CapPicture.cpp 2KB
deviceattr.cpp 2KB
channelattr.cpp 1KB
main.cpp 1KB
audiobroadcast.cpp 1KB
audiointercom.cpp 1KB
exitdemo.cpp 1KB
logtable.cpp 1KB
tool.cpp 703B
main.cpp 505B
Display.cpp 396B
alarmtable.cpp 142B
main.cpp 108B
共 469 条
- 1
- 2
- 3
- 4
- 5
资源评论
Soochow_NJU_Smile
- 粉丝: 16
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功