OpenCV Matlab Code Generator
============================
This module contains a code generator to automatically produce Matlab mex wrappers for other modules within the OpenCV library. Once compiled and added to the Matlab path, this gives users the ability to call OpenCV methods natively from within Matlab.
Build
-----
The Matlab code generator is fully integrated into the OpenCV build system. If cmake finds a Matlab installation available on the host system while configuring OpenCV, it will attempt to generate Matlab wrappers for all OpenCV modules. If cmake is having trouble finding your Matlab installation, you can explicitly point it to the root by defining the `MATLAB_ROOT_DIR` variable. For example, on a Mac you could type:
cmake -DMATLAB_ROOT_DIR=/Applications/MATLAB_R2013a.app ..
If you prefer using the gui version of cmake (cmake-gui), you can use the *Add Entry* option in the GUI to manually add the *path* variable `MATLAB_ROOT_DIR`.
Install
-------
In order to use the bindings, you will need to add them to the Matlab path. The path to add is:
1. `${CMAKE_BUILD_DIR}/modules/matlab` if you are working from the build tree, or
2. `${CMAKE_INSTALL_PREFIX}/matlab` if you have installed OpenCV
In Matlab, simply run:
addpath('/path/to/opencv/matlab/');
Run
---
Once you've added the bindings directory to the Matlab path, you can start using them straight away! OpenCV calls need to be prefixed with a 'cv' qualifier, to disambiguate them from Matlab methods of the same name. For example, to compute the dft of a matrix, you might do the following:
```matlab
% load an image (Matlab)
I = imread('cameraman.tif');
% compute the DFT (OpenCV)
If = cv.dft(I, cv.DFT_COMPLEX_OUTPUT);
```
As you can see, both OpenCV methods and constants can be used with 'cv' qualification. You can also call:
help cv.dft
to get help on the purpose and call signature of a particular method, or
help cv
to get general help regarding the OpenCV bindings. If you ever run into issues with the bindings
cv.buildInformation();
will produce a printout of diagnostic information pertaining to your particular build of OS, OpenCV and Matlab. It is useful to submit this information alongside a bug report to the OpenCV team.
Writing your own mex files
--------------------------
The Matlab bindings come with a set of utilities to help you quickly write your own mex files using OpenCV definitions. By doing so, you have all the speed and freedom of C++, with the power of OpenCV's math expressions and optimizations.
The first thing you need to learn how to do is write a mex-file with Matlab constructs. Following is a brief example:
```cpp
// include useful constructs
// this automatically includes opencv core.hpp and mex.h)
#include <opencv2/matlab/bridge.hpp>
using namespace cv;
using namespace matlab;
using namespace bridge;
// define the mex gateway
void mexFunction(int nlhs, mxArray* plhs[],
int nrhs, const mxArray* prhs[]) {
// claim the inputs into scoped management
MxArrayVector raw(prhs, prhs+nrhs);
// add an argument parser to automatically handle basic options
ArgumentParser parser("my function");
parser.addVariant("variant1", 1, 1, "opt");
MxArrayVector reordered = parser.parse(raw);
// if we get here, we know the inputs are valid and reordered. Unpack...
BridgeVector inputs(reordered.begin(), reordered.end());
Mat required = inputs[0].toMat();
string optional = inputs[1].empty() ? "Default string" : inputs[1].toString();
try {
// Do stuff...
} catch(Exception& e) {
error(e.what());
} catch(...) {
error("Uncaught exception occurred");
}
// allocate an output
Bridge out;
out = required;
plhs[0] = out.toMxArray().releaseOwnership();
}
```
There are a couple of important things going on in this example. Firstly, you need to include `<opencv2/matlab/bridge.hpp>` to enable the bridging capabilities. Once you've done this, you get some nice utilities for free. `MxArray` is a class that wraps Matlab's `mxArray*` class in an OOP-style interface. `ArgumentParser` is a class that handles default, optional and named arguments for you, along with multiple possible calling syntaxes. Finally, `Bridge` is a class that allows bidirectional conversions between OpenCV/std and Matlab types.
Once you have written your file, it can be compiled with the provided mex utility:
cv.mex('my_function.cpp');
This utility automatically links in all of the necessary OpenCV libraries to make your function work.
NOTE: OpenCV uses exceptions throughout the codebase. It is a **very** good idea to wrap your code in exception handlers to avoid crashing Matlab in the event of an exception being thrown.
------------------------------------------------------------------
Developer
=========
The following sections contain information for developers seeking to use, understand or extend the Matlab bindings. The bindings are generated in python using a powerful templating engine called Jinja2. Because Matlab mex gateways have a common structure, they are well suited to templatization. There are separate templates for formatting C++ classes, Matlab classes, C++ functions, constants (enums) and documentation.
The task of the generator is two-fold:
1. To parse the OpenCV headers and build a semantic tree that can be fed to the template engine
2. To define type conversions between C++/OpenCV and Matlab types
Once a source file has been generated for each OpenCV definition, and type conversions have been established, the mex compiler is invoked to produce the mex gateway (shared object) and link in the OpenCV libraries.
File layout
-----------
opencv/modules/matlab (this module)
* `CMakeLists.txt` (main cmake configuration file)
* `README.md` (this file)
* `compile.cmake` (the cmake script for compiling generated source code)
* `generator` (the folder containing generator code)
* `filters.py` (template filters)
* `gen_matlab.py` (the binding generator control script)
* `parse_tree.py` (python class to refactor the hdr_parser.py output)
* `templates` (the raw templates for populating classes, constants, functions and docs)
* `include` (C++ headers for the bindings)
* `mxarray.hpp` (C++ OOP-style interface for Matlab mxArray* class)
* `bridge.hpp` (type conversions)
* `map.hpp` (hash map interface for instance storage and method lookup)
* `test` (generator, compiler and binding test scripts)
Call Tree
---------
The cmake call tree can be broken into 3 main components:
1. configure time
2. build time
3. install time
**Find Matlab (configure)**
The first thing to do is discover a Matlab installation on the host system. This is handled by the `OpenCVFindMatlab.cmake` in `opencv/cmake`. On Windows machines it searches the registry and path, while on *NIX machines it searches a set of canonical install paths. Once Matlab has been found, a number of variables are defined, such as the path to the mex compiler, the mex libraries, the mex include paths, the architectural extension, etc.
**Test the generator (configure)**
Attempt to produce a source file for a simple definition. This tests whether python and pythonlibs are correctly invoked on the host.
**Test the mex compiler (configure)**
Attempt to compile a simple definition using the mex compiler. A mex file is actually just a shared object with a special exported symbol `_mexFunction` which serves as the entry-point to the function. As such, the mex compiler is just a set of scripts configuring the system compiler. In most cases this is the same as the OpenCV compiler, but *could* be different. The test checks whether the mex and generator includes can be found, the system libraries can be linked and the passed compiler flags are compatible.
If any of the configure time tests fail, the bindings will be disabled, but the main OpenCV configure will continue without error. The configuration summary will contain the block:
Matlab
me
没有合适的资源?快使用搜索试试~ 我知道了~
opencv_contrib-3.4.1.zip
共1769个文件
cpp:671个
hpp:414个
jpg:164个
需积分: 13 43 下载量 49 浏览量
2019-03-10
20:17:16
上传
评论 1
收藏 55.68MB ZIP 举报
温馨提示
此资源要配合opencv-3.4.1使用,有些模块包含在称为opencv_contrib的资源库中,强烈安装这些模块,因为它们含有opencv没有的功能。此资源在win10亲测有效。使用方法请看我的文章
资源推荐
资源详情
资源评论
收起资源包目录
opencv_contrib-3.4.1.zip (1769个子文件)
[Tutorial] Adding new Tracker Method for dummies 3KB
ximgproc.bib 9KB
face.bib 5KB
xfeatures2d.bib 4KB
tracking.bib 4KB
optflow.bib 2KB
fuzzy.bib 2KB
bioinspired.bib 2KB
text.bib 2KB
dnn_objdetect.bib 1KB
line_descriptor.bib 1KB
saliency.bib 1KB
bgsegm.bib 937B
surface_matching.bib 889B
structured_light.bib 862B
dpm.bib 686B
img_hash.bib 636B
cnn_3dobj.bib 597B
xobjdetect.bib 547B
xphoto.bib 542B
phase_unwrapping.bib 371B
hfs.bib 350B
reg.bib 286B
triplet_mean.binaryproto 16KB
mnist_mean.binaryproto 3KB
desktop.blend 934KB
backyard.blend 737KB
imgKittyl.bmp 457KB
imgKitty.bmp 457KB
rezult0l.bmp 192KB
rezult0.bmp 192KB
imL2.bmp 109KB
groundtruth.bmp 109KB
imL2l.bmp 109KB
3d_triplet_iter_30000.caffemodel 1.17MB
euclidean_resection.cc 27KB
bundle.cc 23KB
fundamental.cc 17KB
homography.cc 16KB
keyframe_selection.cc 16KB
pipeline.cc 14KB
camera_intrinsics.cc 10KB
nRobustViewMatching.cc 10KB
resect.cc 9KB
distortion_models.cc 8KB
intersect.cc 8KB
initialize_reconstruction.cc 6KB
projection.cc 6KB
reconstruction.cc 6KB
tracks.cc 6KB
feature_matching.cc 5KB
panography.cc 4KB
fundamental_kernel.cc 4KB
conditioning.cc 3KB
numeric.cc 3KB
matches.cc 3KB
robust_fundamental.cc 3KB
twoviewtriangulation.cc 3KB
reconstruction_scale.cc 3KB
robust_resection.cc 2KB
triangulation.cc 2KB
panography_kernel.cc 2KB
robust_estimation.cc 1KB
poly.cc 1KB
surf.cl 60KB
retina_kernel.cl 24KB
dis_flow.cl 20KB
tldDetector.cl 4KB
sparse_matching_gpc.cl 2KB
tmm.cl 2KB
anisodiff.cl 1KB
updatemotionhistory.cl 1KB
.clang-format 1KB
FindGflags.cmake 27KB
FindGlog.cmake 8KB
compile.cmake 2KB
download_boostdesc.cmake 1KB
FindNNPACK.cmake 1KB
FindTinyDNN.cmake 1KB
download_vgg.cmake 1KB
FindTesseract.cmake 641B
FindCaffe.cmake 345B
Installation.cmake 250B
FindGlog.cmake 164B
featureColorName.cpp 3.34MB
erfilter.cpp 152KB
bagofwords_classification.cpp 115KB
binary_descriptor.cpp 104KB
omnidir.cpp 82KB
dis_flow.cpp 67KB
aruco.cpp 66KB
retina_ocl.cpp 64KB
linemod.cpp 59KB
slic.cpp 57KB
variational_refinement.cpp 56KB
lsc.cpp 55KB
odometry.cpp 53KB
test_f1.cpp 52KB
daisy.cpp 52KB
tinyxml2.cpp 52KB
共 1769 条
- 1
- 2
- 3
- 4
- 5
- 6
- 18
资源评论
Turf96
- 粉丝: 26
- 资源: 6
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功