You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

181 lines
5.0 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "motionDetector.h"
  2. motionDetector::motionDetector() : basicVideoAnalyzer(){
  3. m_iWidth = 500;
  4. m_iHeight = 0;
  5. type = VA_VM_DETECT;
  6. m_dArea = 20.0;
  7. m_iSST = 1;
  8. m_iCounterSST = 0;
  9. m_iWindow = 0;
  10. m_iCounterWindow = 0;
  11. m_bPIP = false;
  12. m_bDilatePicture = false;
  13. };
  14. motionDetector::motionDetector(int Width, int Height, double Area) :basicVideoAnalyzer(){
  15. m_iWidth = Width;
  16. m_iHeight = Height;
  17. m_dArea = Area;
  18. m_iSST = 1;
  19. type = VA_VM_DETECT;
  20. m_iCounterSST = 0;
  21. m_iWindow = 0;
  22. m_iCounterWindow = 0;
  23. m_bPIP = true;
  24. m_bDilatePicture = false;
  25. };
  26. motionDetector::~motionDetector(){
  27. };
  28. void motionDetector::setParametersEx(int SST, int DetectionWindow){
  29. m_iSST = SST;
  30. m_iWindow = DetectionWindow;
  31. };
  32. bool motionDetector::m_fImageAnalyzer(Mat &Picture){
  33. if( Picture.cols > 0 && Picture.rows > 0 ){
  34. Mat tMat;
  35. //if( m_iHeight == 0 ) m_iHeight = (m_iWidth * Picture.rows) / Picture.cols;
  36. if( m_iHeight == 0 ){
  37. m_iHeight = Picture.rows;
  38. m_iWidth = Picture.cols;
  39. };
  40. resize( Picture, tMat, Size( m_iWidth, m_iHeight ), 0.0, 0.0, INTER_AREA );
  41. cvtColor( tMat, tMat, CV_RGB2GRAY );
  42. equalizeHist( tMat, tMat );
  43. GaussianBlur( tMat, tMat, Size( 21, 21 ), 0 );
  44. if( m_oMat.empty() ){
  45. tMat.copyTo( m_oMat );
  46. return false;
  47. };
  48. Mat tDelta;
  49. absdiff( m_oMat, tMat, tDelta );
  50. tMat.copyTo( m_oMat );
  51. Mat tThresh;
  52. threshold( tDelta, tThresh, 25, 255, THRESH_BINARY );
  53. if( m_bDilatePicture ) {
  54. // dilate the thresholded image to fill in holes, then find contours
  55. // on thresholded image
  56. dilate( tThresh, tThresh, vector<double>(), Point(-1,-1), 2 );
  57. };
  58. findContours( tThresh, m_vContours, m_vHierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE );
  59. //if( m_vContours.size() > 0 ) return true;
  60. for( tContours::iterator index = m_vContours.begin(); index < m_vContours.end(); index++ ){
  61. if( contourArea( *index, false ) < m_dArea ) continue;
  62. if( m_iCounterSST > 0 ){
  63. m_iCounterSST--;
  64. return false;
  65. };
  66. m_iCounterWindow = m_iWindow;
  67. return true;
  68. };
  69. };
  70. if( m_iCounterWindow ){
  71. m_iCounterWindow--;
  72. return true;
  73. } else {
  74. m_iCounterSST = m_iSST;
  75. };
  76. return false;
  77. };
  78. void motionDetector::markup( Mat &Picture ){
  79. if (m_iCounterSST < 1){};
  80. char Buffer[20];
  81. sprintf(Buffer, "Motion Detected");
  82. rectangle(Picture, Point(Picture.cols - 120, 5), Point(Picture.cols - 10, 15), Scalar(0, 0, 255), CV_FILLED);
  83. putText(Picture, Buffer, Point(Picture.cols - 115, 13), FONT_HERSHEY_SIMPLEX, 0.3, Scalar(255, 0, 0), 1);
  84. /// Approximate contours to polygons + get bounding rects and circles
  85. tContour contours_poly;
  86. Rect tBoundRect;
  87. int i;
  88. double cA;
  89. for( tContours::iterator index = m_vContours.begin(); index < m_vContours.end(); index++ ){
  90. cA = contourArea(*index);
  91. approxPolyDP(*index, contours_poly, 3, true);
  92. if (cA > m_dArea){
  93. fillConvexPoly(Picture, contours_poly, Scalar(0, 0, 255));
  94. } else continue;
  95. //for( i= 0; i< index->size(); i++ ) {
  96. // (*index)[i].x = (*index)[i].x << 1;
  97. // (*index)[i].y = (*index)[i].y << 1;
  98. //};
  99. //approxPolyDP( *index, contours_poly, 3, true );
  100. //tBoundRect = boundingRect( *index );
  101. //drawContours( Picture, contoursType( contours_poly ), 0, Scalar( 255, 255, 255 ), 1, 8, vector<Vec4i>(), 0, Point() );
  102. //polylines( Picture, contours_poly, true, Scalar( 255, 255, 255 ), 2, 4, 0 );
  103. //fillConvexPoly( Picture, contours_poly, Scalar( 0, 0, 255 ));
  104. //rectangle( Picture, tBoundRect.tl(), tBoundRect.br(), Scalar( 0, 255, 255 ), 1, 8, 0 );
  105. };
  106. if( m_bPIP ){
  107. m_mBufferProtectorMutex.lock();
  108. Mat resizedSrc;
  109. Mat subView;
  110. Mat convertedSrc;
  111. resize( m_oMat, resizedSrc, Size( m_iWidth>>2, m_iHeight>>2 ), 0.0, 0.0, INTER_AREA );
  112. subView = Picture( Rect( ( Picture.cols - (m_iWidth>>2) - 10 ), 30, m_iWidth>>2, m_iHeight>>2 ) );
  113. cvtColor( resizedSrc, convertedSrc, CV_GRAY2RGB );
  114. convertedSrc.copyTo( subView );
  115. m_mBufferProtectorMutex.unlock();
  116. };
  117. };
  118. /*
  119. #include <cv.h>
  120. #include <highgui.h>
  121. #include <iostream>
  122. using namespace cv;
  123. int main( int argc, char** argv )
  124. {
  125. double alpha = 0.5; double beta; double input;
  126. Mat src1, src2, dst;
  127. /// Ask the user enter alpha
  128. std::cout<<" Simple Linear Blender "<<std::endl;
  129. std::cout<<"-----------------------"<<std::endl;
  130. std::cout<<"* Enter alpha [0-1]: ";
  131. std::cin>>input;
  132. /// We use the alpha provided by the user if it is between 0 and 1
  133. if( input >= 0.0 && input <= 1.0 )
  134. { alpha = input; }
  135. /// Read image ( same size, same type )
  136. src1 = imread("../../images/LinuxLogo.jpg");
  137. src2 = imread("../../images/WindowsLogo.jpg");
  138. if( !src1.data ) { printf("Error loading src1 \n"); return -1; }
  139. if( !src2.data ) { printf("Error loading src2 \n"); return -1; }
  140. /// Create Windows
  141. namedWindow("Linear Blend", 1);
  142. beta = ( 1.0 - alpha );
  143. addWeighted( src1, alpha, src2, beta, 0.0, dst);
  144. imshow( "Linear Blend", dst );
  145. waitKey(0);
  146. return 0;
  147. }
  148. */