一次性检测出一个文件夹下的所有人脸

本文介绍了一个使用C++和OpenCV实现的人脸检测程序,该程序能够批量处理文件夹内的图像,并检测每张图片中的人脸位置。通过调用facedetect_multiview函数,程序能够返回每个检测到的人脸的位置坐标等信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一次性检测出一个文件夹下的所有人脸——人脸检测。

#include "stdafx.h"
#include <stdio.h>
#include <string>
#include <opencv2/objdetect/objdetect.hpp>
#include "opencv2/imgproc/imgproc.hpp"  
#include "opencv2/highgui/highgui.hpp"  
#include "opencv2/opencv.hpp"  
#include <opencv2/core/core.hpp>
#include<iostream>   
#include<fstream>
#include <opencv.hpp>
//#include<iomanip.h>
#include "facedetect-dll.h"
#pragma comment(lib,"libfacedetect.lib")

using namespace cv;
using namespace std;

int main()
{
	int sum = 0;
	//boundingbox_test.txt
	ofstream f("E:\\boundingbox2.txt");//打开文件用于写,若文件不存在就创建它
	if (!f)//打开文件失败则结束运行
		return -1;
	for (int i = 0; i < 5; i++){//训练集中共有281个检测不到任何脸;测试集中共有189个
		string image_name = "E:\\ESR test\\test\\";//加载训练数据
		image_name = image_name + to_string(i + 1) + ".jpg";//to_string将整型转换为字符串型
		Mat srcImage = imread(image_name, 0);
		if (srcImage.empty())
		{
			fprintf(stderr, "Can not load the image file.\n");
			return -1;
		}

		int * pResults = NULL;
		pResults = facedetect_multiview((unsigned char*)(srcImage.ptr(0)), srcImage.cols, srcImage.rows, srcImage.step,1.2f, 5, 24);
		printf("%d faces detected.\n", (pResults ? *pResults : 0));

		if (*pResults!= 0){
			for (int i = 0; i < (pResults ? *pResults : 0); i++)		//print the detection results
			{
				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];
				f<< x <<" "<< y <<" "<< w <<" "<< h << endl;//使用插入运算符写文件内容
				//f<< setw(20) << "家庭地址:"<< endl;
			
				//printf("face_rect=[%d, %d, %d, %d], neighbors=%d, angle=%d\n", x, y, w, h, neighbors, angle);
			}
		}
		else{
			f << "0   0    0    0" << endl;//使用插入运算符写文件内容
			//cout << i + 1 << endl;
			sum++;
			}
	}
	//cout << "sum=" << sum << endl;
	f.close();//关闭文件
	waitKey(0);
	return 0;
}


	
	
	


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值