Guest User

Untitled

a guest
May 22nd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.23 KB | None | 0 0
  1.  
  2.  
  3. #include "StdAfx.h"
  4. #include "cv.h"
  5. #include "highgui.h"
  6. #include <stdio.h>  
  7. #include <Windows.h>
  8. #include <iostream>
  9.  
  10. using namespace std;
  11. using namespace cv;
  12. static int posX = 0;
  13. static int posY = 0;
  14.  
  15.  
  16. int lastX = posX;
  17. int lastY = posY;
  18.  
  19.  
  20. uchar*    ptr = NULL;
  21.  
  22.  
  23. CvScalar lorange = cvScalar(20,100,100);
  24.  
  25. CvScalar hirange = cvScalar(30,255,255);
  26.  
  27.  
  28.  
  29.  
  30. bool do_track = false;
  31. bool use_hsv = true;
  32.  
  33. int clicked_x = -1;
  34. int clicked_y = -1;
  35. void mousehandler( int event, int x, int y, int flags, void* param )
  36. {
  37.     clicked_x = -1;
  38.     if( event == CV_EVENT_LBUTTONDOWN )
  39.     {
  40.         clicked_x = x;
  41.         clicked_y = y;
  42.                 printf("clicked %d %d\n", x,y);
  43.     }
  44. }
  45.  
  46.  
  47. IplImage* GetThresholdedImage(IplImage* rec)
  48. {
  49.        
  50.         IplImage* GetThresholdedImage(IplImage* rec);
  51.         IplImage* recHSV = cvCreateImage(cvGetSize(rec), 8, 3);
  52.         cvCvtColor(rec, recHSV, CV_BGR2HSV);
  53.         IplImage* recThreshed = cvCreateImage(cvGetSize(rec), 8, 1);
  54.         cvInRangeS(recHSV,cvScalar(20,100,100),cvScalar(30,255,255), recThreshed);
  55.         cvReleaseImage(&recHSV);
  56.         return recThreshed;
  57. }
  58.  
  59. IplImage* GetThresholdedImageRGB(IplImage* rec)
  60. {
  61.         IplImage* recThreshed = cvCreateImage(cvGetSize(rec), 8, 1);
  62.         cvInRangeS(rec,cvScalar(20,100,100),cvScalar(30,255,255), recThreshed);
  63.         return recThreshed;
  64. }
  65.  
  66.  
  67. void GetPrimaryScreenSizeInPixels( SIZE& s)
  68. {
  69. ZeroMemory( &s, sizeof(SIZE) );
  70. s.cx = (LONG)::GetSystemMetrics( SM_CXFULLSCREEN );
  71. s.cy = (LONG)::GetSystemMetrics( SM_CYFULLSCREEN );
  72. }
  73.  
  74. int clamp(int v)
  75. {
  76.     if (v < 0 )
  77.         return 0;
  78.     if (v > 255)
  79.         return 255;
  80.  
  81.     return v;
  82. }
  83. CvMemStorage* storage = cvCreateMemStorage(0);
  84.  CvSeq* cont = cvCreateSeq(CV_SEQ_ELTYPE_POINT, sizeof(CvSeq), sizeof(CvPoint) , storage);
  85.  int main()
  86.  {
  87.  CvCapture* zaznam = cvCaptureFromCAM( CV_CAP_ANY); //zaznamenavanie z kamery
  88.  if ( !zaznam )
  89.  {
  90.      fprintf( stderr, "Chyba! zaznam je bez hodnoty \n ");
  91.      getchar();
  92.      return -1;
  93.  }
  94.  
  95.  
  96.  cvNamedWindow("video",CV_WINDOW_AUTOSIZE);
  97.  cvNamedWindow("thresh",CV_WINDOW_AUTOSIZE);
  98.  IplImage* recScribble = NULL;
  99.  IplImage* snimok = cvQueryFrame(zaznam);
  100.  cvSetMouseCallback("video", mousehandler );
  101.  
  102.  while (true)
  103.  {
  104.    
  105.     snimok = cvQueryFrame(zaznam);
  106.         if (!snimok)
  107.          {
  108.              fprintf( stderr, "Chyba! snimok je bez hodnoty");
  109.              getchar();
  110.              break;
  111.          }
  112.        
  113.     cvFlip(snimok,snimok,1);
  114.     CvSize cs = cvGetSize(snimok);
  115.     int cam_w = cs.width;
  116.     int cam_h = cs.height;
  117.    
  118.     IplImage* recYellowThreshold = use_hsv ? GetThresholdedImage(snimok) : GetThresholdedImageRGB(snimok);
  119.     if(do_track)
  120.      {
  121.         cvFindContours(snimok,storage,&cont,sizeof(CvContour), CV_RETR_EXTERNAL, 2, cvPoint(0,0));
  122.         CvRect cvBoundingRect( CvArr* contour, int update );
  123.         CvRect r = cvBoundingRect(cont, 0 );
  124.         CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments));
  125.         cvMoments(recYellowThreshold, moments, 1);
  126.         double moment10 = cvGetSpatialMoment(moments, 1, 0);
  127.         double moment01 = cvGetSpatialMoment(moments, 0, 1);
  128.         double area = cvGetCentralMoment(moments, 0, 0);
  129.         posX = moment10/area;
  130.         posY = moment01/area;
  131.         printf("position (%d,%d)\n", posX, posY);  
  132.    
  133.         long screen_w = (LONG)::GetSystemMetrics( SM_CXFULLSCREEN );
  134.         long screen_h = (LONG)::GetSystemMetrics( SM_CYFULLSCREEN );
  135.  
  136.         int x = posX * screen_w/cam_w;
  137.         int y = posY * screen_h/cam_h;
  138.         SetCursorPos(x,y);
  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