您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)OpenCV如何實(shí)現(xiàn)智能視頻監(jiān)控的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。
之前在做畢設(shè)的時(shí)候網(wǎng)上找個(gè)完整的實(shí)現(xiàn)代碼挺麻煩的,自己做完分享一下
因?yàn)榇a較為簡(jiǎn)單,沒(méi)有將代碼分開寫在不同文件,有需要自己整合下哈
使用環(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è)置像素點(diǎn)閾值生成圖片int g_pixel = 0;//像素點(diǎn) 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("移動(dòng)軌跡",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幀截取一次錄入視頻,頻繁截取運(yùn)轉(zhuǎn)不過(guò)來(lái) if(framenum % 7 == 0) { //錄像寫入 cvWriteFrame(outavi, pcpframe); } //判斷幀數(shù),若為第一幀,把該幀作為對(duì)比幀 //若大于等于第二幀,則進(jìn)行幀差法處理 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,計(jì)算白色像素點(diǎn),賦值給g_pixel //小延遲后輸出當(dāng)前像素點(diǎn)數(shù)值,防止數(shù)據(jù)刷太快看不清 g_nDelay++; if(g_nDelay > 5) { cout<< "當(dāng)前白色像素點(diǎn):" <<g_pixel << endl; cout << "按ESC退出" << endl; g_nDelay = 0; } //創(chuàng)建像素點(diǎn)滑軌 createTrackbar("像素點(diǎn):","移動(dòng)軌跡",&g_pointnum, 20000,on_Trackbar); on_Trackbar(0, 0);//調(diào)用回調(diào)函數(shù) //顯示圖像 imshow("攝像頭", g_filpdstMat); imshow("移動(dòng)軌跡", currentframe); } //把當(dāng)前幀保存作為下一次處理的前一幀 cvtColor(g_filpdstMat, preframe, CV_BGR2GRAY); //判斷退出,并銷毀錄像窗口,否則下一步錄像無(wú)法打開 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*){ //保存來(lái)人圖片 if(g_pixel > g_pointnum) { g_npic++; if(g_npic > 5)//為了避免風(fēng)吹草動(dòng),小延遲之后才保存圖片 { //保存圖片 cout << endl << endl; cout << "場(chǎng)地異常,警報(bào)響應(yīng),準(zhǔn)備拍照...\a" << endl; imwrite(str_gettime(),g_filpdstMat); cout << "當(dāng)前白色像素點(diǎn):" <<g_pixel << endl; cout << "按ESC退出" << endl; cout << endl; g_npic = 0; } }} //獲取當(dāng)前日期char* str_gettime(){ char tmpbuf[10]; //從tz設(shè)置時(shí)區(qū)環(huán)境變量 _tzset();//時(shí)間函數(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); //時(shí)分秒 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] = ''; //顯示獲取圖像時(shí)間 printf("生成圖片:%s\n", g_str); return g_str; } int bSums(Mat src){ int counter = 0; //迭代器訪問(wèn)像素點(diǎn) Mat_<uchar>::iterator it = src.begin<uchar>(); Mat_<uchar>::iterator itend = src.end<uchar>(); for (; it!=itend; ++it) { if((*it)>0) counter+=1;//二值化后,像素點(diǎn)是0或者255 } return counter;}
感謝各位的閱讀!關(guān)于“OpenCV如何實(shí)現(xiàn)智能視頻監(jiān)控”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。