level 1
半越三霸蜜2y
楼主
下面是我的代码 弄了好长时间解决不了 希望大家可以帮一下忙 谢谢啦!
-----------------------------------------------------------
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
/** 函数声明 */
void detectAndDisplay(Mat frame);
/** 全局变量 */
string face_cascade_name = "haarcascade_frontalface_alt.xml";
string eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
string window_name = "Capture - Face detection";
RNG rng(12345);
/** @主函数 */
int main(int argc, const char** argv)
{
//-- 1. 加载级联分类器文件
if (!face_cascade.load(face_cascade_name)) { printf("--(!)Error loading\n"); return -1; };
if (!eyes_cascade.load(eyes_cascade_name)) { printf("--(!)Error loading\n"); return -1; };
//-- 2. 打开内置摄像头视频流
VideoCapture capture(0);
if (!capture.isOpened())
return -2;
Mat frame;
while (1)
{
capture >> frame;//帧捕获,一帧一帧分解检测。
//-- 3. 对当前帧使用分类器进行检测
if (!frame.empty())
{
detectAndDisplay(frame);//调用这个方法进行识别。
}
else
{
printf(" --(!) No captured frame -- Break!"); break;//没有捕获到帧,跳出!
}
int c = waitKey(30);
if ((char)c == 'c') { break; }
}
return 0;
}
/** @函数 detectAndDisplay */
void detectAndDisplay(Mat frame)
{
std::vector<Rect> faces;
Mat frame_gray;
cvtColor(frame, frame_gray, CV_BGR2GRAY);//得到灰度图像
equalizeHist(frame_gray, frame_gray);//直方图均衡化
//-- 多尺寸检测人脸
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
for (int i = 0; i < faces.size(); i++)
{
Point center(int(faces[i].x + faces[i].width*0.5), int(faces[i].y + faces[i].height*0.5));
ellipse(frame, center, Size(int(faces[i].width*0.5), int(faces[i].height*0.5)), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
Mat faceROI = frame_gray(faces[i]);
std::vector<Rect> eyes;
//-- 在每张人脸上检测双眼
eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
for (int j = 0; j < eyes.size(); j++)
{
Point center(int(faces[i].x + eyes[j].x + eyes[j].width*0.5), int(faces[i].y + eyes[j].y + eyes[j].height*0.5));
int radius = cvRound((eyes[j].width + eyes[i].height)*0.25);
circle(frame, center, radius, Scalar(255, 0, 0), 4, 8, 0);
}
}
//-- 显示结果图像
imshow(window_name, frame);
}
2017年07月14日 07点07分
1
-----------------------------------------------------------
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
using namespace std;
using namespace cv;
/** 函数声明 */
void detectAndDisplay(Mat frame);
/** 全局变量 */
string face_cascade_name = "haarcascade_frontalface_alt.xml";
string eyes_cascade_name = "haarcascade_eye_tree_eyeglasses.xml";
CascadeClassifier face_cascade;
CascadeClassifier eyes_cascade;
string window_name = "Capture - Face detection";
RNG rng(12345);
/** @主函数 */
int main(int argc, const char** argv)
{
//-- 1. 加载级联分类器文件
if (!face_cascade.load(face_cascade_name)) { printf("--(!)Error loading\n"); return -1; };
if (!eyes_cascade.load(eyes_cascade_name)) { printf("--(!)Error loading\n"); return -1; };
//-- 2. 打开内置摄像头视频流
VideoCapture capture(0);
if (!capture.isOpened())
return -2;
Mat frame;
while (1)
{
capture >> frame;//帧捕获,一帧一帧分解检测。
//-- 3. 对当前帧使用分类器进行检测
if (!frame.empty())
{
detectAndDisplay(frame);//调用这个方法进行识别。
}
else
{
printf(" --(!) No captured frame -- Break!"); break;//没有捕获到帧,跳出!
}
int c = waitKey(30);
if ((char)c == 'c') { break; }
}
return 0;
}
/** @函数 detectAndDisplay */
void detectAndDisplay(Mat frame)
{
std::vector<Rect> faces;
Mat frame_gray;
cvtColor(frame, frame_gray, CV_BGR2GRAY);//得到灰度图像
equalizeHist(frame_gray, frame_gray);//直方图均衡化
//-- 多尺寸检测人脸
face_cascade.detectMultiScale(frame_gray, faces, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
for (int i = 0; i < faces.size(); i++)
{
Point center(int(faces[i].x + faces[i].width*0.5), int(faces[i].y + faces[i].height*0.5));
ellipse(frame, center, Size(int(faces[i].width*0.5), int(faces[i].height*0.5)), 0, 0, 360, Scalar(255, 0, 255), 4, 8, 0);
Mat faceROI = frame_gray(faces[i]);
std::vector<Rect> eyes;
//-- 在每张人脸上检测双眼
eyes_cascade.detectMultiScale(faceROI, eyes, 1.1, 2, 0 | CV_HAAR_SCALE_IMAGE, Size(30, 30));
for (int j = 0; j < eyes.size(); j++)
{
Point center(int(faces[i].x + eyes[j].x + eyes[j].width*0.5), int(faces[i].y + eyes[j].y + eyes[j].height*0.5));
int radius = cvRound((eyes[j].width + eyes[i].height)*0.25);
circle(frame, center, radius, Scalar(255, 0, 0), 4, 8, 0);
}
}
//-- 显示结果图像
imshow(window_name, frame);
}