花了一晚上时间,终于解决了检测到的人脸区域的保存和下采样,成果如下:
1.可以批处理一个文件夹下的所有图像;
2.利用opencv进行人脸的检测;
3.对检测到的人脸区域进行保存;
4.对人脸图像32*32的下采样保存。
源代码:
#include "cv.h"
#include "highgui.h"
#include <stdio.h>
using namespace cv;
#include<iostream>
#include<opencv2\opencv.hpp>
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <math.h>
#include <stdio.h>
#include <cv.h>
#include <highgui.h>
#include<io.h>
using std::cout;
using std::endl;
using namespace std;
#include <cmath>
#include<string.h>
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char** argv)
{
CvHaarClassifierCascade * pCascade = 0; // the face detector
CvMemStorage * pStorage = 0; // expandable memory buffer
CvSeq * pFaceRectSeq; // list of detected faces
int ii;
// initializations
FILE *fp;
char str0[200][100];
char str2[200][100];
char str3[200][100];
int i, n = 0;
system("dir /a-d /b E:\\pic\\安以轩\\ >d:\\cam5.txt");
fp = fopen("d:\\cam5.txt", "r");
while (1){
if (fgets(str0[n], 50, fp) == NULL) break;
str0[n][strlen(str0[n]) - 1] = '\0'; // 加一个字符串结束符
n++;
}
n--;
fclose(fp);
double value_psnr01 = 0;
double value_ssim01 = 0;
double value_psnr21 = 0;
double value_ssim21 = 0;
double value_psnr11 = 0;
double value_ssim11 = 0;
for (i = 0; i < 100; i++){
char refColor[200] = "E:\\pic\\安以轩\\";
strcat(refColor, str0[i]);
IplImage * pInpImg = cvLoadImage(refColor, CV_LOAD_IMAGE_COLOR);
pStorage = cvCreateMemStorage(0);
pCascade = (CvHaarClassifierCascade *)cvLoad
("D:\\opencv\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt.xml", 0, 0, 0);
// validate that everything initialized properly
if (!pInpImg || !pStorage || !pCascade){
printf("Initialization failed: %s \n",
(!pInpImg) ? "didn't load image file" :
(!pCascade) ? "didn't load Haar cascade -- "
"make sure path is correct" :
"failed to allocate memory for data storage");
exit(-1);
}
// detect faces in image
pFaceRectSeq = cvHaarDetectObjects
(pInpImg, pCascade, pStorage,
1.1, // increase search scale by 10% each pass
3, // drop groups of fewer than three detections
CV_HAAR_DO_CANNY_PRUNING, // skip regions unlikely to contain a face
cvSize(0, 0)); // use XML default for smallest search scale
// create a window to display detected faces
//cvNamedWindow("Haar Window", CV_WINDOW_AUTOSIZE);
//cv::Mat xMat;
// draw a rectangular outline around each detection
for (ii = 0; ii<(pFaceRectSeq ? pFaceRectSeq->total : 0); ii++){
CvRect * r = (CvRect*)cvGetSeqElem(pFaceRectSeq, ii);
CvPoint pt1 = { r->x, r->y };
CvPoint pt2 = { r->x + r->width, r->y + r->height };
CvSize size = cvSize(r->width, r->height);
cvSetImageROI(pInpImg, cvRect(r->x, r->y, size.width, size.height));
IplImage* pDest = cvCreateImage(size, pInpImg->depth, pInpImg->nChannels);
cvCopy(pInpImg, pDest);
//cvNamedWindow("ShowImage...", 1); //cvNamedWindow会创建一个显示窗口
//cvShowImage("ShowImage...", pDest); //cvShowImage在窗口中显示创建的图像
cvResetImageROI(pDest);
cvSaveImage(refColor, pDest);
cv::Mat pDest1 = imread(refColor);
resize(pDest1, pDest1, Size(32, 32), 0, 0, CV_INTER_LINEAR);
//imshow("wangjian",pDest1);
imwrite(refColor, pDest1);
cvRectangle(pInpImg, pt1, pt2, CV_RGB(0, 255, 0), 3, 4, 0);
}
// display face detections
//cvShowImage("Haar Window", pInpImg);
//cvWaitKey(0);
//cvDestroyWindow("Haar Window");
// clean up and release resources
cvReleaseImage(&pInpImg);
}
if (pCascade) cvReleaseHaarClassifierCascade(&pCascade);
if (pStorage) cvReleaseMemStorage(&pStorage);
return 0;
}