本篇文章給大家分享的是有關(guān)使用OpenCV怎么智能視頻監(jiā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è)置像素點閾值生成圖片
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
//小延遲后輸出當前像素點數(shù)值,防止數(shù)據(jù)刷太快看不清
g_nDelay++;
if(g_nDelay > 5)
{
cout<< "當前白色像素點:" <<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);
}
//把當前幀保存作為下一次處理的前一幀
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è)定閾值判定是否保存當前攝像頭圖片
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設(shè)置時區(qū)環(huán)境變量
_tzset();//時間函數(shù)
//顯示當前日期
_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;
}以上就是使用OpenCV怎么智能視頻監(jiān)控,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司行業(yè)資訊頻道。
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)建站chinadenli.net,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
網(wǎng)頁名稱:使用OpenCV怎么智能視頻監(jiān)控-創(chuàng)新互聯(lián)
URL地址:http://chinadenli.net/article32/eoppc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、移動網(wǎng)站建設(shè)、響應式網(wǎng)站、網(wǎng)頁設(shè)計公司、網(wǎng)站制作、品牌網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容