基于c++的OpenCV⽬标检测
声明:本⽂的源码全部来OpenCV的官⽅⽂档,笔者只是做⼀个学习笔记
以下代码主要实现通过摄像头完成⼈脸、眼睛、⾝体检测
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
void detectAndDisplay(Mat frame);
CascadeClassifier face_cascade; //使⽤级联分类器类加载视频中对象 //face
CascadeClassifier eyes_cascade; //eyes
CascadeClassifier upperbody_cascade; //body
int main(int argc, const char** argv)
{
CommandLineParser parser(argc, argv,
"{help h||}"
"{face_cascade|D:/opencv/sources/data/haarcascades/haarcascade_l|Path to face cascade.}"
"{eyes_cascade|D:/opencv/sources/data/haarcascades/haarcascade_eye_l|Path to eyes cascade.}"
"{upperbody_cascade|D:/opencv/sources/data/haarcascades/l|Path to upperbody cascade.}"
"{camera|0|Camera device number.}");
//路径⾃⼰改,在你安装OpenCV⽬录寻
parser.about("\nThis program demonstrates using the cv::CascadeClassifier class to detect objects (Face + eyes) in a video stream.\n" "You can use Haar or LBP features.\n\n");
parser.printMessage();
String face_cascade_name = <String>("face_cascade");
String eyes_cascade_name = <String>("eyes_cascade");
String upperbody_cascade_name = <String>("upperbody_cascade");
//-- 1. Load the cascades
if (!face_cascade.load(face_cascade_name))
{
cout << "--(!)Error loading face cascade\n";
return -1;
};
if (!eyes_cascade.load(eyes_cascade_name))
{
cout << "--(!)Error loading eyes cascade\n";
return -1;
};
if (!upperbody_cascade.load(upperbody_cascade_name))
{
cout << "--(!)Error loading upperbody cascade\n";
return -1;
};
int camera_device = <int>("camera");
VideoCapture capture;
//-- 2. Read the video stream
capture.open(camera_device);
if (!capture.isOpened())
{
cout << "--(!)Error opening video capture\n";
return -1;
}
Mat frame;
while (ad(frame))
{
if (pty())
{
cout << "--(!) No captured frame -- Break!\n";
break;
}
//-- 3. Apply the classifier to the frame
detectAndDisplay(frame);
if (waitKey(10) == 27)
{
break; // escape
}
}
return 0;
}
void detectAndDisplay(Mat frame)
{
Mat frame_gray;
cvtColor(frame, frame_gray, COLOR_BGR2GRAY); //转灰度图
equalizeHist(frame_gray, frame_gray); //直⽅图等化:增强图像的对⽐度
//-- Detect faces
std::vector<Rect> faces;
face_cascade.detectMultiScale(frame_gray, faces); //在图像中检测不同⼤⼩的对象,并将检测到的对象以矩形列表返回,这⾥检测的是⼈脸 //这个返回值是放在faces⾥⾯的,detectMultiScale这个函数是void
for (size_t i = 0; i < faces.size(); i++)
{
Point center(faces[i].x + faces[i].width / 2,\
faces[i].y + faces[i].height / 2); //计算到⾯部的中⼼点
ellipse(frame, center, Size(faces[i].width / 2,\
faces[i].height / 2), 0, 0, 360, Scalar(255, 0, 255), 4); //绘制圆
Mat faceROI = frame_gray(faces[i]); //先确定脸,再去眼睛
//-- In each face, detect eyes
std::vector<Rect> eyes;
eyes_cascade.detectMultiScale(faceROI, eyes);
for (size_t j = 0; j < eyes.size(); j++)
{
Point eye_center(faces[i].x + eyes[j].x + eyes[j].width / 2,\
faces[i].y + eyes[j].y + eyes[j].height / 2);
int radius = cvRound((eyes[j].width + eyes[j].height)*0.25);
circle(frame, eye_center, radius, Scalar(255, 0, 0), 4);
}
}
std::vector<Rect> upperbody;
upperbody_cascade.detectMultiScale(frame_gray, upperbody);
for (size_t i = 0; i < upperbody.size(); i++)
{
Point top_left(upperbody[i].x, upperbody[i].y);
Point low_right(upperbody[i].x + upperbody[i].width, \
upperbody[i].y + upperbody[i].height);
rectangle(frame, top_left, low_right, Scalar(0, 255, 0), 4);
}rectangle函数opencv
//-- Show what you got
imshow("Capture - Face detection", frame); }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论