Guest User

Simple object tracking in Processing

a guest
May 20th, 2013
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.82 KB | None | 0 0
  1. import JMyron.*; //Import the video library
  2. JMyron webcam;   //Declare a camera object
  3.  
  4. int image_width=352, image_height=288;
  5. int centre_imagex=(image_width/2)-1, centre_imagey=(image_height/2)-1;
  6.  
  7. static Frame w;
  8.  
  9. int reference_image_width=40, reference_image_height=40;
  10. int centre_reference_imagex=(reference_image_width/2)-1, centre_reference_imagey=(reference_image_height/2)-1;
  11. int[] reference_image = new color[reference_image_width*reference_image_height];  // To store the reference image
  12. boolean  tracking_mode=false;
  13.  
  14. int search_area_width=540, search_area_height=440;  // The neighbourhood pixel area to assess. Should be even numbers.
  15.            // The bigger the search area, the slower the system.
  16.  
  17. int mode=1;
  18.  
  19. void setup(){
  20.   // This function is executed once at start-up
  21.  
  22.   if (mode==1) {  // Video mode
  23.     webcam = new JMyron();  //Make a new instance of the video object
  24.     webcam.start(image_width,image_height);  //Start a capture using a particular resolution
  25.  
  26.     // JMyron will tell you actual resolution that the webcam uses
  27.     image_width = webcam.getForcedWidth();
  28.     image_height = webcam.getForcedHeight();
  29.  
  30.     webcam.stop(); // stop the webcam
  31.     size(image_width, image_height); // set the picture size correctly
  32.     webcam.start(image_width, image_height); // start it again using correct size
  33.    
  34.     //Disable the 'intelligence' supplied in the library.
  35.     //This helps obtain the fastest frame rate possible
  36.     webcam.findGlobs(0);  
  37.      
  38.     loadPixels();
  39.  
  40.  
  41. // Calculate the centre of the webcam image
  42.  
  43.     centre_imagex=(image_width/2)-1;
  44.     centre_imagey=(image_height/2)-1;
  45.   }
  46.  
  47. //    println("(This program is using the Myron webcam library version:" + webcam.version() + ")");
  48. println(">> Using a resolution of " + image_width + " * " + image_height);
  49.  
  50. println("");
  51. }
  52.  
  53.  
  54. void draw(){
  55.   // This function is repeatedly executed
  56.   //  as many times each second as possible.
  57.  
  58.   int[] img;                          // To store the webcam image
  59.  
  60.   float total_difference=0;           // Difference level for the current comparison
  61.   float this_difference;              // Difference level of the current comparison
  62.   int lowest_offsetx=0, lowest_offsety=0; // Position of the best match
  63.   float lowest_difference_so_far=999999999;     // Records the lowest difference so far
  64.  
  65.   int reference_array_index, image_array_index;
  66.   int array_index, neighbourhood_array_index, x, y,
  67.       along, down, offsetx, offsety;  // Used for indexing
  68.   int x_position, y_position;         // Used for temporary calculation of neighbourhood pixel position
  69.  
  70.   int start_offsetx, start_offsety; // Top left hand corner of the search area
  71.  
  72.   img = webcam.image();         // Get the webcam image
  73.   webcam.update();            // Update the webcam image
  74.  
  75.   loadPixels();               // Need to do this before writing to pixels() array
  76.  
  77.   // Quick loop to copy the webcam image into the output pixels
  78.   for (y=0; y< height; y++) {
  79.     for (x=0; x< width; x++)  {
  80.       image_array_index=(y * width) + x;
  81.       pixels[image_array_index] = img[image_array_index]; // Copy webcam pixels into output array
  82.     }
  83.   }
  84.  
  85.   if (tracking_mode) { // if we are in tracking mode then show reference image in top left corner
  86.     lowest_difference_so_far=999999999;     // Records the lowest difference so far
  87.  
  88.     for (y=0; y< reference_image_height; y++) {
  89.       for (x=0; x< reference_image_width; x++)  {
  90.         reference_array_index=(y * reference_image_width) + x;
  91.         image_array_index=(y * width) + x;
  92.         pixels[image_array_index] = reference_image[reference_array_index]; // Copy webcam pixels into output array
  93.       }
  94.     }
  95.   }
  96.  
  97.   //print(" centre x y = " + centre_imagex + centre_imagey );
  98.  
  99.   updatePixels();      // Update the display window with the current data from pixels[]
  100.  
  101.   if (tracking_mode) {
  102.    
  103.     // Calculate top left hand corner of the search area
  104.     start_offsetx= -1 * (search_area_width/ 2);
  105.     start_offsety= -1 * (search_area_height/ 2);
  106.  
  107.     // Go through the range of x and y offsets, search for best match
  108.     for (offsety=start_offsety; offsety < start_offsety + search_area_height; offsety += 2) {
  109.       for (offsetx=start_offsetx; offsetx < start_offsetx + search_area_width; offsetx += 2)  {
  110.  
  111.         total_difference=0; // Reset
  112.      
  113.         // Now compare the reference image with the offset section
  114.         // of the search area.
  115.         for (down=0; down < reference_image_height; down += 3) {
  116.           for (along=0; along < reference_image_width; along += 3) {
  117.            
  118.             reference_array_index=(down * reference_image_width) + along;
  119.             image_array_index=( (centre_imagey + down + offsety - centre_reference_imagey) * width)
  120.                                   + ( centre_imagex + along + offsetx - centre_reference_imagex);
  121.            
  122.             this_difference=    abs( red(img[image_array_index]) - red(reference_image[reference_array_index]) )
  123.                               + abs( green(img[image_array_index]) - green(reference_image[reference_array_index]) )
  124.                               + abs( blue(img[image_array_index]) - blue(reference_image[reference_array_index]) );
  125.                              
  126.             total_difference+= this_difference;
  127.           }
  128.         }
  129.        
  130.         // Compare this difference with 'best so far' difference
  131.         if (total_difference < lowest_difference_so_far) {
  132.           // Record position of best match so far
  133.           lowest_difference_so_far=total_difference;
  134.           lowest_offsetx=offsetx; // Store vector x
  135.           lowest_offsety=offsety; // Store vector y
  136.         }
  137.  
  138.       }
  139.     }
  140.  
  141.    // println("  Best match: vector x, y, lowest = " + lowest_offsetx + " " + lowest_offsety + " " + lowest_difference_so_far);
  142.  
  143.     stroke( 255, 0, 0 );     // Set line colour to red
  144.     fill(0,0,0);
  145.     rect(     centre_imagex - (reference_image_width/2)  + lowest_offsetx,
  146.               centre_imagey - (reference_image_height/2)  + lowest_offsety,
  147.               reference_image_width*3, reference_image_height*1.5   );  
  148.   }
  149.  
  150.   //----------------------------------------------------------------
  151.   // Draw a blue rectangle to show the search area
  152.   //----------------------------------------------------------------
  153.   stroke(0, 0, 255);   // Set line colour to blue
  154.   noFill();            // Don't fill the rectangle
  155.   rect( centre_imagex -  search_area_width/2, centre_imagey -  search_area_height/2,
  156.            search_area_width,  search_area_height);  // Draw rectangle          
  157.  }
  158.  
  159. void mousePressed(){
  160.   // If the user clicks on the any part of the frame this function is executed.
  161.   // In this function, the part of the frame which is clicked on
  162.   // is copied into array reference_image[] for subsequent tracking.
  163.   // The mouse click location indicates the centre of the reference image area.
  164.  
  165.   int xstart, ystart, x, y, count=0;
  166.   int reference_image_index;
  167.  
  168.   int[]  img;
  169.  
  170.   img = webcam.image();         // Get the webcam image
  171.   webcam.update();            // Update the webcam image
  172.  
  173.  // User clicks on (mouseX, mouseY)
  174.   xstart=mouseX- (reference_image_width/2);
  175.   ystart=mouseY- (reference_image_width/2);
  176.  
  177.   //println(" ok - xstart = " + xstart + " ystart = " + ystart );
  178.    
  179.   count=0;
  180.  
  181.   for (y=ystart; y< ystart+reference_image_height; y++) {
  182.     for (x=xstart; x< xstart+reference_image_width; x++)  {
  183.       reference_image_index=(y * width) + x; // Calculate position
  184.       reference_image[count]=img[reference_image_index];
  185.      
  186.       count++;
  187.     }
  188.   }  
  189.   tracking_mode=true;
  190.   println(" Reference image captured - tracking mode is on" );
  191. }  
  192.  
  193. public void stop(){
  194.   // This function is executed when the program stops.
  195.  
  196.   webcam.stop();  //Stop the webcam object
  197.   super.stop();
  198. }
Advertisement
Add Comment
Please, Sign In to add comment