Share Pastebin
Guest
Public paste!

CABELO

By: a guest | Jan 29th, 2010 | Syntax: C | Size: 4.99 KB | Hits: 183 | Expires: Never
Copy text to clipboard
  1. #include "cv.h"
  2. #include "highgui.h"
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <stdlib.h>
  6.  
  7. #include <X11/Xlib.h>
  8. #include <X11/extensions/XTest.h>
  9.  
  10. #define WIDTH  640
  11. #define HEIGHT 480
  12.  
  13. #define SEARCHRANGE 100
  14.  
  15. #define STATE_UP   0
  16. #define STATE_DOWN 1
  17.  
  18. #define CHANGE_NONE 0
  19. #define CHANGE_SHRINKING 1
  20. #define CHANGE_GROWING 2
  21.  
  22. #define CHANGEFACTOR .75
  23.  
  24.  
  25. int searchx = -1;
  26. int searchy = -1;
  27.  
  28. int startw = 100;
  29. int starth = 100;
  30.  
  31. int lasth = 100;
  32. int lastw = 100;
  33.  
  34. int shapechange = CHANGE_NONE;
  35.  
  36. CvPoint topleft;
  37. CvPoint bottomright;
  38.  
  39. int state;
  40.  
  41. template<class T> class Image {
  42.  
  43.         private:
  44.         IplImage *imgp;
  45.  
  46.         public:
  47.         Image (IplImage *img=0) {imgp = img;}
  48.         ~Image () {imgp = 0;}
  49.         void operator=(IplImage *img) {imgp=img;}
  50.         inline T* operator[] (const int rowIndex) {
  51.                 return ((T*)(imgp->imageData + rowIndex*imgp->widthStep));
  52.         }
  53. };
  54.  
  55. typedef struct {
  56.         unsigned char b,g,r;
  57. } RgbPixel;
  58. typedef struct {
  59.         float b,g,r;
  60. } RgbPixelFloat;
  61.  
  62. typedef Image<RgbPixel> RgbImage;
  63.  
  64. void on_mouse( int event, int x, int y, int flags, void* param )
  65. {
  66. }
  67.  
  68. void paint_red (IplImage *frame, Display *dpy) {
  69.  
  70.         int xmin, xmax, ymin, ymax;
  71.         int top = HEIGHT, bottom = -1, left = WIDTH, right = -1;
  72.         if (searchx == -1 || searchy == -1) {
  73.  
  74.                xmin = 0;
  75.                ymin = 0;
  76.                xmax = WIDTH;
  77.                ymax = HEIGHT;
  78.  
  79.         } else {
  80.  
  81.                 xmin = MAX (0, searchx - SEARCHRANGE);
  82.                 xmax = MIN (WIDTH, searchx + SEARCHRANGE);
  83.                 ymin = MAX (0, searchy - SEARCHRANGE);
  84.                 ymax = MIN (HEIGHT, searchy + SEARCHRANGE);
  85.  
  86.         }
  87.  
  88.         RgbImage image (frame);
  89.  
  90.         int redxt = 0, redyt = 0;
  91.         int count = 0;
  92.  
  93.         for (int i = ymin; i < ymax; i++) {
  94.         for (int j = xmin; j < xmax; j++) {
  95.                 int r = image[i][j].r;
  96.                 int g = image[i][j].g;
  97.                 int b = image[i][j].b;
  98.                 if (g < 0x20 && b < 0x20 && r > 0x80) {
  99.                         image[i][j].r=0xFF;
  100.                         image[i][j].g=0x00;
  101.                         image[i][j].b=0x00;
  102.                         redyt += i;
  103.                         redxt += j;
  104.                         count++;
  105.                         top = MIN (top, i);
  106.                         bottom = MAX (bottom, i);
  107.                         left = MIN (left, j);
  108.                         right = MAX (right, j);
  109.                 }
  110.  
  111.         }}
  112.  
  113.         if (count) {
  114.  
  115.                 searchx = redxt / count;
  116.                 searchy = redyt / count;
  117.  
  118.                 float redx = (float) redxt / count / WIDTH;
  119.                 float redy = (float) redyt / count / HEIGHT;
  120.  
  121.                 int screenx = (int)(redx * 1480.0 - 100);
  122.                 int screeny = (int)(redy * 1000.0 - 100);
  123.  
  124.                 XTestFakeMotionEvent (dpy, DefaultScreen (dpy), screenx, screeny, 0);
  125.  
  126.                 //boundingbox = cvRect (top, bottom, left, right);
  127.                 topleft = cvPoint (left, top);
  128.                 bottomright = cvPoint (right, bottom);
  129.  
  130.  
  131.                 int thish = bottom - top;
  132.                 int thisw = right - left;
  133.  
  134.                 if (shapechange && abs(lasth - thish) < lasth * 1 - CHANGEFACTOR) {
  135.  
  136.                         if (shapechange == CHANGE_GROWING)
  137.                                 state = STATE_UP;
  138.                         else
  139.                                 state = STATE_DOWN;
  140.  
  141.                 }
  142.  
  143.                 if (thish < lasth * CHANGEFACTOR) shapechange = CHANGE_SHRINKING;
  144.                 else if (thish * CHANGEFACTOR > lasth) shapechange = CHANGE_GROWING;
  145.                 else shapechange = CHANGE_NONE;
  146.  
  147.                 lasth = thish;
  148.                 lastw = thisw;
  149.  
  150.                 XFlush (dpy);
  151.  
  152.         } else {
  153.                 searchx = -1;
  154.                 searchy = -1;
  155.         }
  156.  
  157. }
  158.  
  159. int main( int argc, char** argv )
  160. {
  161.  
  162.     Display *dpy = XOpenDisplay (0);
  163.  
  164.     CvCapture* capture = cvCaptureFromCAM(0);
  165.     IplImage *frame = 0;
  166.    
  167.     cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, WIDTH);
  168.     cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, HEIGHT);
  169.  
  170.     if( !capture )
  171.     {
  172.         fprintf(stderr,"Could not initialize capturing...\n");
  173.         return -1;
  174.     }
  175.  
  176.     cvNamedWindow( "CamShiftDemo", 1 );
  177.     cvSetMouseCallback( "CamShiftDemo", on_mouse, 0 );
  178.    
  179.     frame = cvQueryFrame( capture );
  180.     int step = frame->widthStep / sizeof (uchar);
  181.  
  182.     for(;;)
  183.     {
  184.  
  185.         frame = cvQueryFrame( capture );
  186.         if( !frame )
  187.             break;
  188.  
  189.         cvFlip (frame, 0, 1);
  190.         paint_red (frame, dpy);
  191.         cvRectangle (frame, topleft, bottomright, cvScalar(255,255,255), 3-4*state, 8, 0);
  192.         cvShowImage ("CamShiftDemo", frame);
  193.         char c = cvWaitKey(10);
  194.  
  195.     }
  196.  
  197.     cvReleaseCapture( &capture );
  198.     cvDestroyWindow("CamShiftDemo");
  199.  
  200.     return 0;
  201. }