Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
SecurityCam - WatchPoint.cpp

WatchPoint.cpp

Caricato da: Carlduke
Scarica il programma completo

  1. #include "WatchPoint.h"
  2. #include "Image.h"
  3. #include "cvUtils.h"
  4.  
  5.  
  6. int WatchPoint::total_watch_point = 0;
  7. Timer *WatchPoint::timer = NULL;
  8.  
  9. WatchPoint::WatchPoint(IplImage *_image,CvPoint p1,CvPoint p2) : Box(p1,p2)
  10. {
  11.   counter = 0;
  12.  
  13.   motion = false;
  14.   improvement_has_been_made = false;
  15.   improvement_level = 1;
  16.  
  17.   tolerance = 5;
  18.   if(!_image)
  19.   {
  20.           cout<<"Image to WatchPoint constructor null"<<endl;
  21.       return;
  22.   }
  23.  
  24.  
  25.   ipl_initial_image = cvCreateImage(cvSize(640,480),IPL_DEPTH_8U,3);
  26.   cvCopy(_image,ipl_initial_image);
  27.  
  28.   current_watch_point = total_watch_point;
  29.   total_watch_point++;
  30.  
  31.   stringstream ss;
  32.   ss<<"Watch point "<<current_watch_point;
  33.   window_name = ss.str();
  34.  
  35.   cvNamedWindow(window_name.c_str(),CV_WINDOW_NORMAL);
  36.   cvMoveWindow(window_name.c_str(),100,100);
  37.   cvResizeWindow(window_name.c_str(),640,480);
  38.  
  39.   on_motion_image = NULL;
  40.  
  41.   if(!timer)
  42.   {
  43.           timer = new Timer();
  44.           timer->start();
  45.   }
  46.   spawn_sec = timer->get_seconds();
  47.  
  48.   fliptime = 30;
  49. }
  50.  
  51. WatchPoint::~WatchPoint()
  52. {
  53.         /*total_watch_point--;
  54.         if(total_watch_point  == 0)
  55.         {
  56.                 timer->stop();
  57.                 delete timer;
  58.         }*/
  59. }
  60.  
  61. void WatchPoint::flip_image(IplImage *img)
  62. {
  63.         ipl_current_image = img;
  64. }
  65.  
  66. void WatchPoint::set_tolerance(float t)
  67. {
  68.         tolerance = t;
  69. }
  70.  
  71. void WatchPoint::set_motion_image(IplImage *img)
  72. {
  73.     on_motion_image = img;
  74. }
  75.  
  76. void WatchPoint::set_fliptime(int time)
  77. {
  78.         fliptime = time;
  79. }
  80.  
  81. void WatchPoint::createRgbImage(RgbImage &initial_image,RgbImage &current_image)
  82. {
  83.         CvSize size;
  84.         size.height = (_p1.y < _p2.y ? _p2.y - _p1.y : _p1.y - _p2.y);
  85.         size.width  = (_p1.x < _p2.x ? _p2.x - _p1.x : _p1.x - _p2.x);
  86.  
  87.         CvPoint start;
  88.         start.x = (_p1.x < _p2.x ? _p1.x : _p2.x);
  89.         start.y = (_p1.y < _p2.y ? _p1.y : _p2.y);
  90.  
  91.         for(int x = start.x; x < start.x + size.width; x++)
  92.          for(int y = start.y; y < start.y + size.height; y++)
  93.          {
  94.            if(x < 0 || x > 640 || y < 0 || y > 480)continue;
  95.  
  96.            initial_image[y-start.y][x-start.x] = RgbImage(ipl_initial_image)[y][x];
  97.            current_image[y-start.y][x-start.x] = RgbImage(ipl_current_image)[y][x];
  98.          }
  99.  
  100.          if(improvement_has_been_made)
  101.          {
  102.                  improve_quality(true);
  103.          }
  104. }
  105.  
  106. bool WatchPoint::movement()
  107. {
  108.         int pix_num = 0;
  109.         int tot_pix = 0;
  110.  
  111.         IplImage *temp_image1,*temp_image2;
  112.  
  113.         CvSize size;
  114.         size.height = (_p1.y < _p2.y ? _p2.y - _p1.y : _p1.y - _p2.y);
  115.         size.width  = (_p1.x < _p2.x ? _p2.x - _p1.x : _p1.x - _p2.x);
  116.    
  117.         CvPoint start;
  118.         start.x = (_p1.x < _p2.x ? _p1.x : _p2.x);
  119.         start.y = (_p1.y < _p2.y ? _p1.y : _p2.y);
  120.  
  121.         if(improvement_has_been_made)
  122.         {
  123.                 size.height *= 2;
  124.                 size.width *= 2;
  125.                 start.x *= 2;
  126.                 start.y *= 2;
  127.         }
  128.  
  129.         temp_image1 = cvCreateImage(size,IPL_DEPTH_8U,3);
  130.         temp_image2 = cvCreateImage(size,IPL_DEPTH_8U,3);
  131.  
  132.     RgbImage initial_image(temp_image1);
  133.     RgbImage current_image(temp_image2);
  134.  
  135.         createRgbImage(initial_image,current_image);
  136.  
  137.         for(int x = start.x; x < start.x + size.width; x++)
  138.          for(int y = start.y; y < start.y + size.height; y++)
  139.          {
  140.           if(!improvement_has_been_made)
  141.           {
  142.             if(x < 0 || x > 640 || y < 0 || y > 480)continue;
  143.           }
  144.           else
  145.           {
  146.         if(x < 0 || x > 640*2 || y < 0 || y > 480*2)continue;
  147.           }
  148.  
  149.            tot_pix++;
  150.  
  151.            if((current_image[y-start.y][x-start.x].r >= initial_image[y-start.y][x-start.x].r-tolerance && current_image[y-start.y][x-start.x].r <= initial_image[y-start.y][x-start.x].r+tolerance) ||
  152.               (current_image[y-start.y][x-start.x].g >= initial_image[y-start.y][x-start.x].g-tolerance && current_image[y-start.y][x-start.x].g <= initial_image[y-start.y][x-start.x].g+tolerance) ||
  153.               (current_image[y-start.y][x-start.x].b >= initial_image[y-start.y][x-start.x].b-tolerance && current_image[y-start.y][x-start.x].b <= initial_image[y-start.y][x-start.x].b+tolerance))
  154.                   {
  155.                  
  156.                   }
  157.                   else
  158.               {
  159.                         pix_num++;
  160.                   }
  161.          }
  162.  
  163.          if(pix_num >= (10*tot_pix)/100)
  164.          {
  165.                         motion = true;
  166.          }
  167.          else
  168.          {
  169.                  motion = false;
  170.          }
  171.  
  172.          draw(initial_image,current_image,on_motion_image);
  173.          //cvShowManyImages(window_name.c_str(),2,initial_image,current_image);
  174.  
  175.          cvReleaseImage(&temp_image1);
  176.          cvReleaseImage(&temp_image2);
  177.  
  178.          int sec = timer->get_seconds();
  179.  
  180.          if(timer->delta_time(sec,spawn_sec) > fliptime)
  181.          {
  182.                 spawn_sec = timer->get_seconds();
  183.                 if(!motion)
  184.                 {
  185.                   cout<<"Updating image on watchpoint "<<current_watch_point<<endl;
  186.                   cvCopy(ipl_current_image,ipl_initial_image);
  187.                 }
  188.          }
  189.  
  190.  return motion;
  191. }
  192.  
  193. void WatchPoint::draw(IplImage *img1,IplImage *img2,IplImage *img3)
  194. {
  195.         if(!motion)
  196.                 cvShowManyImages(window_name.c_str(),2,img1,img2);
  197.         else
  198.                 cvShowManyImages(window_name.c_str(),3,img1,img2,img3);
  199. }
  200.  
  201. void WatchPoint::improve_quality(bool called_by_movement)
  202. {
  203.         CvSize size;
  204.         size.width = ipl_current_image->width*2;
  205.         size.height = ipl_current_image->height*2;
  206.  
  207.         RgbImage initial_image(ipl_initial_image);
  208.         RgbImage current_image(ipl_current_image);
  209.  
  210.         RgbImage new_initial_image = cvCreateImage(size,IPL_DEPTH_8U,3);
  211.         RgbImage new_current_image = cvCreateImage(size,IPL_DEPTH_8U,3);
  212.  
  213.         int i = 0,j = 0;
  214.  
  215.         cout<<"making new images"<<endl;
  216.         for(int x = 0;x < size.width-2;x += 2)
  217.         {
  218.                 for(int y = 0;y < size.height;y++)
  219.                 {
  220.  
  221.                         new_current_image[y][x]     = current_image[j][i];
  222.                         new_current_image[y][x+1]   = current_image[j][i];
  223.                
  224.                     new_initial_image[y][x]     = initial_image[j][i];
  225.                         new_initial_image[y][x+1]   = initial_image[j][i];
  226.                        
  227.                         j = y/2;
  228.                 }
  229.                 i = x/2;
  230.         }
  231.         cout<<"made"<<endl;
  232.  
  233.        
  234.         if(!called_by_movement)
  235.         {
  236.      cvReleaseImage(&ipl_initial_image);
  237.          ipl_initial_image = cvCreateImage(size,IPL_DEPTH_8U,3);
  238.          cvCopy(new_initial_image,ipl_initial_image);
  239.         }
  240.        
  241.         ipl_current_image = new_current_image;
  242.  
  243.         cvReleaseImage((IplImage**)&new_initial_image);
  244.         cvReleaseImage((IplImage**)&new_current_image);
  245.  
  246.         improvement_has_been_made = true;
  247.         //improvement_level++;
  248. }