As I mentioned in previous comments, your code is using the old C interface of OpenCV. It is preferred to use the newer C++ interface.
Perhaps it is best to show a complete example. The code below runs face detecting using the Haar-based cascade classifier. I simplified it from your code a bit by removing some of the parameters parsing.
If you want an easier wrapper for using OpenCV inside MATLAB, consider using mexopencv
.
faces.cpp
#include "mex.h"
#include <string>
#include "opencv2/opencv.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/imgproc/imgproc.hpp"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
// validate arguments
if (nrhs < 2) {
mexErrMsgTxt("Wrong number of input arguments.");
}
if (nlhs > 1) {
mexErrMsgTxt("Too many output arguments.");
}
if (!mxIsChar(prhs[0]) || mxGetM(prhs[0])!=1) {
mexErrMsgTxt("First argument must be a string.");
}
if (!mxIsUint8(prhs[1]) || mxGetNumberOfDimensions(prhs[0])!=2) {
mexErrMsgTxt("Second argument must be a uint8 grayscale image.");
}
// get XML cascade file name
char *xmlfile = mxArrayToString(prhs[0]);
cv::CascadeClassifier cascade;
if (!cascade.load(std::string(xmlfile))) {
mexErrMsgTxt("Failed to load cascade classifier.");
}
mxFree(xmlfile);
// get grayscale image
mwSize nrows = mxGetM(prhs[1]);
mwSize ncols = mxGetN(prhs[1]);
uint8_T *data = reinterpret_cast<uint8_T*>(mxGetData(prhs[1]));
// copy into an OpenCV mat (there are better ways to do this step!)
cv::Mat img(nrows, ncols, CV_8UC1, cv::Scalar::all(0));
for(mwIndex c=0; c<ncols; c++) {
for(mwIndex r=0; r<nrows; r++) {
img.at<char>(r,c) = data[r + nrows*c];
}
}
// process image before detection
cv::equalizeHist(img, img);
// detect faces
std::vector<cv::Rect> faces;
cascade.detectMultiScale(img, faces, 1.1, 4, 0, cv::Size(30,30));
// return rectangles found to MATLAB
plhs[0] = mxCreateDoubleMatrix(4, faces.size(), mxREAL);
double *out = mxGetPr(plhs[0]);
for(mwIndex i=0; i<faces.size(); i++) {
out[i+0] = static_cast<double>(faces[i].x);
out[i+1] = static_cast<double>(faces[i].y);
out[i+2] = static_cast<double>(faces[i].width);
out[i+3] = static_cast<double>(faces[i].height);
}
}
Assuming you have download OpenCV 2.4.7 and extracted it into C:\OpenCV
(with sources
and build
sub-directories beneath it), run the following command to compile the code:
mex -largeArrayDims -I'C:\OpenCV\build\include' -L'C:\OpenCV\build\x64\vc11\lib'
-lopencv_core247 -lopencv_imgproc247 -lopencv_objdetect247 faces.cpp
(adjust the libraries path above according to your compiler. I'm using VS2012)
Next we test the MEX-function in MATLAB:
% some grayscale face image
img = imread('http://www.ece.rice.edu/~wakin/images/lena512.bmp');
% detect face
rect = faces('./haarcascade_frontalface_alt2.xml', img);
% show result
imshow(img)
rectangle('Position',rect(:,1), 'LineWidth',4, 'EdgeColor','g')