opencv中的侧脸检测

opencv中的侧脸检测

opencv中的haarcascade_profileface.xml检测时只能检测右侧脸

Opencv 2.4.3以后添加了侧脸检测,要想检测侧脸只需将加载的文件改为haarcascade_profileface.xml,这个只是识别图形上朝一边的脸,如果想要识别朝另一边的脸就需要对图片进行水平翻转,再进行检测。

int main(){
	const char *pstrImageName = "test.jpg";
	IplImage *pSrcImage = cvLoadImage(pstrImageName, 0);
	if(!pSrcImage){
		cout << pstrImageName << "can't load " << endl;
		return -1;
	}

	// 加载Haar特征检测分类器
	// haarcascade_profileface.xml系OpenCV自带的分类器 
	const char *pstrCascadeFileName = "D:/Program Files/OpenCv244/opencv/data/haarcascades/haarcascade_profileface.xml";
	CvHaarClassifierCascade *pHaarCascade = NULL;
	pHaarCascade = (CvHaarClassifierCascade*)cvLoad(pstrCascadeFileName);
	// 人脸识别与标记
	if (pHaarCascade != NULL)
	{		
		CvMemStorage *pcvMStorage = cvCreateMemStorage(0);

		CvSeq *pcvSeqFaces = cvHaarDetectObjects(pSrcImage, pHaarCascade, pcvMStorage);

		// 标记
		for(int i = 0; i total; i++)
		{
			CvRect* r = (CvRect*)cvGetSeqElem(pcvSeqFaces, i);	
			CvPoint center;
			int radius;
			center.x = cvRound((r->x + r->width * 0.5));
			center.y = cvRound((r->y + r->height * 0.5));
			radius = cvRound((r->width + r->height) * 0.25);
			cvCircle(pSrcImage, center, radius, cvScalar(255));
		}
		cvReleaseMemStorage(&pcvMStorage);
	}
	cvNamedWindow("face detection", CV_WINDOW_AUTOSIZE);
	cvShowImage("face detection", pSrcImage);
	cvWaitKey(0);

	cvDestroyAllWindows();
	cvReleaseImage(&pSrcImage);	
}


你可能感兴趣的:(人脸识别,AI,opencv)