#include <windows.h>
#include <time.h>
#include <opencv2\opencv.hpp>
#include <cv.h>
#include <highgui.h>
using namespace cv;
#pragma comment(lib, "winmm.lib ")
#pragma comment(lib,"opencv_core244d.lib")
#pragma comment(lib,"opencv_highgui244d.lib")
int diffPics(Mat frame_src1, Mat frame_src2, int i_threshold, Mat& m_dst); //将两图片相减并阈值分割
bool isDiff(Mat m_src1, Mat m_src2, int i_threshold, long l_total); //调用diffPics,判断分割后的图像是否差异很大
Mat g_diffPics;
int main(int argc,char** argv)
{
SYSTEMTIME sys;
// time_t the_time;
namedWindow("capture", 1);
namedWindow("diff", 1);
VideoCapture cap(0);
if(!cap.isOpened()) return -1;
Mat frame1;
Mat frame2;
static char filename[40];
static char filename2[45];
cap >> frame2;
frame1 = frame2.clone();
imshow("capture", frame1);
imshow("diff", frame1);
while( 1 )
{
cap >> frame2;
imshow("capture", frame2);
if(isDiff(frame1, frame2, 20, 5000))
{
// (void) time(&the_time);
// tm tm_ptr = *localtime(&the_time);
GetLocalTime(&sys);
sprintf(filename,"%04d%02d%02d%02d%02d%02d%03d.jpg",
sys.wYear, sys.wMonth, sys.wDay,
sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds
/*tm_ptr.tm_year-100+2000,tm_ptr.tm_mon + 1, tm_ptr.tm_mday,
tm_ptr.tm_hour,tm_ptr.tm_min, tm_ptr.tm_sec8*/);
sprintf(filename2,"diff_%04d%02d%02d%02d%02d%02d%03d.jpg",
sys.wYear, sys.wMonth, sys.wDay,
sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds
/*"diff_%04d%02d%02d%02d%02d%03d.jpg",
tm_ptr.tm_year-100+2000,tm_ptr.tm_mon + 1, tm_ptr.tm_mday,
tm_ptr.tm_hour,tm_ptr.tm_min, tm_ptr.tm_sec*/);
imshow("diff", frame2);
imwrite(filename, frame2);
imwrite(filename2, g_diffPics);
frame1 = frame2.clone();
}
if(waitKey(2)>=0) break;
}
destroyWindow("capture");
destroyWindow("diff");
return 0;
}
int diffPics(Mat frame_src1, Mat frame_src2, int i_threshold, Mat& m_dst)
{
m_dst.create(frame_src1.size(), CV_8UC1);
// Mat m_threshold = Mat::ones(frame_src1.rows,frame_src1.cols,CV_8UC1)*i_threshold;
Mat tem = abs(frame_src1 - frame_src2);
cvtColor(tem, m_dst, CV_RGB2GRAY);
// m_dst = (m_dst - m_threshold);
threshold(m_dst, m_dst, i_threshold, 255, THRESH_TOZERO);
namedWindow("diffPics", 1);
imshow("diffPics", m_dst);
// destroyWindow("diffPics");
return 0;
}
bool isDiff(Mat m_src1, Mat m_src2, int i_thresh, long l_total)
{
bool flag;
long total= 0;
diffPics(m_src1, m_src2, i_thresh, g_diffPics);
for(unsigned int i = 0;i<g_diffPics.rows; i++)
for(unsigned int j = 0;j<g_diffPics.cols; j++)
// for (unsigned int n = 0;n < temp.channels(); n++)
{
if (g_diffPics.at<uchar>(i, j)>0)
total += 1;
}
//sum += temp.at<uchar>(i,j*temp.channels()+n); //取得像素或者赋值
//if (sum > (307200*60))
// flag = true;
//else
// flag = false;
if ( total > l_total)
flag = true;
else
flag = false;
return flag;
}
06-13
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交