概述
之前在做毕设的时候网上找个完整的实现代码挺麻烦的,自己做完分享一下
因为代码较为简单,没有将代码分开写在不同文件,有需要自己整合下哈
使用环境Visual Studio 2010 和 OpenCV 2.4.9
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <ctime>
using namespace std;
using namespace cv;
int videoplay();
void on_Trackbar(int ,void*);
char* str_gettime();
int bSums(Mat src);
char g_str[17];
int g_nNum = 0;//图片名称
int g_nDelay = 0;
int g_npic = 0;
Mat g_filpdstMat;
int g_pointnum = 1000;//设置像素点阈值生成图片
int g_pixel = 0;//像素点
int main()
{
VideoCapture capture(0);
//视频输出VideoWriter
CvVideoWriter* outavi = NULL;
//VideoWriter outavi;
//outavi.open("sre.avi",-1, 5.0, Size(640, 480), true);
outavi = cvCreateVideoWriter("录像.avi", -1, 5.0, cvSize(640, 480), 1);
namedWindow("摄像头",WINDOW_AUTOSIZE);
namedWindow("移动轨迹",WINDOW_AUTOSIZE);
IplImage *pcpframe = NULL;
Mat tempframe, currentframe, preframe, cpframe;
Mat frame,jpg;
int framenum = 0;
//读取一帧处理
while (1)
{
if(!capture.isOpened())
{
cout << "读取失败" << endl;
return -1;
}
capture >> frame;//读取摄像头把每一帧传给frame
frame.copyTo(cpframe);//把frame赋给cpframe,不影响frame
tempframe = frame;//把frame赋给tempframe,影响frame
flip(tempframe,g_filpdstMat,1);//水平翻转图像
pcpframe = &IplImage(cpframe);//为了释放窗口,把Mat转化为IplImage使用
//cpframe=cvarrToMat(pcpframe);
//ipl转化矩阵 pBinary = &IplImage(Img)
//7帧截取一次录入视频,频繁截取运转不过来
if(framenum % 7 == 0)
{
//录像写入
cvWriteFrame(outavi, pcpframe);
}
//判断帧数,若为第一帧,把该帧作为对比帧
//若大于等于第二帧,则进行帧差法处理
framenum++;
if (framenum == 1)
{
cvtColor(g_filpdstMat, preframe, CV_BGR2GRAY);
}
if (framenum >= 2)
{
cvtColor(g_filpdstMat, currentframe, CV_BGR2GRAY);
//灰度图
absdiff(currentframe,preframe,currentframe);//帧差法
threshold(currentframe, currentframe, 30, 255.0, CV_THRESH_BINARY);
//二值化
erode(currentframe, currentframe,Mat());//腐蚀
dilate(currentframe, currentframe,Mat());//膨胀
g_pixel = bSums(currentframe);//调用函数bSums,计算白色像素点,赋值给g_pixel
//小延迟后输出当前像素点数值,防止数据刷太快看不清
g_nDelay++;
if(g_nDelay > 5)
{
cout<< "当前白色像素点:" <<g_pixel << endl;
cout << "按ESC退出" << endl;
g_nDelay = 0;
}
//创建像素点滑轨
createTrackbar("像素点:","移动轨迹",&g_pointnum, 20000,on_Trackbar);
on_Trackbar(0, 0);//调用回调函数
//显示图像
imshow("摄像头", g_filpdstMat);
imshow("移动轨迹", currentframe);
}
//把当前帧保存作为下一次处理的前一帧
cvtColor(g_filpdstMat, preframe, CV_BGR2GRAY);
//判断退出,并销毁录像窗口,否则下一步录像无法打开
if((char)waitKey(10) == 27){cvReleaseVideoWriter(&outavi);break;}
}//end while
while(1)
{
//显示提示窗口
jpg = imread("模式选择.jpg", 1);
imshow("模式选择",jpg);
//设置key选择操作
char key;
key = waitKey(0);
if(key == 'p' || key == 'P')//播放视频
videoplay();
if(key == 'q' || key == 'Q')//退出
break;
}
return 0;
}
//打开录像
int videoplay()
{
VideoCapture video("录像.avi");
if(!video.isOpened())
{
fprintf(stderr,"打开失败n");
return false;
}
while(1)
{
Mat frame;
video>>frame;
if(frame.empty())
{
break;
}
cvNamedWindow("视频", CV_WINDOW_AUTOSIZE);
imshow("视频",frame);
waitKey(30);
}
cvDestroyWindow("视频");
return 0;
}
//滑轨设定阈值判定是否保存当前摄像头图片
void on_Trackbar(int ,void*)
{
//保存来人图片
if(g_pixel > g_pointnum)
{
g_npic++;
if(g_npic > 5)//为了避免风吹草动,小延迟之后才保存图片
{
//保存图片
cout << endl << endl;
cout << "场地异常,警报响应,准备拍照...a" << endl;
imwrite(str_gettime(),g_filpdstMat);
cout << "当前白色像素点:" <<g_pixel << endl;
cout << "按ESC退出" << endl;
cout << endl;
g_npic = 0;
}
}
}
//获取当前日期
char* str_gettime()
{
char tmpbuf[10];
//从tz设置时区环境变量
_tzset();//时间函数
//显示当前日期
_strdate(tmpbuf);
g_str[0] = tmpbuf[6];
g_str[1] = tmpbuf[7];
g_str[2] = tmpbuf[0];
g_str[3] = tmpbuf[1];
g_str[4] = tmpbuf[3];
g_str[5] = tmpbuf[4];
_strtime(tmpbuf);
//时分秒
g_str[6] = tmpbuf[0];
g_str[7] = tmpbuf[1];
g_str[8] = tmpbuf[3];
g_str[9] = tmpbuf[4];
g_str[10] = tmpbuf[6];
g_str[11] = tmpbuf[7];
//规定图片jpg格式
g_str[12] = '.';
g_str[13] = 'j';
g_str[14] = 'p';
g_str[15] = 'g';
g_str[16] = '