|
|
#include "motionDetector.h"
motionDetector::motionDetector() : basicVideoAnalyzer(){ m_iWidth = 500; m_iHeight = 0; type = VA_VM_DETECT; m_dArea = 20.0; m_iSST = 1; m_iCounterSST = 0; m_iWindow = 0; m_iCounterWindow = 0; m_bPIP = false; m_bDilatePicture = false;};
motionDetector::motionDetector(int Width, int Height, double Area) :basicVideoAnalyzer(){ m_iWidth = Width; m_iHeight = Height; m_dArea = Area; m_iSST = 1; type = VA_VM_DETECT; m_iCounterSST = 0; m_iWindow = 0; m_iCounterWindow = 0; m_bPIP = true; m_bDilatePicture = false;};
motionDetector::~motionDetector(){};
void motionDetector::setParametersEx(int SST, int DetectionWindow){ m_iSST = SST; m_iWindow = DetectionWindow;};
bool motionDetector::m_fImageAnalyzer(Mat &Picture){ if( Picture.cols > 0 && Picture.rows > 0 ){ Mat tMat; //if( m_iHeight == 0 ) m_iHeight = (m_iWidth * Picture.rows) / Picture.cols;
if( m_iHeight == 0 ){ m_iHeight = Picture.rows; m_iWidth = Picture.cols; }; resize( Picture, tMat, Size( m_iWidth, m_iHeight ), 0.0, 0.0, INTER_AREA ); cvtColor( tMat, tMat, CV_RGB2GRAY ); equalizeHist( tMat, tMat ); GaussianBlur( tMat, tMat, Size( 21, 21 ), 0 ); if( m_oMat.empty() ){ tMat.copyTo( m_oMat ); return false; }; Mat tDelta;
absdiff( m_oMat, tMat, tDelta );
tMat.copyTo( m_oMat ); Mat tThresh;
threshold( tDelta, tThresh, 25, 255, THRESH_BINARY ); if( m_bDilatePicture ) { // dilate the thresholded image to fill in holes, then find contours
// on thresholded image
dilate( tThresh, tThresh, vector<double>(), Point(-1,-1), 2 ); }; findContours( tThresh, m_vContours, m_vHierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE ); //if( m_vContours.size() > 0 ) return true;
for( tContours::iterator index = m_vContours.begin(); index < m_vContours.end(); index++ ){ if( contourArea( *index, false ) < m_dArea ) continue; if( m_iCounterSST > 0 ){ m_iCounterSST--; return false; }; m_iCounterWindow = m_iWindow; return true; }; }; if( m_iCounterWindow ){ m_iCounterWindow--; return true; } else { m_iCounterSST = m_iSST; }; return false;};
void motionDetector::markup( Mat &Picture ){ if (m_iCounterSST < 1){};
char Buffer[20]; sprintf(Buffer, "Motion Detected"); rectangle(Picture, Point(Picture.cols - 120, 5), Point(Picture.cols - 10, 15), Scalar(0, 0, 255), CV_FILLED); putText(Picture, Buffer, Point(Picture.cols - 115, 13), FONT_HERSHEY_SIMPLEX, 0.3, Scalar(255, 0, 0), 1);
/// Approximate contours to polygons + get bounding rects and circles
tContour contours_poly; Rect tBoundRect; int i; double cA;
for( tContours::iterator index = m_vContours.begin(); index < m_vContours.end(); index++ ){ cA = contourArea(*index); approxPolyDP(*index, contours_poly, 3, true); if (cA > m_dArea){ fillConvexPoly(Picture, contours_poly, Scalar(0, 0, 255)); } else continue; //for( i= 0; i< index->size(); i++ ) {
// (*index)[i].x = (*index)[i].x << 1;
// (*index)[i].y = (*index)[i].y << 1;
//};
//approxPolyDP( *index, contours_poly, 3, true );
//tBoundRect = boundingRect( *index );
//drawContours( Picture, contoursType( contours_poly ), 0, Scalar( 255, 255, 255 ), 1, 8, vector<Vec4i>(), 0, Point() );
//polylines( Picture, contours_poly, true, Scalar( 255, 255, 255 ), 2, 4, 0 );
//fillConvexPoly( Picture, contours_poly, Scalar( 0, 0, 255 ));
//rectangle( Picture, tBoundRect.tl(), tBoundRect.br(), Scalar( 0, 255, 255 ), 1, 8, 0 );
}; if( m_bPIP ){ m_mBufferProtectorMutex.lock(); Mat resizedSrc; Mat subView; Mat convertedSrc; resize( m_oMat, resizedSrc, Size( m_iWidth>>2, m_iHeight>>2 ), 0.0, 0.0, INTER_AREA ); subView = Picture( Rect( ( Picture.cols - (m_iWidth>>2) - 10 ), 30, m_iWidth>>2, m_iHeight>>2 ) ); cvtColor( resizedSrc, convertedSrc, CV_GRAY2RGB ); convertedSrc.copyTo( subView ); m_mBufferProtectorMutex.unlock(); };};
/*
#include <cv.h>
#include <highgui.h>
#include <iostream>
using namespace cv;
int main( int argc, char** argv ){ double alpha = 0.5; double beta; double input;
Mat src1, src2, dst;
/// Ask the user enter alpha
std::cout<<" Simple Linear Blender "<<std::endl; std::cout<<"-----------------------"<<std::endl; std::cout<<"* Enter alpha [0-1]: "; std::cin>>input;
/// We use the alpha provided by the user if it is between 0 and 1
if( input >= 0.0 && input <= 1.0 ) { alpha = input; }
/// Read image ( same size, same type )
src1 = imread("../../images/LinuxLogo.jpg"); src2 = imread("../../images/WindowsLogo.jpg");
if( !src1.data ) { printf("Error loading src1 \n"); return -1; } if( !src2.data ) { printf("Error loading src2 \n"); return -1; }
/// Create Windows
namedWindow("Linear Blend", 1);
beta = ( 1.0 - alpha ); addWeighted( src1, alpha, src2, beta, 0.0, dst);
imshow( "Linear Blend", dst );
waitKey(0); return 0;} */
|