使用OpenCV怎么实现一个摄像头视频监控程序

使用OpenCV怎么实现一个摄像头视频监控程序?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

程序如下:

#include <cv.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <highgui.h>
 
#define ESC 0x1b
 
#define TRUE 1
#define FALSE 0
 
// 检测图像异常,仅在采样时调用。
// 返回真表示已检测到异常,需要重新采样。
// 返回假表示未检测到异常,在一定时间后即可获取基准图像。
int detect(CvCapture* capture, IplImage* std, IplImage* frm, CvRect* rect);
 
// 图像采样,确定基准图像,以便监测场景变化
// 返回真表示采样成功,返回假表示采样失败
int gather(CvCapture* capture, IplImage* std, CvRect* rect);
 
// 摄像机监视,用矩形框标示出和基准图像反差较大的图像范围。
void monitor(CvCapture* capture, IplImage* std, CvRect* rect);
 
// 求 x 的平方
int square(int x);
 
int main(int argc, char* argv[])
{
 CvCapture* capture;   // 摄像机源
 IplImage* std;     // 基准图像
 CvRect rect;      // 异常位置矩形标识
 
 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;            // 循环变量
 int f = FALSE;         // 检测到异常的标识
 int x1 = -1, x2 = 0;      // 异常区域矩形横坐标范围
 int y1 = -1, y2 = 0;      // 异常区域矩形纵坐标范围
 
 uchar *ptr1b, *ptr1g, *ptr1r;  // 基准图像的每个像素的三个颜色通道的值
 uchar *ptr2b, *ptr2g, *ptr2r;  // 实时图像的每个像素的三个颜色通道的值
 
 int squaresum;         // 计算 RGB 差值平方和
 
 // 遍历图像中的每一个点,将实时采样图与基准图做比较,检测两者的每一个
 // 像素点的 RGB 差值平方和。当该值大于 8192 时(换算成灰度值则意味着
 // 两者的灰度差大于 90)则立即报告出现异常,只有遍历完毕后仍未找到异
 // 常才报告没有异常。
 
 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;       // 检测到异常的标识
 int finish = FALSE;       // 采样已完成的标识
 clock_t start_time, real_time; // 时间段监测
 
 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;
}

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对亿速云的支持。