#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#define CV_VERSION_ID CVAUX_STR(CV_MAJOR_VERSION) CVAUX_STR(CV_MINOR_VERSION) CVAUX_STR(CV_SUBMINOR_VERSION)
#ifdef _DEBUG
#define cvLIB(name) "opencv_" name CV_VERSION_ID "d"
#else
#define cvLIB(name) "opencv_" name CV_VERSION_ID
#endif
//#pragma comment( lib, cvLIB("core") )
//#pragma comment( lib, cvLIB("imgproc") )
//#pragma comment( lib, cvLIB("highgui") )
//#pragma comment( lib, cvLIB("flann") )
//#pragma comment( lib, cvLIB("features2d") )
//#pragma comment( lib, cvLIB("calib3d") )
//#pragma comment( lib, cvLIB("gpu") )
//#pragma comment( lib, cvLIB("legacy") )
//#pragma comment( lib, cvLIB("ml") )
//#pragma comment( lib, cvLIB("objdetect") )
//#pragma comment( lib, cvLIB("ts") )
//#pragma comment( lib, cvLIB("video") )
//#pragma comment( lib, cvLIB("contrib") )
//#pragma comment( lib, cvLIB("nonfree") )
using namespace cv;
using namespace std;
//以上为网上找的opencv万能文件头
int main()
{
CascadeClassifier faceCascade;
faceCascade.load("C:\\opencv\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt2.xml"); //加载分类器,注意文件路径 (自己找找你的opencv解压路径下的文件)
vector<Rect> faces;//容器
VideoCapture cap(0); // 打开摄像头0
if (!cap.isOpened()) // 异常处理
{
cout << "Cannot open the video cam" << endl;
waitKey(60000);
return -1;
}
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //输入流的宽
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //输入流的高
cout << "Frame size : " << dWidth << " x " << dHeight << endl;
namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //创建一个窗体
/*string text = "LZ";
Point pt(100, 100);
Scalar color = CV_RGB(0, 255, 255);*/
while (1)
{
Mat frame;
Mat imgGray;
bool bSuccess = cap.read(frame); // 获取一帧
if (!bSuccess) //异常处理
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
if (frame.channels() == 3)
{
cvtColor(frame, imgGray, CV_RGB2GRAY);//转为灰度图
}
else
{
imgGray = frame;
}
faceCascade.detectMultiScale(imgGray, faces, 1.2, 6, 0, Size(0, 0)); //检测人脸
if (faces.size() > 0)
{
for (int i = 0; i < faces.size(); i++)
{
rectangle(frame, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height),
Scalar(0, 255, 255), 1, 8); //框出人脸位置 faces[i].x,faces[i].y, faces[i].x + faces[i].width, faces[i].y + faces[i].height
}
}
///*RNG rng(12345);
//Scalar value(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
//copyMakeBorder(frame, frame, 100, 200, 100, 200, BORDER_CONSTANT, value);*/
//rectangle(frame, Rect(10, 10, 200, 200), Scalar(0, 0, 255), 1, 1, 0);
//putText(frame, text, pt, CV_FONT_HERSHEY_DUPLEX, 1.0f, color);
imshow("MyVideo", frame); //显示获取到的视频帧
if (waitKey(30) == 27) //按Esc键退出
{
cout << "esc key is pressed by user" << endl;
break;
}
}
waitKey();
return 0;
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <opencv2/opencv.hpp>
#define CV_VERSION_ID CVAUX_STR(CV_MAJOR_VERSION) CVAUX_STR(CV_MINOR_VERSION) CVAUX_STR(CV_SUBMINOR_VERSION)
#ifdef _DEBUG
#define cvLIB(name) "opencv_" name CV_VERSION_ID "d"
#else
#define cvLIB(name) "opencv_" name CV_VERSION_ID
#endif
//#pragma comment( lib, cvLIB("core") )
//#pragma comment( lib, cvLIB("imgproc") )
//#pragma comment( lib, cvLIB("highgui") )
//#pragma comment( lib, cvLIB("flann") )
//#pragma comment( lib, cvLIB("features2d") )
//#pragma comment( lib, cvLIB("calib3d") )
//#pragma comment( lib, cvLIB("gpu") )
//#pragma comment( lib, cvLIB("legacy") )
//#pragma comment( lib, cvLIB("ml") )
//#pragma comment( lib, cvLIB("objdetect") )
//#pragma comment( lib, cvLIB("ts") )
//#pragma comment( lib, cvLIB("video") )
//#pragma comment( lib, cvLIB("contrib") )
//#pragma comment( lib, cvLIB("nonfree") )
using namespace cv;
using namespace std;
//以上为网上找的opencv万能文件头
int main()
{
CascadeClassifier faceCascade;
faceCascade.load("C:\\opencv\\opencv\\sources\\data\\haarcascades\\haarcascade_frontalface_alt2.xml"); //加载分类器,注意文件路径 (自己找找你的opencv解压路径下的文件)
vector<Rect> faces;//容器
VideoCapture cap(0); // 打开摄像头0
if (!cap.isOpened()) // 异常处理
{
cout << "Cannot open the video cam" << endl;
waitKey(60000);
return -1;
}
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //输入流的宽
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //输入流的高
cout << "Frame size : " << dWidth << " x " << dHeight << endl;
namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //创建一个窗体
/*string text = "LZ";
Point pt(100, 100);
Scalar color = CV_RGB(0, 255, 255);*/
while (1)
{
Mat frame;
Mat imgGray;
bool bSuccess = cap.read(frame); // 获取一帧
if (!bSuccess) //异常处理
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
if (frame.channels() == 3)
{
cvtColor(frame, imgGray, CV_RGB2GRAY);//转为灰度图
}
else
{
imgGray = frame;
}
faceCascade.detectMultiScale(imgGray, faces, 1.2, 6, 0, Size(0, 0)); //检测人脸
if (faces.size() > 0)
{
for (int i = 0; i < faces.size(); i++)
{
rectangle(frame, Point(faces[i].x, faces[i].y), Point(faces[i].x + faces[i].width, faces[i].y + faces[i].height),
Scalar(0, 255, 255), 1, 8); //框出人脸位置 faces[i].x,faces[i].y, faces[i].x + faces[i].width, faces[i].y + faces[i].height
}
}
///*RNG rng(12345);
//Scalar value(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
//copyMakeBorder(frame, frame, 100, 200, 100, 200, BORDER_CONSTANT, value);*/
//rectangle(frame, Rect(10, 10, 200, 200), Scalar(0, 0, 255), 1, 1, 0);
//putText(frame, text, pt, CV_FONT_HERSHEY_DUPLEX, 1.0f, color);
imshow("MyVideo", frame); //显示获取到的视频帧
if (waitKey(30) == 27) //按Esc键退出
{
cout << "esc key is pressed by user" << endl;
break;
}
}
waitKey();
return 0;
}
//注意安装环境及opencv的配置问题,
//debug下提示像是内存问题,请直接r版运行。