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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章