OpenCV实现简单摄像头视频监控程序

作者:幸福de小阳 时间:2021-12-10 01:23:07 

如何在冗长的监控录像中找到关键点?我们知道,监控录像中大部分信息都是没用的,那些信息就等同于一幅静态图像。我们要等待监控的范围内出现异常情况时再跟踪。

这其实是一个最简单的计算机图像处理程序。简单的思路是这样的:首先给摄像头取景采样,当背景稳定时,以该图片作为基准图片。然后在监控过程中,若出现了和基准图片反差较大的视频帧,那么启动捕捉程序,并标出异常区域。

程序如下:


#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;
}

来源:https://blog.csdn.net/smallyang0613/article/details/38331233

标签:OpenCV,摄像头,视频监控
0
投稿

猜你喜欢

  • C# Email发送邮件 对方打开邮件可获得提醒

    2022-01-07 17:59:14
  • Redis之GEO存储地理位置信息的使用

    2023-12-22 14:29:49
  • C#中控件动态添加事件绑定的时机详解

    2022-05-05 17:40:59
  • 一篇文章带你了解Java 中序列化与反序列化

    2021-11-27 21:19:27
  • Android实现信息弹出框

    2023-04-20 06:27:40
  • java的Jackson将json字符串转换成泛型List

    2021-10-06 20:17:49
  • Kotlin基础教程之Run,标签Label,函数Function-Type

    2022-08-28 14:11:01
  • Springboot+Mybatis-plus不使用SQL语句进行多表添加操作及问题小结

    2021-09-30 10:31:10
  • 基于C#模拟实现回合制游戏

    2021-08-01 23:09:55
  • 关于Java反编译字节码文件

    2021-10-07 01:41:32
  • C#中正则表达式(Regex)过滤内容的基本使用方法

    2023-11-26 12:51:00
  • C#读写Config配置文件案例

    2022-10-22 20:11:09
  • Java接口默认方法带来的问题分析【二义性问题】

    2023-11-27 20:32:55
  • MyBatis中基于别名typeAliases的设置

    2022-03-07 22:18:19
  • java使用IO流对数组排序实例讲解

    2023-09-04 02:24:19
  • 用AdapterViewFlipper轻松完成图片轮播

    2021-12-08 12:50:47
  • Java如何跳过https的ssl证书验证详解

    2023-08-24 11:34:56
  • Springboot使用POI实现导出Excel文件示例

    2021-09-22 08:18:31
  • 详细解读JAVA多线程实现的三种方式

    2022-01-14 04:35:31
  • Android 通过TCP协议上传指定目录文件的方法

    2023-11-07 23:34:11
  • asp之家 软件编程 m.aspxhome.com