Guest User

Untitled

a guest
May 23rd, 2018
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.99 KB | None | 0 0
  1. #include "StdAfx.h"
  2. #include "cv.h"
  3. #include "highgui.h"
  4. #include <stdio.h>  
  5. #include <Windows.h>
  6. #include <iostream>
  7.  
  8. using namespace std;
  9. using namespace cv;
  10. static int posX = 0;
  11. static int posY = 0;
  12.  
  13.  
  14. int lastX = posX;
  15. int lastY = posY;
  16.  
  17.  
  18. uchar*    ptr = NULL;
  19. int white_pixels = 255;
  20.  
  21. CvScalar lorange = cvScalar(120,120,30);
  22.  
  23. CvScalar hirange = cvScalar(170,150,50);
  24.  
  25.  
  26.  
  27.  
  28. bool do_track = false;
  29. bool use_hsv = true;
  30.  
  31. int clicked_x = -1;
  32. int clicked_y = -1;
  33. void mousehandler( int event, int x, int y, int flags, void* param )
  34. {
  35.     clicked_x = -1;
  36.     if( event == CV_EVENT_LBUTTONDOWN )
  37.     {
  38.         clicked_x = x;
  39.         clicked_y = y;
  40.                 printf("clicked %d %d\n", x,y);
  41.     }
  42. }
  43.  
  44.  
  45. IplImage* GetThresholdedImage(IplImage* rec)
  46. {
  47.        
  48.         IplImage* GetThresholdedImage(IplImage* rec);
  49.         IplImage* recHSV = cvCreateImage(cvGetSize(rec), 8, 3);
  50.         cvCvtColor(rec, recHSV, CV_BGR2HSV);
  51.         IplImage* recThreshed = cvCreateImage(cvGetSize(rec), 8, 1);
  52.         cvInRangeS(recHSV,cvScalar(20,100,100),cvScalar(30,255,255), recThreshed);
  53.         cvReleaseImage(&recHSV);
  54.         return recThreshed;
  55. }
  56.  
  57. IplImage* GetThresholdedImageRGB(IplImage* rec)
  58. {
  59.         IplImage* recThreshed = cvCreateImage(cvGetSize(rec), 8, 1);
  60.         cvInRangeS(rec,cvScalar(20,100,100),cvScalar(30,255,255), recThreshed);
  61.         return recThreshed;
  62. }
  63.  
  64.  
  65. void GetPrimaryScreenSizeInPixels( SIZE& s)
  66. {
  67. ZeroMemory( &s, sizeof(SIZE) );
  68. s.cx = (LONG)::GetSystemMetrics( SM_CXFULLSCREEN );
  69. s.cy = (LONG)::GetSystemMetrics( SM_CYFULLSCREEN );
  70. }
  71.  
  72. int clamp(int v)
  73. {
  74.     if (v < 0 )
  75.         return 0;
  76.     if (v > 255)
  77.         return 255;
  78.  
  79.     return v;
  80. }
  81. CvMemStorage* storage = cvCreateMemStorage(0);
  82.  CvSeq* cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , storage);
  83.  int main()
  84.  {
  85.  CvCapture* zaznam = cvCaptureFromCAM( CV_CAP_ANY); //zaznamenavanie z kamery
  86.  if ( !zaznam )
  87.  {
  88.      fprintf( stderr, "Chyba! zaznam je bez hodnoty \n ");
  89.      getchar();
  90.      return -1;
  91.  }
  92.  
  93.  
  94.  cvNamedWindow("video",CV_WINDOW_AUTOSIZE);
  95.  cvNamedWindow("thresh",CV_WINDOW_AUTOSIZE);
  96.  IplImage* recScribble = NULL;
  97.  IplImage* snimok = cvQueryFrame(zaznam);
  98.  cvSetMouseCallback("video", mousehandler );
  99.  
  100.  while (true)
  101.  {
  102.     snimok = cvQueryFrame(zaznam);
  103.         if (!snimok)
  104.          {
  105.              fprintf( stderr, "Chyba! snimok je bez hodnoty");
  106.              getchar();
  107.              break;
  108.          }
  109.        
  110.     cvFlip(snimok,snimok,1);
  111.     CvSize cs = cvGetSize(snimok);
  112.     int cam_w = cs.width;
  113.     int cam_h = cs.height;
  114.     int x;
  115.     int y;
  116.     IplImage* recYellowThreshold = use_hsv ? GetThresholdedImage(snimok) : GetThresholdedImageRGB(snimok);
  117.     if(do_track)
  118.      {
  119.      int av_x = 0; int av_y = 0; int nwpix = 0;
  120.         for( int x=0; x < cam_w; x++)
  121.         {
  122.             for( int y= 0; y < cam_h; y++)
  123.             {
  124.             av_x += x; av_x += y;nwpix ++;
  125.             char * pixel = (recYellowThreshold->imageData +(x+y*cam_w));
  126.             if (pixel[0] > 0);
  127.             }
  128.  
  129.         }
  130.         av_y / nwpix;
  131.         av_x / nwpix;
  132.  
  133.         long screen_w = (LONG)::GetSystemMetrics( SM_CXFULLSCREEN );
  134.         long screen_h = (LONG)::GetSystemMetrics( SM_CYFULLSCREEN );
  135.         posX = av_x * screen_h/cam_h;
  136.         posY = av_y * screen_w/cam_w;
  137.         SetCursorPos(posX,posY);
  138.         }
  139.  
  140.        else
  141.         {
  142.            
  143.             if ( clicked_x != -1 )
  144.             {      
  145.                
  146.                 snimok->imageData[(clicked_x+clicked_y*cam_w)*3];
  147.                 snimok->imageData[(clicked_x+clicked_y*cam_w)*3 +1];
  148.                 char * pixel = (snimok->imageData +(clicked_x+clicked_y*cam_w)*3);  
  149.             }
  150.         }
  151.    
  152.         cvShowImage("thresh",recYellowThreshold);
  153.         cvShowImage("video",snimok);
  154.         int k = cvWaitKey(10);
  155.                 if ( k == 32) break;
  156.                 if ( k == '1' ) { use_hsv = true; printf("hsv\n"); }
  157.                 if ( k == '2' ) { use_hsv = false; printf("bgr\n"); }
  158.                 if ( k == '3' ) { do_track = true;printf("track on\n"); }
  159.                 if ( k == '4' ) { do_track = false; printf("track off\n"); }
  160.  }
  161.         cvReleaseCapture( &zaznam );
  162.         cvDestroyWindow ("Moje Okno");
  163.         return 0;
  164.  
  165.  }
Add Comment
Please, Sign In to add comment