溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用OpenCV怎么實(shí)現(xiàn)一個(gè)攝像頭視頻監(jiān)控程序

發(fā)布時(shí)間:2021-04-19 17:40:42 來(lái)源:億速云 閱讀:261 作者:Leah 欄目:編程語(yǔ)言

使用OpenCV怎么實(shí)現(xiàn)一個(gè)攝像頭視頻監(jiān)控程序?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。

程序如下:

#include <cv.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <highgui.h>
 
#define ESC 0x1b
 
#define TRUE 1
#define FALSE 0
 
// 檢測(cè)圖像異常,僅在采樣時(shí)調(diào)用。
// 返回真表示已檢測(cè)到異常,需要重新采樣。
// 返回假表示未檢測(cè)到異常,在一定時(shí)間后即可獲取基準(zhǔn)圖像。
int detect(CvCapture* capture, IplImage* std, IplImage* frm, CvRect* rect);
 
// 圖像采樣,確定基準(zhǔn)圖像,以便監(jiān)測(cè)場(chǎng)景變化
// 返回真表示采樣成功,返回假表示采樣失敗
int gather(CvCapture* capture, IplImage* std, CvRect* rect);
 
// 攝像機(jī)監(jiān)視,用矩形框標(biāo)示出和基準(zhǔn)圖像反差較大的圖像范圍。
void monitor(CvCapture* capture, IplImage* std, CvRect* rect);
 
// 求 x 的平方
int square(int x);
 
int main(int argc, char* argv[])
{
 CvCapture* capture;   // 攝像機(jī)源
 IplImage* std;     // 基準(zhǔn)圖像
 CvRect rect;      // 異常位置矩形標(biāo)識(shí)
 
 capture = cvCreateCameraCapture(0);
 if (!capture) return -1;
 
 std = cvQueryFrame(capture);
 rect = cvRect(-1, -1, 0, 0);
 
 std = cvCloneImage(std);
 
 cvNamedWindow("Monitor Screen");
 
 if (gather(capture, std, &rect))
 {
 monitor(capture, std, &rect);
 }
 
 cvDestroyWindow("Monitor Screen");
 cvReleaseImage(&std);
 cvReleaseCapture(&capture);
 
 return 0;
}
 
int detect(CvCapture* capture, IplImage* std, IplImage* frm, CvRect* rect)
{
 int x, y;            // 循環(huán)變量
 int f = FALSE;         // 檢測(cè)到異常的標(biāo)識(shí)
 int x1 = -1, x2 = 0;      // 異常區(qū)域矩形橫坐標(biāo)范圍
 int y1 = -1, y2 = 0;      // 異常區(qū)域矩形縱坐標(biāo)范圍
 
 uchar *ptr1b, *ptr1g, *ptr1r;  // 基準(zhǔn)圖像的每個(gè)像素的三個(gè)顏色通道的值
 uchar *ptr2b, *ptr2g, *ptr2r;  // 實(shí)時(shí)圖像的每個(gè)像素的三個(gè)顏色通道的值
 
 int squaresum;         // 計(jì)算 RGB 差值平方和
 
 // 遍歷圖像中的每一個(gè)點(diǎn),將實(shí)時(shí)采樣圖與基準(zhǔn)圖做比較,檢測(cè)兩者的每一個(gè)
 // 像素點(diǎn)的 RGB 差值平方和。當(dāng)該值大于 8192 時(shí)(換算成灰度值則意味著
 // 兩者的灰度差大于 90)則立即報(bào)告出現(xiàn)異常,只有遍歷完畢后仍未找到異
 // 常才報(bào)告沒(méi)有異常。
 
 for (y = 0; y < std->height; y++)
 {
 for (x = 0; x < std->width; x++)
 {
  ptr1b = cvPtr2D(std, y, x) + 0; ptr2b = cvPtr2D(frm, y, x) + 0;
  ptr1g = cvPtr2D(std, y, x) + 1; ptr2g = cvPtr2D(frm, y, x) + 1;
  ptr1r = cvPtr2D(std, y, x) + 2; ptr2r = cvPtr2D(frm, y, x) + 2;
 
  squaresum =
  square(*ptr1b - *ptr2b) +
  square(*ptr1g - *ptr2g) +
  square(*ptr1r - *ptr2r);
 
  if (squaresum > 8192)
  {
  if (f)
  {
   if (x < x1) x1 = x; else if (x > x2) x2 = x;
   if (y < y1) y1 = y; else if (y > y2) y2 = y;
  }
  else
  {
   f = TRUE;
 
   x1 = x; y1 = y;
   x2 = x; y2 = y;
  }
  }
 }
 }
 
 if (x2 - x1 > frm->width / 4 || y2 - y1 > frm->height / 4)
 {
 f = TRUE;
 }
 else
 {
 f = FALSE;
 }
 
 *rect = cvRect(x1, y1, x2 - x1, y2 - y1);
 return f;
}
 
int gather(CvCapture* capture, IplImage* std, CvRect* rect)
{
 IplImage* frm;
 int except = FALSE;       // 檢測(cè)到異常的標(biāo)識(shí)
 int finish = FALSE;       // 采樣已完成的標(biāo)識(shí)
 clock_t start_time, real_time; // 時(shí)間段監(jiān)測(cè)
 
 start_time = clock();
 
 while (!finish)
 {
 frm = cvQueryFrame(capture);
 cvShowImage("Monitor Screen", frm);
 
 except = detect(capture, std, frm, rect);
 
 if (except)
 {
  start_time = clock();
  cvCopyImage(frm, std);
 }
 
 if (cvWaitKey(15) == ESC) break;
 
 real_time = clock();
 if (real_time - start_time >= 3000)
 {
  finish = TRUE;
 }
 }
 
 return finish;
}
 
void monitor(CvCapture* capture, IplImage* std, CvRect* rect)
{
 IplImage* frm;
 int except = FALSE;
 int finish = FALSE;
 
 while (!finish)
 {
 frm = cvQueryFrame(capture);
 
 except = detect(capture, std, frm, rect);
 
 if (except)
 {
  cvRectangle(
  frm,
  cvPoint(rect->x, rect->y),
  cvPoint(rect->x + rect->width, rect->y + rect->height),
  cvScalar(0, 0, 255),
  4);
 }
 cvShowImage("Monitor Screen", frm);
 
 if (cvWaitKey(15) == ESC)
 {
  finish = TRUE;
 }
 }
}
 
int square(int x)
{
 return x * x;
}

看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。

向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI