不动如山分享 http://blog.sciencenet.cn/u/hustliaohh 脚踏实地,稳步向前

博文

mexopencv工具箱介绍

已有 5158 次阅读 2013-5-9 09:23 |个人分类:科研道路|系统分类:科研笔记| MATLAB, OpenCV, 工具箱

全文转自: http://www.cs.stonybrook.edu/~kyamagu/mexopencv/

Collection and a development kit of matlab mex functions for OpenCV library

This software package provides matlab mex functions that interface a hundred of OpenCV APIs. Also the package contains a C++ class that converts between Matlab's native data types and OpenCV data types. The package is suitable for fast prototyping of OpenCV application in Matlab, use of OpenCV as an external toolbox in Matlab, and the development of a custom mex function.

Note: The OpenCV developers are planning to implement Matlab wrappers as of February 2013. While the mexopencv will be kept as a private project, let's see how the official wrappers come out.


Download

Github

Please refer the above link for how to compile the source code. Usually it is as easy as typing mexopencv.make in matlab.

If you're using git,

Getting started

Here is an example of how simple it is to use an OpenCV function from matlab:

% Load a face detector and an image
detector = cv.CascadeClassifier('haarcascade_frontalface_alt.xml');
im = imread('myface.jpg');
% Preprocess
gr = cv.cvtColor(im, 'RGB2GRAY');
gr = cv.equalizeHist(gr);
% Detect
boxes = detector.detect(gr, 'ScaleFactor',  1.3, ...
                           'MinNeighbors', 2, ...
                           'MinSize',      [30, 30]);
% Draw results
imshow(im);
fori = 1:numel(boxes)
   rectangle('Position',  boxes{i}, ...
             'EdgeColor', 'g');
end

Would you like to use a camera input? No problem.

% Connect to a camera
camera = cv.VideoCapture();
pause(2);
fori = 1:50
   % Capture and show frame
   frame = camera.read;
   imshow(frame);
   pause(0.3);
end

The package already contains more than 150 OpenCV functions/classes. You can check a list of supported functions in the online documentation. If there isn't your favorite one, you can easily add a new mex function through MxArray class. MxArray is a data conversion utility for Matlab's native array and OpenCV data types. With this class, your mex function is as simple as the following:

#include "mexopencv.hpp"
voidmexFunction( intnlhs, mxArray *plhs[],
                 intnrhs, constmxArray *prhs[] )
{
   // Check arguments
   if(nrhs!=2 || nlhs>1)
       mexErrMsgIdAndTxt("myfunc:invalidArgs", "Wrong number of arguments");
 
   // Convert MxArray to cv::Mat and cv::Size
   cv::Mat src = MxArray(prhs[0]).toMat(), dst;
   cv::Size ksize = MxArray(prhs[1]).toSize();
 
   // Use your favorite OpenCV function
   cv::blur(src, dst, ksize);
 
   // Convert cv::Mat back to mxArray*
   plhs[0] = MxArray(dst);
}

Check the README file and the developer documentation for detail.

License

The code may be redistributed under The BSD 3-Clause License.





https://blog.sciencenet.cn/blog-507072-688061.html

上一篇:机器学习应该关注什么?
下一篇:第二讲 Learning to Answer Yes/No
收藏 IP: 122.205.9.*| 热度|

1 陈筝

该博文允许注册用户评论 请点击登录 评论 (0 个评论)

数据加载中...

Archiver|手机版|科学网 ( 京ICP备07017567号-12 )

GMT+8, 2024-4-27 17:25

Powered by ScienceNet.cn

Copyright © 2007- 中国科学报社

返回顶部