OpenCV實(shí)現(xiàn)簡(jiǎn)單攝像頭視頻監(jiān)控程序
如何在冗長的監(jiān)控錄像中找到關(guān)鍵點(diǎn)?我們知道,監(jiān)控錄像中大部分信息都是沒用的,那些信息就等同于一幅靜態(tài)圖像。我們要等待監(jiān)控的范圍內(nèi)出現(xiàn)異常情況時(shí)再跟蹤。
這其實(shí)是一個(gè)最簡(jiǎn)單的計(jì)算機(jī)圖像處理程序。簡(jiǎn)單的思路是這樣的:首先給攝像頭取景采樣,當(dāng)背景穩(wěn)定時(shí),以該圖片作為基準(zhǔn)圖片。然后在監(jiān)控過程中,若出現(xiàn)了和基準(zhǔn)圖片反差較大的視頻幀,那么啟動(dòng)捕捉程序,并標(biāo)出異常區(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 // 檢測(cè)圖像異常,僅在采樣時(shí)調(diào)用。 // 返回真表示已檢測(cè)到異常,需要重新采樣。 // 返回假表示未檢測(cè)到異常,在一定時(shí)間后即可獲取基準(zhǔn)圖像。 int detect(CvCapture* capture, IplImage* std, IplImage* frm, CvRect* rect); // 圖像采樣,確定基準(zhǔn)圖像,以便監(jiān)測(cè)場(chǎng)景變化 // 返回真表示采樣成功,返回假表示采樣失敗 int gather(CvCapture* capture, IplImage* std, CvRect* rect); // 攝像機(jī)監(jiān)視,用矩形框標(biāo)示出和基準(zhǔn)圖像反差較大的圖像范圍。 void monitor(CvCapture* capture, IplImage* std, CvRect* rect); // 求 x 的平方 int square(int x); int main(int argc, char* argv[]) { CvCapture* capture; // 攝像機(jī)源 IplImage* std; // 基準(zhǔn)圖像 CvRect rect; // 異常位置矩形標(biāo)識(shí) 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; // 檢測(cè)到異常的標(biāo)識(shí) int x1 = -1, x2 = 0; // 異常區(qū)域矩形橫坐標(biāo)范圍 int y1 = -1, y2 = 0; // 異常區(qū)域矩形縱坐標(biāo)范圍 uchar *ptr1b, *ptr1g, *ptr1r; // 基準(zhǔn)圖像的每個(gè)像素的三個(gè)顏色通道的值 uchar *ptr2b, *ptr2g, *ptr2r; // 實(shí)時(shí)圖像的每個(gè)像素的三個(gè)顏色通道的值 int squaresum; // 計(jì)算 RGB 差值平方和 // 遍歷圖像中的每一個(gè)點(diǎn),將實(shí)時(shí)采樣圖與基準(zhǔn)圖做比較,檢測(cè)兩者的每一個(gè) // 像素點(diǎn)的 RGB 差值平方和。當(dāng)該值大于 8192 時(shí)(換算成灰度值則意味著 // 兩者的灰度差大于 90)則立即報(bào)告出現(xiàn)異常,只有遍歷完畢后仍未找到異 // 常才報(bào)告沒有異常。 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; // 檢測(cè)到異常的標(biāo)識(shí) int finish = FALSE; // 采樣已完成的標(biāo)識(shí) clock_t start_time, real_time; // 時(shí)間段監(jiān)測(cè) 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; }
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C語言實(shí)現(xiàn)簡(jiǎn)單職工信息管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡(jiǎn)單職工信息管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-07-07C語言交換奇偶位與offsetof宏的實(shí)現(xiàn)方法
offsetof()是C自帶的一個(gè)宏,它的作用就是計(jì)算結(jié)構(gòu)體成員相對(duì)于首地址處的偏移量,下面這篇文章主要給大家介紹了關(guān)于C語言交換奇偶位與offsetof宏的實(shí)現(xiàn)方法,需要的朋友可以參考下2023-02-02《戰(zhàn)狼》中兩軍作戰(zhàn)入侵代碼竟然是輸出星期幾的!
這篇文章主要介紹了《戰(zhàn)狼》中兩軍作戰(zhàn)入侵代碼竟然是輸出星期幾的,喜歡戰(zhàn)狼和編程的同學(xué)可以了解下。2017-11-11C語言深入探究動(dòng)態(tài)規(guī)劃之線性DP
線性動(dòng)態(tài)規(guī)劃,是較常見的一類動(dòng)態(tài)規(guī)劃問題,其是在線性結(jié)構(gòu)上進(jìn)行狀態(tài)轉(zhuǎn)移,這類問題不像背包問題、區(qū)間DP等有固定的模板,線性動(dòng)態(tài)規(guī)劃的目標(biāo)函數(shù)為特定變量的線性函數(shù),約束是這些變量的線性不等式或等式,目的是求目標(biāo)函數(shù)的最大值或最小值2022-04-04C語言實(shí)現(xiàn)電話簿項(xiàng)目管理
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)電話簿項(xiàng)目管理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-07-07