现在的位置: 首页 > 综合 > 正文

用 OpenCV 编写一个简单的摄像头视频监控程序

2018年10月24日 ⁄ 综合 ⁄ 共 4616字 ⁄ 字号 评论关闭

Refer from http://blog.csdn.net/smallyang0613/article/details/38331233

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

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

程序如下:

  1. #include <cv.h>  
  2. #include <time.h>  
  3. #include <stdio.h>  
  4. #include <stdlib.h>  
  5. #include <highgui.h>  
  6.   
  7. #define ESC 0x1b  
  8.   
  9. #define TRUE 1  
  10. #define FALSE 0  
  11.   
  12. // 检测图像异常,仅在采样时调用。  
  13. // 返回真表示已检测到异常,需要重新采样。  
  14. // 返回假表示未检测到异常,在一定时间后即可获取基准图像。  
  15. int detect(CvCapture* capture, IplImage* std, IplImage* frm, CvRect* rect);  
  16.   
  17. // 图像采样,确定基准图像,以便监测场景变化  
  18. // 返回真表示采样成功,返回假表示采样失败  
  19. int gather(CvCapture* capture, IplImage* std, CvRect* rect);  
  20.   
  21. // 摄像机监视,用矩形框标示出和基准图像反差较大的图像范围。  
  22. void monitor(CvCapture* capture, IplImage* std, CvRect* rect);  
  23.   
  24. // 求 x 的平方  
  25. int square(int x);  
  26.   
  27. int main(int argc, char* argv[])  
  28. {  
  29.     CvCapture* capture;     // 摄像机源  
  30.     IplImage* std;          // 基准图像  
  31.     CvRect rect;            // 异常位置矩形标识  
  32.   
  33.     capture = cvCreateCameraCapture(0);  
  34.     if (!capture) return -1;  
  35.   
  36.     std = cvQueryFrame(capture);  
  37.     rect = cvRect(-1, -1, 0, 0);  
  38.   
  39.     std = cvCloneImage(std);  
  40.   
  41.     cvNamedWindow("Monitor Screen");  
  42.   
  43.     if (gather(capture, std, &rect))  
  44.     {  
  45.         monitor(capture, std, &rect);  
  46.     }  
  47.   
  48.     cvDestroyWindow("Monitor Screen");  
  49.     cvReleaseImage(&std);  
  50.     cvReleaseCapture(&capture);  
  51.   
  52.     return 0;  
  53. }  
  54.   
  55. int detect(CvCapture* capture, IplImage* std, IplImage* frm, CvRect* rect)  
  56. {  
  57.     int x, y;                       // 循环变量  
  58.     int f = FALSE;                  // 检测到异常的标识  
  59.     int x1 = -1, x2 = 0;            // 异常区域矩形横坐标范围  
  60.     int y1 = -1, y2 = 0;            // 异常区域矩形纵坐标范围  
  61.   
  62.     uchar *ptr1b, *ptr1g, *ptr1r;   // 基准图像的每个像素的三个颜色通道的值  
  63.     uchar *ptr2b, *ptr2g, *ptr2r;   // 实时图像的每个像素的三个颜色通道的值  
  64.   
  65.     int squaresum;                  // 计算 RGB 差值平方和  
  66.   
  67.     // 遍历图像中的每一个点,将实时采样图与基准图做比较,检测两者的每一个  
  68.     // 像素点的 RGB 差值平方和。当该值大于 8192 时(换算成灰度值则意味着  
  69.     // 两者的灰度差大于 90)则立即报告出现异常,只有遍历完毕后仍未找到异  
  70.     // 常才报告没有异常。  
  71.   
  72.     for (y = 0; y < std->height; y++)  
  73.     {  
  74.         for (x = 0; x < std->width; x++)  
  75.         {  
  76.             ptr1b = cvPtr2D(std, y, x) + 0; ptr2b = cvPtr2D(frm, y, x) + 0;  
  77.             ptr1g = cvPtr2D(std, y, x) + 1; ptr2g = cvPtr2D(frm, y, x) + 1;  
  78.             ptr1r = cvPtr2D(std, y, x) + 2; ptr2r = cvPtr2D(frm, y, x) + 2;  
  79.   
  80.             squaresum =  
  81.                 square(*ptr1b - *ptr2b) +  
  82.                 square(*ptr1g - *ptr2g) +  
  83.                 square(*ptr1r - *ptr2r);  
  84.   
  85.             if (squaresum > 8192)  
  86.             {  
  87.                 if (f)  
  88.                 {  
  89.                     if (x < x1) x1 = x; else if (x > x2) x2 = x;  
  90.                     if (y < y1) y1 = y; else if (y > y2) y2 = y;  
  91.                 }  
  92.                 else  
  93.                 {  
  94.                     f = TRUE;  
  95.   
  96.                     x1 = x; y1 = y;  
  97.                     x2 = x; y2 = y;  
  98.                 }  
  99.             }  
  100.         }  
  101.     }  
  102.   
  103.     if (x2 - x1 > frm->width / 4 || y2 - y1 > frm->height / 4)  
  104.     {  
  105.         f = TRUE;  
  106.     }  
  107.     else  
  108.     {  
  109.         f = FALSE;  
  110.     }  
  111.   
  112.     *rect = cvRect(x1, y1, x2 - x1, y2 - y1);  
  113.     return f;  
  114. }  
  115.   
  116. int gather(CvCapture* capture, IplImage* std, CvRect* rect)  
  117. {  
  118.     IplImage* frm;  
  119.     int except = FALSE;             // 检测到异常的标识  
  120.     int finish = FALSE;             // 采样已完成的标识  
  121.     clock_t start_time, real_time;  // 时间段监测  
  122.   
  123.     start_time = clock();  
  124.   
  125.     while (!finish)  
  126.     {  
  127.         frm = cvQueryFrame(capture);  
  128.         cvShowImage("Monitor Screen", frm);  
  129.   
  130.         except = detect(capture, std, frm, rect);  
  131.   
  132.         if (except)  
  133.         {  
  134.             start_time = clock();  
  135.             cvCopyImage(frm, std);  
  136.         }  
  137.   
  138.         if (cvWaitKey(15) == ESC) break;  
  139.   
  140.         real_time = clock();  
  141.         if (real_time - start_time >= 3000)  
  142.         {  
  143.             finish = TRUE;  
  144.         }  
  145.     }  
  146.   
  147.     return finish;  
  148. }  
  149.   
  150. void monitor(CvCapture* capture, IplImage* std, CvRect* rect)  
  151. {  
  152.     IplImage* frm;  
  153.     int except = FALSE;  
  154.     int finish = FALSE;  
  155.   
  156.     while (!finish)  
  157.     {  
  158.         frm = cvQueryFrame(capture);  
  159.   
  160.         except = detect(capture, std, frm, rect);  
  161.   
  162.         if (except)  
  163.         {  
  164.             cvRectangle(  
  165.                 frm,  
  166.                 cvPoint(rect->x, rect->y),  
  167.                 cvPoint(rect->x + rect->width, rect->y + rect->height),  
  168.                 cvScalar(0, 0, 255),  
  169.                 4);  
  170.         }  
  171.         cvShowImage("Monitor Screen", frm);  
  172.   
  173.         if (cvWaitKey(15) == ESC)  
  174.         {  
  175.             finish = TRUE;  
  176.         }  
  177.     }  
  178. }  
  179.   
  180. int square(int x)  
  181. {  
  182.     return x * x;  

抱歉!评论已关闭.