Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import JMyron.*; //Import the video library
- JMyron webcam; //Declare a camera object
- int image_width=352, image_height=288;
- int centre_imagex=(image_width/2)-1, centre_imagey=(image_height/2)-1;
- static Frame w;
- int reference_image_width=40, reference_image_height=40;
- int centre_reference_imagex=(reference_image_width/2)-1, centre_reference_imagey=(reference_image_height/2)-1;
- int[] reference_image = new color[reference_image_width*reference_image_height]; // To store the reference image
- boolean tracking_mode=false;
- int search_area_width=540, search_area_height=440; // The neighbourhood pixel area to assess. Should be even numbers.
- // The bigger the search area, the slower the system.
- int mode=1;
- void setup(){
- // This function is executed once at start-up
- if (mode==1) { // Video mode
- webcam = new JMyron(); //Make a new instance of the video object
- webcam.start(image_width,image_height); //Start a capture using a particular resolution
- // JMyron will tell you actual resolution that the webcam uses
- image_width = webcam.getForcedWidth();
- image_height = webcam.getForcedHeight();
- webcam.stop(); // stop the webcam
- size(image_width, image_height); // set the picture size correctly
- webcam.start(image_width, image_height); // start it again using correct size
- //Disable the 'intelligence' supplied in the library.
- //This helps obtain the fastest frame rate possible
- webcam.findGlobs(0);
- loadPixels();
- // Calculate the centre of the webcam image
- centre_imagex=(image_width/2)-1;
- centre_imagey=(image_height/2)-1;
- }
- // println("(This program is using the Myron webcam library version:" + webcam.version() + ")");
- println(">> Using a resolution of " + image_width + " * " + image_height);
- println("");
- }
- void draw(){
- // This function is repeatedly executed
- // as many times each second as possible.
- int[] img; // To store the webcam image
- float total_difference=0; // Difference level for the current comparison
- float this_difference; // Difference level of the current comparison
- int lowest_offsetx=0, lowest_offsety=0; // Position of the best match
- float lowest_difference_so_far=999999999; // Records the lowest difference so far
- int reference_array_index, image_array_index;
- int array_index, neighbourhood_array_index, x, y,
- along, down, offsetx, offsety; // Used for indexing
- int x_position, y_position; // Used for temporary calculation of neighbourhood pixel position
- int start_offsetx, start_offsety; // Top left hand corner of the search area
- img = webcam.image(); // Get the webcam image
- webcam.update(); // Update the webcam image
- loadPixels(); // Need to do this before writing to pixels() array
- // Quick loop to copy the webcam image into the output pixels
- for (y=0; y< height; y++) {
- for (x=0; x< width; x++) {
- image_array_index=(y * width) + x;
- pixels[image_array_index] = img[image_array_index]; // Copy webcam pixels into output array
- }
- }
- if (tracking_mode) { // if we are in tracking mode then show reference image in top left corner
- lowest_difference_so_far=999999999; // Records the lowest difference so far
- for (y=0; y< reference_image_height; y++) {
- for (x=0; x< reference_image_width; x++) {
- reference_array_index=(y * reference_image_width) + x;
- image_array_index=(y * width) + x;
- pixels[image_array_index] = reference_image[reference_array_index]; // Copy webcam pixels into output array
- }
- }
- }
- //print(" centre x y = " + centre_imagex + centre_imagey );
- updatePixels(); // Update the display window with the current data from pixels[]
- if (tracking_mode) {
- // Calculate top left hand corner of the search area
- start_offsetx= -1 * (search_area_width/ 2);
- start_offsety= -1 * (search_area_height/ 2);
- // Go through the range of x and y offsets, search for best match
- for (offsety=start_offsety; offsety < start_offsety + search_area_height; offsety += 2) {
- for (offsetx=start_offsetx; offsetx < start_offsetx + search_area_width; offsetx += 2) {
- total_difference=0; // Reset
- // Now compare the reference image with the offset section
- // of the search area.
- for (down=0; down < reference_image_height; down += 3) {
- for (along=0; along < reference_image_width; along += 3) {
- reference_array_index=(down * reference_image_width) + along;
- image_array_index=( (centre_imagey + down + offsety - centre_reference_imagey) * width)
- + ( centre_imagex + along + offsetx - centre_reference_imagex);
- this_difference= abs( red(img[image_array_index]) - red(reference_image[reference_array_index]) )
- + abs( green(img[image_array_index]) - green(reference_image[reference_array_index]) )
- + abs( blue(img[image_array_index]) - blue(reference_image[reference_array_index]) );
- total_difference+= this_difference;
- }
- }
- // Compare this difference with 'best so far' difference
- if (total_difference < lowest_difference_so_far) {
- // Record position of best match so far
- lowest_difference_so_far=total_difference;
- lowest_offsetx=offsetx; // Store vector x
- lowest_offsety=offsety; // Store vector y
- }
- }
- }
- // println(" Best match: vector x, y, lowest = " + lowest_offsetx + " " + lowest_offsety + " " + lowest_difference_so_far);
- stroke( 255, 0, 0 ); // Set line colour to red
- fill(0,0,0);
- rect( centre_imagex - (reference_image_width/2) + lowest_offsetx,
- centre_imagey - (reference_image_height/2) + lowest_offsety,
- reference_image_width*3, reference_image_height*1.5 );
- }
- //----------------------------------------------------------------
- // Draw a blue rectangle to show the search area
- //----------------------------------------------------------------
- stroke(0, 0, 255); // Set line colour to blue
- noFill(); // Don't fill the rectangle
- rect( centre_imagex - search_area_width/2, centre_imagey - search_area_height/2,
- search_area_width, search_area_height); // Draw rectangle
- }
- void mousePressed(){
- // If the user clicks on the any part of the frame this function is executed.
- // In this function, the part of the frame which is clicked on
- // is copied into array reference_image[] for subsequent tracking.
- // The mouse click location indicates the centre of the reference image area.
- int xstart, ystart, x, y, count=0;
- int reference_image_index;
- int[] img;
- img = webcam.image(); // Get the webcam image
- webcam.update(); // Update the webcam image
- // User clicks on (mouseX, mouseY)
- xstart=mouseX- (reference_image_width/2);
- ystart=mouseY- (reference_image_width/2);
- //println(" ok - xstart = " + xstart + " ystart = " + ystart );
- count=0;
- for (y=ystart; y< ystart+reference_image_height; y++) {
- for (x=xstart; x< xstart+reference_image_width; x++) {
- reference_image_index=(y * width) + x; // Calculate position
- reference_image[count]=img[reference_image_index];
- count++;
- }
- }
- tracking_mode=true;
- println(" Reference image captured - tracking mode is on" );
- }
- public void stop(){
- // This function is executed when the program stops.
- webcam.stop(); //Stop the webcam object
- super.stop();
- }
Advertisement
Add Comment
Please, Sign In to add comment