I am using OpenCV via Matlab to detect faces in a video and then do some processing using Matlab. At the moment I do face detection on the IplImage-structured frames (queried by cvQueryFrame) of the video. I save each of the queried frames as a jpg and then use the face coordinates to get the ROI for the required processing. See the portion of code outlining this below.
% After reading in frame from video..
for i=1:size
img = calllib('highgui210','cvQueryFrame',cvCapture);
calllib('cxcore210','cvFlip',img,img,1);
calllib('highgui210', 'cvSaveImage', 'ThisFrame.jpg', img, ptr);
% Rest of the processing comes here..
This being the case, I feel that there should be an easier and less-crude way to convert an 'IplImage' image to a matrix or array in Matlab. Is this a possibility? If yes,how is this done?
Some pointers in this direction would be much appreciated!
Try mexing the following code:
/*
* Usage:
* img = IplImage2mxArray( cvImgPtr, releaseFlag );
*/
void mexFunction( int nout, mxArray* pout[], int nin, const mxArray* pin[]) {
if ( nin != 2 )
mexErrMsgTxt("wrong number of inputs");
if ( nout != 1 )
mexErrMsgTxt("wrong number of outputs");
IplImage* cvImg = (IplImage*)mxGetData(pin[0]); // get the pointer
// allocate the output
mwSize imgDims[3] = {cvImg->height, cvImg->width, cvImg->nChannels};
pout[0] = mxCreateNumericArray( 3, imgDims, mxDOUBLE_CLASS, mxREAL );
if ( pout[0] == NULL )
mexErrMsgTxt("out of memeory");
double* imgP = mxGetPr(pout[0]);
double divVal = pow(2.0, cvImg->depth) - 1;
double releaseFlag = mxGetScalar( pin[1] );
for ( int x = 0 ; x < cvImg->width; x++ ) {
for ( int y = 0 ; y < cvImg->height; y++ ) {
CvScalar s = cvGet2D(cvImg, y, x);
for (int c = 0; c < cvImg->nChannels; c++) {
imgP[ y + x * cvImg->height + c * cvImg->height * cvImg->width ] = s.val[c] / divVal;
}
}
}
if ( releaseFlag != 0 ) {
cvReleaseImage( &cvImg );
}
}
You'll end up with a function (mex) IplImage2mxArray, use it in Matlab:
>> cvImg = calllib('highgui210','cvQueryFrame',cvCapture);
>> img = IplImage2mxArray( cvImg, false ); % no cvReleaseImage after cvQueryFrame
Due to internal opencv representations the channels of img might be permuted (BGR instread of RGB). Also note that img might contain four channels - an additional alpha channel.
-Shai