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 - main.cpp

main.cpp

Caricato da: Carlduke
Scarica il programma completo

  1. #include <iostream>
  2.  
  3. #include <cv.h>
  4. #include <cxcore.h>
  5. #include <highgui.h>
  6.  
  7. #include "Image.h"
  8. #include "WatchPoint.h"
  9. #include "Thread.h"
  10. #include "Timer.h"
  11.  
  12. #define WHITESPACE " \t\n\r"
  13.  
  14. using namespace std;
  15.  
  16. static const char *window1_id  = "Camera input";
  17. bool running = true;
  18. bool draw_box = false;
  19. CvPoint pt1,pt2;
  20. IplImage *image = NULL;
  21. IplImage *motion_image = NULL;
  22. RgbImage input_image;
  23. std::vector<WatchPoint> watch_point;
  24. int tolerance = 60;
  25.  
  26. void mouse_handler1(int event,int x,int y,int flags,void *param);
  27. void keyboard_handler();
  28. DWORD update_watch_point(LPVOID data);
  29.  
  30. Thread<void> thread(NULL);
  31. Thread<void> update_thread(NULL);
  32.  
  33. DWORD thread_func(LPVOID data);
  34.  
  35. Timer *timer;
  36.  
  37. int main(int argc,char *argv[])
  38. {
  39.         thread.ThreadFunction = thread_func;
  40.         thread.Create();
  41.  
  42.         CvCapture *capture = NULL;
  43.  
  44.         cvNamedWindow(window1_id,CV_WINDOW_NORMAL);
  45.         cvMoveWindow(window1_id,100,100);
  46.         cvResizeWindow(window1_id,800,600);
  47.         cvSetMouseCallback(window1_id,mouse_handler1);
  48.  
  49.         motion_image = cvLoadImage("OnMotionDetected.png");
  50.  
  51.         capture = cvCaptureFromCAM(0);
  52.  
  53.         image = cvQueryFrame(capture);
  54.         //processed_image = image;
  55.  
  56.         update_thread.ThreadFunction = update_watch_point;
  57.         update_thread.Create();
  58.  
  59.         while(running)
  60.         {
  61.      image = cvQueryFrame(capture);
  62.          input_image = image;
  63.          
  64.  
  65.          keyboard_handler();
  66.  
  67.          if(draw_box)
  68.            cvRectangle(input_image,pt1,pt2,cvScalar(255.0,255.0,255.0),5,10);
  69.      
  70.  
  71.          for(int i = 0;i < watch_point.size();i++)
  72.          {
  73.                  cvRectangle(input_image,watch_point[i]._p1,watch_point[i]._p2,cvScalar(255.0,155.0,155.0),5,1);
  74.          }
  75.  
  76.          cvShowImage(window1_id,input_image);
  77.         }
  78.  
  79.        
  80.         update_thread.Close();
  81.         thread.Close();
  82.  
  83.         Sleep(200);
  84.  
  85.         cvReleaseImage(&motion_image);
  86.         cvReleaseCapture(&capture);
  87.         cvDestroyWindow(window1_id);
  88.        
  89.  
  90.         exit(0);
  91.  
  92.         return 0;
  93. }
  94.  
  95. void mouse_handler1(int event,int x,int y,int flags,void *param)
  96. {
  97.         switch(event)
  98.         {
  99.                 case CV_EVENT_RBUTTONDOWN:
  100.                 {
  101.                         //(flags & CV_EVENT_FLAG_CTRLKEY)
  102.                         {
  103.                          draw_box = true;
  104.                          pt1.x = x;
  105.                          pt1.y = y;
  106.                          cout<<"Left button pressed at "<<x<<" "<<y<<endl;
  107.                         }
  108.                 }
  109.                 break;
  110.  
  111.                 case CV_EVENT_RBUTTONUP:
  112.                 {
  113.                         //if(flags & CV_EVENT_FLAG_CTRLKEY)
  114.                         {
  115.                          draw_box = false;
  116.                          watch_point.push_back(WatchPoint(input_image,pt1,pt2));
  117.                         }
  118.                 }
  119.                 break;
  120.  
  121.                 case CV_EVENT_MOUSEMOVE:
  122.                 {
  123.                         //if(flags & CV_EVENT_FLAG_CTRLKEY)
  124.                         {
  125.                          if(draw_box)
  126.                          {
  127.                                 pt2.x = x;
  128.                                 pt2.y = y;
  129.                          }
  130.                         }
  131.                 }
  132.                 break;
  133.  
  134.                 default: break;
  135.         }
  136. }
  137.  
  138. void keyboard_handler()
  139. {
  140.         char key = cvWaitKey(1);
  141.  
  142.         if(key == 27)
  143.         {
  144.                 running = false;
  145.         }
  146.  
  147.         if(key == '-')
  148.         {
  149.                 tolerance--;
  150.                 if(tolerance < 0)tolerance = 0;
  151.                  cout<<"tolerance: "<<tolerance<<endl;
  152.         }
  153.  
  154.         if(key == '+')
  155.         {
  156.                 tolerance++;
  157.             cout<<"tolerance: "<<tolerance<<endl;
  158.         }
  159.  
  160.         if(key == 'i')
  161.         {
  162.        watch_point[0].improve_quality();
  163.         }
  164. }
  165.  
  166. DWORD update_watch_point(LPVOID data)
  167. {
  168.         while(update_thread.is_running())
  169.         {
  170.           Sleep(10);
  171.           static int c = 0;
  172.           for(int i = 0;i < watch_point.size();i++)
  173.           {
  174.             watch_point[i].flip_image(input_image);
  175.             watch_point[i].set_tolerance(tolerance);
  176.             watch_point[i].set_motion_image(motion_image);
  177.             watch_point[i].movement();
  178.           }
  179.         }
  180.  
  181.         return 0;
  182. }
  183.  
  184. DWORD thread_func(LPVOID data){
  185.         while(thread.is_running())
  186.         {
  187.                 char char_text[100];
  188.                 char *token = NULL;
  189.  
  190.                 cin.getline(char_text,100,'\n');
  191.  
  192.                 token = strtok(char_text,WHITESPACE);
  193.  
  194.                
  195.                 if(strcmp(token,"exit") == 0)
  196.                 {
  197.                         running = false;
  198.                 }
  199.                 else if(strcmp(token,"watchpoint") == 0)
  200.                 {
  201.                          int sensor = atoi(strtok(NULL,WHITESPACE));
  202.  
  203.                          token = strtok(NULL,WHITESPACE);
  204.  
  205.                          if(sensor > watch_point.size())
  206.                          {
  207.                                         cout<<"Watch point "<<sensor<<" doesn't exis"<<endl;
  208.                                         continue;
  209.                          }
  210.  
  211.                          if(strcmp(token,"tolerance") == 0)
  212.                          {
  213.                                  watch_point[sensor].set_tolerance(atoi(strtok(NULL,WHITESPACE)));
  214.                                  cout<<"Watchpoint "<<sensor<<" tolerance set to "<<watch_point[sensor].get_tolerance()<<endl;
  215.                          }
  216.                         else if(strcmp(token,"fliptime") == 0)
  217.                         {
  218.                                 watch_point[sensor].set_fliptime(atoi(strtok(NULL,WHITESPACE)));
  219.                                 cout<<"Watchpoint "<<sensor<<" fliptime set to "<<watch_point[sensor].get_fliptime()<<endl;
  220.                         }
  221.                         else if(strcmp(token,"improve") == 0)
  222.                         {
  223.                                  cout<<"Improving watchpoint "<<sensor<<endl;
  224.  
  225.                                  watch_point[sensor].improve_quality();
  226.                         }
  227.                         else
  228.                         {
  229.                                 cout<<"Unknown command"<<endl;
  230.                         }
  231.                 }
  232.                 else
  233.                 {
  234.                         cout<<"Unkown command"<<endl;
  235.                 }
  236.                
  237.         }
  238.  
  239.         cout<<"Quitting thread"<<endl;
  240.  
  241.         return 0;
  242. }