OpenCV温故而知新: 人流统计

应用场景

人流统计主要被广泛应用于一些商场,或者步行街等与人流相关的商业活动场所,这也是以人流来评价一个地方(或者是在一定时间内)的商业价值的一个重要手段。除这个人流统计之外,其实还有一个类似的算法是车流统计,车流统计主要用于高速公路或者马路。但从算法实现、技术角度来讲,这二者其实并没有太大的差别。

补充说明

本算法在OpenCV的自带示例里有完整代码,你完全可以参考该代码进行学习和修改。 Continue reading “OpenCV温故而知新: 人流统计”

OpenCV温故而知新: 遺失物偵測

函式功能规格

全域类别说明:

class CLostDetect{

private:

int         nAlarmFlag         ;                    //警告之flag

int         nWidth, nHeight ;                    //Frame之宽高

int         nAlarmTimer              ;                    //警告时间

 

bool      bEgdeDen           ;                    //edge density的变数

bool      bObjectSetted            ;                    //侦测物是否设定

int         nHdims               ;                    //Histogram维度变数

float      pHranges_arr[2]  ;                    //Histogram范围大小

float      *pHranges           ;

CvRect cvRectObjectWinodw       ;             //设定选取对象之框的变量

CvHistogram *pRHist                   ;             //计算R & G & B之histogram

CvHistogram *pGHist                    ;

CvHistogram *pBHist                    ;

IplImage *pCurFrame                            ;     //input image

IplImage *pGray                             ;      //color to gray

IplImage *pEdge                                 ;      //edge image

IplImage *pObjectImageModel            ;      //for rotate 0

IplImage *pObjectImageModel1          ;      //for rotate 15 degree

IplImage *pObjectImageModelMinus1       ;      //for rotate-15 degree

public:

CLostDetect(int width, int height, int alarmerTimer);                            //建构子

~CLostDetect();                                                                        //解构子

bool Reset() { bObjectSetted = false; return true;};                  //把bObjectSetted设为false

bool SetImage(BYTE* pImageArray);                                             //承接AP所传入的变量型态

bool SetImage(IplImage *pImage);                                           //whole algorithm

bool SetObject(IplImage *pImage, int x, int y, int width, int height);

//设定object model

int   GetObjectState();                                                               //计算edge density

bool Alarm() {return (nAlarmFlag>nAlarmTimer); };               //send alarm message

};

成员函式说明:

CLostDetect::CLostDetect(int width, int height, int nSeconds=5){

Purpose: initial all variables we’ll use the program

Usage: set all variable will use.

Parameter:

Parameter

Type

Description

width

int

the width of frame

height

int

the height of frame

nSeconds

int

alarm time set to 5

}

CLostDetect::~CLostDetect(){

Purpose: release variables

release all image variables

release both Hist variables

}

bool CLostDetect::SetImage(BYTE* pImageArray){

Purpose: Succeed to AP’s package to IplImage data type variable.

Usage: While webcam give image data into the program, check it data type.

Parameter:

Parameter

Type

Description

pImageArray BYTE Succeed input data type

Return:

True, if success

False, otherwise

}

bool CLostDetect::SetImage(IplImage *pImage){

Purpose: detect whether the lost happened.

Usage: While AP given the right data type, starting the algorithm of detection

Parameter:

Parameter

Type

Description

PImage IplImage If the input data is not NULL, starting to go the process

Return:

True, if success

False, otherwise

}

bool CLostDetect::SetObject(IplImage *pImage, int x, int y, int width, int height){

Purpose: set the object model.

Usage: given image frame, object position, width and height, return bool variable.

Parameter:

Parameter

Type

Description

pImage

IplImage

input image variable

x

int

Position of x

y

int

Position of y

width

int

Width of frame

height

int

Height of frame

Return:

True, if success

False, otherwise

}

int CLostDetect::GetObjectState(){

Purpose: After setting object model, quantify the object model edge density.

Usage: compute the edge density of object model.

Return:

0 when the procedure finished

}

测试程序及序列

测试程序: http://rg4.net/p/easyiv/liblostdetect_test.7z。测试程序快捷键:按r重新设定ROI检测区域,按p暂停处理,按t中止。

测试视频序列:http://rg4.net/p/easyiv/liblostdetect_sample.7z

 

OpenCV温故而知新: 画面遮蔽功能实现

应用场景

画面遮蔽侦测主要应用于一些强制性开放的视频地点,如:煤矿矿产区,或者是一些特定的娱乐场所,这些视频在一些时候通常是要作为“现场证据”来呈现的,而有人的却可能由于各种原因,不想让视频给录下来…这个模块的目标就是检测出画面遮蔽,并告警通知相关人员去进行必要的处理。

工作原理

先将视频进行灰度处理,然后进行背景、前景差检测画面的差异,并检测差异部分是否是大片连续的,若是则认为画面被遮蔽。

补充说明

由于本算法对其应用场景有一定的特殊要求,若是放到不太合适的地方容易出现许多误报的状况。

以下是接口说明。

函式功能规格说明

全域类别说明

class CMaliciousDetect{

private:

bool bAlarmFlag;                                   //发出警告的bool variable

int nAlarmTimer;                             //警告时间

int nWidth, nHeight;                       //Frame之宽高

bool IsSetted;                                         //background是否设定变量

IplImage *Frame;                                   //input image

IplImage *Gray;                             //把input image转换成gray level

IplImage *C_edge;                       // The edge value image

IplImage *BG_edge;

IplImage *_small;                                //resize C_edge to _small

IplImage *BG_small;                            //       BG_edge to _small

CvHistogram *BG_hist,*C_hist;          //计算histogram for 变量BG_hist & C_hist

int hist_size ;                                         //设histogram之纵轴范围

CvScalar  num1 , num2 ;                     //计算BG_small&_small的edge value

int WCount;                                          //出现warning的时间变量

int OCount;                                     //发生状况一的遮蔽,计算秒数的变量

int ECount;                                     //发生状况二的遮蔽,计算秒数的变量

int HCount;                                     //发生状况三的遮蔽,计算秒数的变变

int H_HT ;                                       //histogram high and low threshold

int H_LT ;

double HistDiff;                                   //the difference of BG_hist & C_hist

public:

CMaliciousDetect( int nImageWidth, int nImageHeight, int nSeconds );  //建构子

~CMaliciousDetect();                                                                        //解构子

bool SetImage(BYTE *pImageArray);  //由AP承接image package

bool SetImage(IplImage *pImage);       //Whole algorithm

bool Alarm() {return bAlarmFlag;};      //send the alarm information

void ResetSystem() {IsSetted = false;};//判断是否已经设定background

};

成员函式说明:

CMaliciousDetect::CMaliciousDetect(int nImageWidth, int nImageHeight, int nSeconds){

Purpose:initial image variables and Hist variables.

Usage :set all variable will use.

Parameter:

Parameter Type Description
nImageWidth int the width of frame
nImageHeight int the height of frame
nSeconds int alarm time

}

CMaliciousDetect::~CMaliciousDetect(){

Purpose:release variables

release all image variables

release both Hist variables

}

bool CMaliciousDetect::SetImage(BYTE* pImageArray){

Purpose: Succeed to AP’s package.

Usage: While webcam give image data into the program, check it data type.

Parameter:

Parameter

Type

Description

pImageArray BYTE input data type

Return:

True, if success

False, otherwise

}

bool CMaliciousDetect::SetImage(IplImage *pImage){

Purpose: detect whether the occlusion happened.

Usage: While AP given the right data type, starting the algorithm of detection

Parameter

Parameter

Type

Description

PImage IplImage If the input data is not NULL, starting to go the process

Return:

True, if success

False, otherwise

}

测试程序及序列

测试程序: http://rg4.net/p/easyiv/libmaliciousdetect_test.7z

测试视频序列:http://rg4.net/p/easyiv/libmaliciousdetect_sample.7z