OpenCV實現(xiàn)簡單攝像頭視頻監(jiān)控程序
更新時間:2022年01月20日 08:01:04 作者:幸福de小陽
這篇文章主要為大家詳細介紹了OpenCV實現(xiàn)簡單攝像頭視頻監(jiān)控程序,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
如何在冗長的監(jiān)控錄像中找到關鍵點?我們知道,監(jiān)控錄像中大部分信息都是沒用的,那些信息就等同于一幅靜態(tài)圖像。我們要等待監(jiān)控的范圍內出現(xiàn)異常情況時再跟蹤。
這其實是一個最簡單的計算機圖像處理程序。簡單的思路是這樣的:首先給攝像頭取景采樣,當背景穩(wěn)定時,以該圖片作為基準圖片。然后在監(jiān)控過程中,若出現(xiàn)了和基準圖片反差較大的視頻幀,那么啟動捕捉程序,并標出異常區(qū)域。
程序如下:
#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);
// 圖像采樣,確定基準圖像,以便監(jiān)測場景變化
// 返回真表示采樣成功,返回假表示采樣失敗
int gather(CvCapture* capture, IplImage* std, CvRect* rect);
// 攝像機監(jiān)視,用矩形框標示出和基準圖像反差較大的圖像范圍。
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; // 循環(huán)變量
int f = FALSE; // 檢測到異常的標識
int x1 = -1, x2 = 0; // 異常區(qū)域矩形橫坐標范圍
int y1 = -1, y2 = 0; // 異常區(qū)域矩形縱坐標范圍
uchar *ptr1b, *ptr1g, *ptr1r; // 基準圖像的每個像素的三個顏色通道的值
uchar *ptr2b, *ptr2g, *ptr2r; // 實時圖像的每個像素的三個顏色通道的值
int squaresum; // 計算 RGB 差值平方和
// 遍歷圖像中的每一個點,將實時采樣圖與基準圖做比較,檢測兩者的每一個
// 像素點的 RGB 差值平方和。當該值大于 8192 時(換算成灰度值則意味著
// 兩者的灰度差大于 90)則立即報告出現(xiàn)異常,只有遍歷完畢后仍未找到異
// 常才報告沒有異常。
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; // 時間段監(jiān)測
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;
}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C語言實現(xiàn)線性動態(tài)(單向)鏈表的示例代碼
本文主要介紹了C語言實現(xiàn)線性動態(tài)(單向)鏈表的示例代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05
C++基于boost asio實現(xiàn)sync tcp server通信流程詳解
這篇文章主要介紹了C++基于boost asio實現(xiàn)sync tcp server通信的流程,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-07-07

