欧美大屁股bbbbxxxx,狼人大香伊蕉国产www亚洲,男ji大巴进入女人的视频小说,男人把ji大巴放进女人免费视频,免费情侣作爱视频

歡迎來到入門教程網(wǎng)!

C語言

當(dāng)前位置:主頁 > 軟件編程 > C語言 >

OpenCV實現(xiàn)簡單攝像頭視頻監(jiān)控程序

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊: 次

如何在冗長的監(jiān)控錄像中找到關(guān)鍵點?我們知道,監(jiān)控錄像中大部分信息都是沒用的,那些信息就等同于一幅靜態(tài)圖像。我們要等待監(jiān)控的范圍內(nèi)出現(xiàn)異常情況時再跟蹤。

這其實是一個最簡單的計算機圖像處理程序。簡單的思路是這樣的:首先給攝像頭取景采樣,當(dāng)背景穩(wěn)定時,以該圖片作為基準(zhǔn)圖片。然后在監(jiān)控過程中,若出現(xiàn)了和基準(zhǔn)圖片反差較大的視頻幀,那么啟動捕捉程序,并標(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
 
// 檢測圖像異常,僅在采樣時調(diào)用。
// 返回真表示已檢測到異常,需要重新采樣。
// 返回假表示未檢測到異常,在一定時間后即可獲取基準(zhǔn)圖像。
int detect(CvCapture* capture, IplImage* std, IplImage* frm, CvRect* rect);
 
// 圖像采樣,確定基準(zhǔn)圖像,以便監(jiān)測場景變化
// 返回真表示采樣成功,返回假表示采樣失敗
int gather(CvCapture* capture, IplImage* std, CvRect* rect);
 
// 攝像機監(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;   // 攝像機源
 IplImage* std;     // 基準(zhǔn)圖像
 CvRect rect;      // 異常位置矩形標(biāo)識
 
 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;         // 檢測到異常的標(biāo)識
 int x1 = -1, x2 = 0;      // 異常區(qū)域矩形橫坐標(biāo)范圍
 int y1 = -1, y2 = 0;      // 異常區(qū)域矩形縱坐標(biāo)范圍
 
 uchar *ptr1b, *ptr1g, *ptr1r;  // 基準(zhǔn)圖像的每個像素的三個顏色通道的值
 uchar *ptr2b, *ptr2g, *ptr2r;  // 實時圖像的每個像素的三個顏色通道的值
 
 int squaresum;         // 計算 RGB 差值平方和
 
 // 遍歷圖像中的每一個點,將實時采樣圖與基準(zhǔn)圖做比較,檢測兩者的每一個
 // 像素點的 RGB 差值平方和。當(dāng)該值大于 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;       // 檢測到異常的標(biāo)識
 int finish = FALSE;       // 采樣已完成的標(biāo)識
 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;
}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:C語言中二維數(shù)組作為函數(shù)參數(shù)來傳遞的三種方法

欄    目:C語言

下一篇:華為筆試算法題匯總

本文標(biāo)題:OpenCV實現(xiàn)簡單攝像頭視頻監(jiān)控程序

本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/215.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負(fù)任何責(zé)任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有