OpenCV實現(xiàn)智能視頻監(jiān)控
本文實例為大家分享了OpenCV實現(xiàn)智能視頻監(jiān)控的具體代碼,供大家參考,具體內(nèi)容如下
之前在做畢設(shè)的時候網(wǎng)上找個完整的實現(xiàn)代碼挺麻煩的,自己做完分享一下
因為代碼較為簡單,沒有將代碼分開寫在不同文件,有需要自己整合下哈
使用環(huán)境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;//設(shè)置像素點閾值生成圖片
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);//水平翻轉(zhuǎn)圖像
pcpframe = &IplImage(cpframe);//為了釋放窗口,把Mat轉(zhuǎn)化為IplImage使用
//cpframe=cvarrToMat(pcpframe);
//ipl轉(zhuǎn)化矩陣 pBinary = &IplImage(Img)
//7幀截取一次錄入視頻,頻繁截取運轉(zhuǎn)不過來
if(framenum % 7 == 0)
{
//錄像寫入
cvWriteFrame(outavi, pcpframe);
}
//判斷幀數(shù),若為第一幀,把該幀作為對比幀
//若大于等于第二幀,則進行幀差法處理
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);//調(diào)用函數(shù)bSums,計算白色像素點,賦值給g_pixel
//小延遲后輸出當(dāng)前像素點數(shù)值,防止數(shù)據(jù)刷太快看不清
g_nDelay++;
if(g_nDelay > 5)
{
cout<< "當(dāng)前白色像素點:" <<g_pixel << endl;
cout << "按ESC退出" << endl;
g_nDelay = 0;
}
//創(chuàng)建像素點滑軌
createTrackbar("像素點:","移動軌跡",&g_pointnum, 20000,on_Trackbar);
on_Trackbar(0, 0);//調(diào)用回調(diào)函數(shù)
//顯示圖像
imshow("攝像頭", g_filpdstMat);
imshow("移動軌跡", currentframe);
}
//把當(dāng)前幀保存作為下一次處理的前一幀
cvtColor(g_filpdstMat, preframe, CV_BGR2GRAY);
//判斷退出,并銷毀錄像窗口,否則下一步錄像無法打開
if((char)waitKey(10) == 27){cvReleaseVideoWriter(&outavi);break;}
}//end while
while(1)
{
//顯示提示窗口
jpg = imread("模式選擇.jpg", 1);
imshow("模式選擇",jpg);
//設(shè)置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;
}
//滑軌設(shè)定閾值判定是否保存當(dāng)前攝像頭圖片
void on_Trackbar(int ,void*)
{
//保存來人圖片
if(g_pixel > g_pointnum)
{
g_npic++;
if(g_npic > 5)//為了避免風(fēng)吹草動,小延遲之后才保存圖片
{
//保存圖片
cout << endl << endl;
cout << "場地異常,警報響應(yīng),準(zhǔn)備拍照...\a" << endl;
imwrite(str_gettime(),g_filpdstMat);
cout << "當(dāng)前白色像素點:" <<g_pixel << endl;
cout << "按ESC退出" << endl;
cout << endl;
g_npic = 0;
}
}
}
//獲取當(dāng)前日期
char* str_gettime()
{
char tmpbuf[10];
//從tz設(shè)置時區(qū)環(huán)境變量
_tzset();//時間函數(shù)
//顯示當(dāng)前日期
_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];
//規(guī)定圖片jpg格式
g_str[12] = '.';
g_str[13] = 'j';
g_str[14] = 'p';
g_str[15] = 'g';
g_str[16] = '\0';
//顯示獲取圖像時間
printf("生成圖片:%s\n", g_str);
return g_str;
}
int bSums(Mat src)
{
int counter = 0;
//迭代器訪問像素點
Mat_<uchar>::iterator it = src.begin<uchar>();
Mat_<uchar>::iterator itend = src.end<uchar>();
for (; it!=itend; ++it)
{
if((*it)>0) counter+=1;//二值化后,像素點是0或者255
}
return counter;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C語言實現(xiàn) 數(shù)據(jù)類型占多少字節(jié)指針占多少字節(jié)
欄 目:C語言
下一篇:C語言中access/_access函數(shù)的使用實例詳解
本文標(biāo)題:OpenCV實現(xiàn)智能視頻監(jiān)控
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/207.html
您可能感興趣的文章
- 01-10數(shù)據(jù)結(jié)構(gòu)課程設(shè)計-用棧實現(xiàn)表達式求值的方法詳解
- 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
- 01-10求斐波那契(Fibonacci)數(shù)列通項的七種實現(xiàn)方法
- 01-10C語言 解決不用+、-、&#215;、&#247;數(shù)字運算符做加法
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10用C++實現(xiàn)DBSCAN聚類算法
- 01-10深入全排列算法及其實現(xiàn)方法
- 01-10全排列算法的非遞歸實現(xiàn)與遞歸實現(xiàn)的方法(C++)
- 01-10用C語言實現(xiàn)單鏈表的各種操作(一)
- 01-10用C語言實現(xiàn)單鏈表的各種操作(二)


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 04-02jquery與jsp,用jquery
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10C#中split用法實例總結(jié)
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文


