|||
全文转自: 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.
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,
git clone git://github.com/kyamagu/mexopencv.git |
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.
LicenseThe code may be redistributed under The BSD 3-Clause License.
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-9-21 03:33
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社