欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

OpenCV實現(xiàn)智能視頻監(jiān)控

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

本文實例為大家分享了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://mengdiqiu.com.cn/a1/Cyuyan/207.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有