mexhaardetect.cpp

来自「Face-Detector, OpenCV Haar cascade objec」· C++ 代码 · 共 126 行

CPP
126
字号
/****************************************************************************
 NJU Magic.  Copyright (c) 2007.  All Rights Reserved.            
  
--------------------------------------------------------------------
Permission to use, copy, or modify this software and its documentation
for educational and research purposes only and without fee is hereby
granted, provided that this copyright notice appear on all copies and
supporting documentation.  For any other uses of this software, in
original or modified form, including but not limited to distribution
in whole or in part, specific prior permission must be obtained from
NJU Magic and the authors.  These programs shall not be used, rewritten,
or adapted as the basis of a commercial software or hardware product
without first obtaining appropriate licenses from NJU Magic. NJU Magic 
makes no representations about the suitability of this software for any
purpose.  It is provided "as is" without express or implied warranty.

---------------------------------------------------------------------

 File: mexHaarDetect.cpp                                                                         
 Authors: Yao Wei  
 Date Created    : 2008-1-9
                                          
****************************************************************************/

// requires the OpenCV library to compile
//
//Example usage: See the matlab file mexHaarDetect.m 
//
//Compile in Matlab:
//>>mex mexHaarDetect.cpp -IOpenCV\cv\include\ -IOpenCV\cxcore\include\ OpenCV\lib_ms\cv.lib OpenCV\lib_ms\cxcore.lib



#include "mexHaarDetect.h"#include "cv.h"static CvMemStorage* storage = 0;static CvHaarClassifierCascade* cascade = 0;/* Input Arguments */#define	I_IN	prhs[0]/* Output Arguments */#define	DATA_OUT	plhs[0]#define	I_OUT	plhs[1]
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]){	int i;	int nD;    const int  *rND;
	if (nrhs != 1)	{		mexPrintf("Usage: rFaces = mexHaarDetect(facedata);\n"
			      "       -facedata:\n"
				  "                An m by n matrix of gray image.\n\n");		return;	}    // Get image size	nD=mxGetNumberOfDimensions(I_IN);    rND=mxGetDimensions(I_IN);	if ((nD != 2))// || (rND[2] != 3))	{		mexErrMsgTxt("Need single channel image");		return;	}	//convert to OpenCV	unsigned char* data=(unsigned char*) mxGetData(I_IN);	//if IplImge is used then width must be multiple of 4!!!!	CvMat frame=cvMat(rND[1],rND[0], CV_8UC1, data);	CvMat* pFrameT=cvCreateMat(rND[0],rND[1], CV_8UC1);	cvTranspose(&frame,pFrameT);//Transpose Matlab->C
	//load cascade	if (cascade == 0)		cascade = (CvHaarClassifierCascade*)cvLoad("haarcascade_frontalface_alt.xml");    if( !cascade )    {        mexErrMsgTxt("ERROR: Could not load classifier cascade\n" );        return;    }
	if(storage == 0)	storage = cvCreateMemStorage(0);
	if( !storage )
    {
        mexErrMsgTxt("ERROR: Could not allocate memory\n" );
        return;
    }    
	//detect parameters
	float fScale=1.1f;
	int nOverlap=3;

	//run the detector
	CvSeq* objects = cvHaarDetectObjects( pFrameT, cascade, storage, fScale, nOverlap);	//output the results	int rNDout[2];	rNDout[0]=4;rNDout[1]=objects->total;	// Create a matrix for the return argument	DATA_OUT = mxCreateNumericArray(2,rNDout,mxDOUBLE_CLASS, mxREAL);	double* dataout = (double*) mxGetData(DATA_OUT);    for(i = 0; i < objects->total ; i++ )    {		CvRect* r = (CvRect*)cvGetSeqElem( objects, i );		*(dataout++) = (r->x);		*(dataout++) = (r->y);		*(dataout++) = (r->width);		*(dataout++) = (r->height);    }	//release memory	//cvReleaseMemStorage(&storage);	cvReleaseMat(&pFrameT);	//cvReleaseHaarClassifierCascade( &cascade );}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?