本篇文章給大家分享的是有關(guān)使用OpenCV怎么智能視頻監(jiān)控,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。
創(chuàng)新互聯(lián)是一家專業(yè)提供會(huì)寧企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)、HTML5建站、小程序制作等業(yè)務(wù)。10年已為會(huì)寧眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)的建站公司優(yōu)惠進(jìn)行中。
#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ú)法打開(kāi) 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; } //打開(kāi)錄像 int videoplay() { VideoCapture video("錄像.avi"); if(!video.isOpened()) { fprintf(stderr,"打開(kāi)失敗\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] = '\0'; //顯示獲取圖像時(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; }
以上就是使用OpenCV怎么智能視頻監(jiān)控,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
當(dāng)前標(biāo)題:使用OpenCV怎么智能視頻監(jiān)控
本文鏈接:http://aaarwkj.com/article32/ihphsc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供外貿(mào)建站、營(yíng)銷型網(wǎng)站建設(shè)、網(wǎng)站改版、定制網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)