我是靠谱客的博主 靓丽航空,最近开发中收集的这篇文章主要介绍opencv,C++实现图像中检测人脸,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

参考博客:https://blog.csdn.net/u012679707/article/details/80376969

1.搭建VS2015+opencv3.4.1环境

2.如果opencv3.4.1下搜索不到训练好的人脸检测模型,就是haarcascade_frontalface_alt.xml文件,手动下载xml文件

下载链接:

链接:https://pan.baidu.com/s/1VZwhNO8beb-PYtxITgPOug

提取码:rxv2

3.在项目目录下新建一个xml文件夹,将下载的xml文件放入到文件夹下

 

4.测试图片

 

5.实现代码,注意测试图片的路径

#include <opencv2/opencv.hpp>
#include <opencv.hpp>
#include <iostream>
#include <opencv2face.hpp>

using namespace std;
using namespace cv;

int main(int argc, char *argv[])
{
	Mat img = imread("C:\Users\JSM-SQ\Desktop\face\001.jpg");
	namedWindow("display");
	imshow("display", img);

	/*********************************** 1.加载人脸检测器  ******************************/
	// 建立级联分类器
	CascadeClassifier cascade;
	// 加载训练好的 人脸检测器(.xml)
	//注意路径问题,当前目录的上一个目录中的xml文件夹下
	const string path = "../xml/haarcascade_frontalface_alt2.xml";
	if (!cascade.load(path))
	{
		cout << "cascade load failed!n";
	}

	//计时
	double t = 0;
	t = (double)getTickCount();
	/*********************************** 2.人脸检测 ******************************/
	vector<Rect> faces(0);
	cascade.detectMultiScale(img, faces, 1.1, 2, 0, Size(30, 30));

	cout << "detect face number is :" << faces.size() << endl;
	/********************************  3.显示人脸矩形框 ******************************/

	if (faces.size() > 0)
	{
		for (size_t i = 0; i < faces.size(); i++)
		{
			rectangle(img, faces[i], Scalar(150, 0, 0), 3, 8, 0);

		}
	}
	else cout << "未检测到人脸" << endl;

	t = (double)getTickCount() - t;  //getTickCount():  Returns the number of ticks per second.
	cout << "检测人脸用时:" << t * 1000 / getTickFrequency() << "ms (不计算加载模型的时间)" << endl;

	namedWindow("face_detect");
	imshow("face_detect", img);
	waitKey(0);
	
	destroyWindow("display");
	destroyWindow("face_detect");
	return 0;
}

6.结果

 

最后

以上就是靓丽航空为你收集整理的opencv,C++实现图像中检测人脸的全部内容,希望文章能够帮你解决opencv,C++实现图像中检测人脸所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(43)

评论列表共有 0 条评论

立即
投稿
返回
顶部