|
发表于 2015-10-28 15:15:48
|
显示全部楼层
借花献佛,发一下展示于博士人脸检测算法效果的代码:
/*
The MIT License (MIT)
Copyright (c) 2015 Shiqi Yu
shiqi.yu@gmail.com
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include <opencv2/opencv.hpp>
#ifdef _DEBUG
#pragma comment(lib,"opencv_core249d.lib")
#pragma comment(lib,"opencv_highgui249d.lib")
#pragma comment(lib,"opencv_imgproc249d.lib")
#else
#pragma comment(lib,"opencv_core249.lib")
#pragma comment(lib,"opencv_highgui249.lib")
#pragma comment(lib,"opencv_imgproc249.lib")
#endif
#include "../include/facedetect-dll.h"
#pragma comment(lib,"../lib/libfacedetect.lib")
using namespace cv;
int main(int argc, char* argv[])
{
//load an image and convert it to gray (single-channel)
for( int i=0; i<100; i++ )
{
char filename[64]={0};
sprintf_s(filename, 64, "../Probe/test%d.jpg", i+1);
IplImage* iplImg = cvLoadImage(filename, CV_LOAD_IMAGE_ANYCOLOR);
if( !iplImg )
{
continue;
}
//Mat gray = imread("test.png", CV_LOAD_IMAGE_GRAYSCALE);//失败
Mat gray(iplImg), src(iplImg); cvtColor(src, gray, CV_RGB2GRAY);
if(gray.empty())
{
fprintf(stderr, "Can not load the image file.\n");
return -1;
}
int * pResults = NULL;
cvNamedWindow("orig", 1);//
//imshow("orig", gray);//有乱码
///////////////////////////////////////////
// frontal face detection
// it's fast, but cannot detect side view faces
//////////////////////////////////////////
//!!! The input image must be a gray one (single-channel)
//!!! DO NOT RELEASE pResults !!!
pResults = facedetect_frontal((unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, gray.step,
1.2f, 2, 24);
printf("%d frontal faces detected.\n", (pResults ? *pResults : 0));
//print the detection results
for(int i = 0; i < (pResults ? *pResults : 0); i++)
{
short * p = ((short*)(pResults+1))+6*i;
int x = p[0];
int y = p[1];
int w = p[2];
int h = p[3];
int neighbors = p[4];
printf("face_rect=[%d, %d, %d, %d], neighbors=%d\n", x,y,w,h,neighbors);
cvRectangle(iplImg, cvPoint(x,y), cvPoint(x+w, y+h), CV_RGB(255,0,0), 2);
}
cvShowImage("orig", iplImg);//放在绘图函数后,才会显示
cvWaitKey(0);//等待绘图
///////////////////////////////////////////
// multiview face detection
// it can detection side view faces, but slower than the frontal face detection.
//////////////////////////////////////////
//!!! The input image must be a gray one (single-channel)
//!!! DO NOT RELEASE pResults !!!
pResults = facedetect_multiview((unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, gray.step,
1.2f, 4, 24);
printf("%d faces detected.\n", (pResults ? *pResults : 0));
//print the detection results
for(int i = 0; i < (pResults ? *pResults : 0); i++)
{
short * p = ((short*)(pResults+1))+6*i;
int x = p[0];
int y = p[1];
int w = p[2];
int h = p[3];
int neighbors = p[4];
int angle = p[5];
printf("face_rect=[%d, %d, %d, %d], neighbors=%d, angle=%d\n", x,y,w,h,neighbors, angle);
cvRectangle(iplImg, cvPoint(x,y), cvPoint(x+w, y+h), CV_RGB(0,255,0), 2);
}
cvShowImage("orig", iplImg);//放在绘图函数后,才会显示
cvWaitKey(0);//等待绘图
///////////////////////////////////////////
// reinforced multiview face detection
// it can detection side view faces, but slower than the frontal face detection.
//////////////////////////////////////////
//!!! The input image must be a gray one (single-channel)
//!!! DO NOT RELEASE pResults !!!
pResults = facedetect_multiview_reinforce((unsigned char*)(gray.ptr(0)), gray.cols, gray.rows, gray.step,
1.2f, 4, 24);
printf("%d faces detected.\n", (pResults ? *pResults : 0));
//print the detection results
for(int i = 0; i < (pResults ? *pResults : 0); i++)
{
short * p = ((short*)(pResults+1))+6*i;
int x = p[0];
int y = p[1];
int w = p[2];
int h = p[3];
int neighbors = p[4];
int angle = p[5];
printf("face_rect=[%d, %d, %d, %d], neighbors=%d, angle=%d\n", x,y,w,h,neighbors, angle);
cvRectangle(iplImg, cvPoint(x,y), cvPoint(x+w, y+h), CV_RGB(0,0,255), 2);
}
cvShowImage("orig", iplImg);//放在绘图函数后,才会显示
cvWaitKey(0);//等待绘图
cvReleaseImage(&iplImg);
}
return 0;
}
|
|