We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › scanning center of image
Page Index Toggle Pages: 1
scanning center of image (Read 562 times)
scanning center of image
Apr 20th, 2009, 1:44pm
 
Hello all,

I am working on a color tracking program, which is fully functional, but now I need to only scan a middle strip of the image. I want to essentially have the program look at and recognize a horizontal strip of pixels of a given width (lets say 10) centered at y=height/2.

How?

Below is the working code that scans the entire image.

/**
* Color Tracking
* by Justin Brooks
*
*
* based on original code:
*   Brightness Tracking
*   by Golan Levin.
*
* Tracks the tracks pixel closest in color to specified color.
*/


import processing.video.*;

Capture video;

void setup() {
 size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
 // Uses the default video input, see the reference if this causes an error
 video = new Capture(this, width, height, 30);
 noStroke();
 smooth();


}

void draw() {

 if (video.available()) {
   video.read();
   image(video, 0, 0, width, height); // Draw the webcam video onto the screen
   int colorX = 0; // X-coordinate of the closest in color video pixel
   int colorY = 0; // Y-coordinate of the closest in color video pixel
   float closestColor = 10000; //we set this to be abritrarily large, once program runs, the first pixel it scans will be set to this value
   // Search for the closest in color pixel: For each row of pixels in the video image and
   // for each pixel in the yth row, compute each pixel's index in the video
   color colorRead=color(0,255,0); //this is the color we will be searching for
   video.loadPixels();
   int index = 0;



   for (int y = 0 ; y < height; y++) {
     for (int x = 0; x < video.width; x++) {
       // Get the color stored in the pixel
       color pixelValue = video.pixels[index];
       // Determine the color of the pixel
       float colorProximity = abs(red(pixelValue)-0)+abs(green(pixelValue)-255)+abs(blue(pixelValue)-0);
       // If that value is closer in color value than any previous, then store the
       // color proximity of that pixel, as well as its (x,y) location
       if (colorProximity < closestColor) {
         closestColor = colorProximity;
         colorY = y;
         colorX = x;
       }
       index++;
     }
   }

   //Draw the area in which we are scanning
   rectMode(CENTER);
   stroke(0);
   noFill();
   rect(width/2,height/2,50,50);

   // Draw a large, circle at the closest in value pixel
   fill(colorRead, 128); //it will fill the circle as the color you want.
   noStroke();
   ellipse(colorX, colorY, 50, 50);
 }
}
Re: scanning center of image
Reply #1 - Apr 20th, 2009, 9:28pm
 
to determine an area in the middle of the frame to be scanned [horizontal line of 10px] you should make your for iteration considering only those pixels;

if you declared for(int y = 0; y < height; y ++){} and then for(int x = 0; x < video.width; x++){} you should keep the second line unaltered while you define in the first iteration only the specific portions you want to be scanned.

in the case of 10 px let us suppose you declare a var named scanHeight to define how many pixels in the y axis should be scanned [this will scan 10px in y axis and video.width px in the x axis // totalizing (10 * video.width) pixels scanned each time for is called] it could be written:
for(int y = ((height/2) - (scanHeight/2)); y < ((height/2) + (scanHeight/2)); y++){}
this will mess up your index variable. to correct this you can make the index start in the right place instead of starting at 0; it could be done by declaring, before the for iteration, a value for the first index, which in this case would be: index = ((((height/2) - (scanHeight/2)) * width) - 1);

i hope this helps (:
Re: scanning center of image
Reply #2 - Apr 20th, 2009, 10:08pm
 
pedro,

thank you so much... i can now adjust the "thickness" of the scan.

your for() index was something that I had thought of previously, but would get the wrong locational values; presumably because the index was off.

could you please explain, or link to, why this throws off my initial index?

thanks again for your help!
Re: scanning center of image
Reply #3 - Apr 21st, 2009, 10:16am
 
your index variable is based on two for iterations; for the index variable value to correspond to the correct index of the pixels[] every line of the video has to be read. what i did with that for iteration was skip many lines, so your index variable would start at 0 while you are actually reading the line ((height/2) - (scanHeight/2)); the index value has to correspond to the line, so. this way you must declare this initial value for the variable corresponding to the first pixel you scan.

Code:
 for (int y = 0 ; y < height; y++) {
    for (int x = 0; x < video.width; x++) {
      color pixelValue = video.pixels[index];
      float colorProximity = abs(red(pixelValue)-0)+abs(green(pixelValue)-255)+abs(blue(pixelValue)-0);
      if (colorProximity < closestColor) {
        closestColor = colorProximity;
        colorY = y;
        colorX = x;
      }

      index++; // each time for(int x - ...) is read index += 1;
// this way your 'for' should iterate through all indexes to have
// the correct value for the pixels[]
// once on the new code it does not iterate through all pixels you
// have to define the initial value for index out of your 'for'
//structure

    }
  }


i hope i was clear enough (:
Re: scanning center of image
Reply #4 - Apr 21st, 2009, 10:22am
 
thanks again! feel free to check out the code below. i am much on my way to finishing it! the only feature left to add is the ability to set the color to be tracked by a click on a pixel with my mouse.

Code:

/**
* Color Tracking
* by Justin Brooks
*
*
* based on original code:
*   Brightness Tracking
*   by Golan Levin.
*
* Tracks the tracks pixel closest in color to specified color.
*/


import processing.video.*;

Capture video;

void setup() {
 size(640, 480); // Change size to 320 x 240 if too slow at 640 x 480
 // Uses the default video input, see the reference if this causes an error
 video = new Capture(this, width, height, 30);
 noStroke();
 smooth();



}

void draw() {

 if (video.available()) {
   video.read();
   image(video, 0, 0, width, height); // Draw the webcam video onto the screen
   int colorX = 0; // X-coordinate of the closest in color video pixel
   int colorY = 0; // Y-coordinate of the closest in color video pixel
   float closestColor = 10000; //we set this to be abritrarily large, once program runs, the first pixel it scans will be set to this value
   // Search for the closest in color pixel: For each row of pixels in the video image and
   // for each pixel in the yth row, compute each pixel's index in the video
   video.loadPixels();
   color colorRead=color(0,255,0); //this is the color we will be searching for
   int index = 0;
   colorMode(RGB);
   int scanHeight=10; //how wide would you like to scan?
   index = ((((height/2) - (scanHeight/2)) * width) - 1); //set this for first index value
   color Getcolor=get(mouseX,mouseY);


   //getting color by mouse click
   if (mousePressed){ //if mouse is pressed, we show a different color cross hair and perform color catching action
     video.read();
     color Get = get(mouseX,mouseY);
     rectMode(CENTER);
     stroke(Getcolor);
     noFill();
     rect(mouseX,mouseY,52,52);
   }


   for(int y = ((height/2) - (scanHeight/2)); y < ((height/2) + (scanHeight/2)); y++){
     for (int x = 0; x < video.width; x++) {
       // Get the color stored in the pixel
       color pixelValue = video.pixels[index];
       // Determine the color of the pixel
       float colorProximity = abs(red(pixelValue)-red(colorRead))+abs(green(pixelValue)-green(colorRead))+abs(blue(pixelValue)-blue(colorRead));
       // If that value is closer in color value than any previous, then store the
       // color proximity of that pixel, as well as its (x,y) location
       if (colorProximity < closestColor) {
         closestColor = colorProximity;
         colorY = y;
         colorX = x;
       }
       index++;
     }
   }


   // Draw a large, circle at the closest in value pixel
   fill(colorRead, 128); //it will fill the circle as the color you want.
   noStroke();
   ellipse(colorX, colorY, 50, 50);
   
   //print the location of the color found
   print("X: ");
   println(colorX);
   print("Y: "); //this doesn't really matter, but doesn't slow the program.
   println(colorY);

   //mousing setup - we will draw a crosshairs on where the mouse is.
   rectMode(CENTER);
   stroke(0);
   noFill();
   rect(mouseX,mouseY,50,50);
   rect(mouseX,mouseY,5,5);

   //draw a rectangle around the area we are scanning
   rectMode(CENTER);
   stroke(255,3,239); //bright pink
   noFill();
   rect(width/2,height/2,width,scanHeight);


   //draw a rectangle in the bottom corner to show what color is being picked up by the mouse location of video feed
   stroke(0);
   fill(Getcolor);
   rect(width-45,height-45, 90,90);
 }
}


Re: scanning center of image
Reply #5 - Apr 21st, 2009, 1:58pm
 
wonderful.
i am glad that helped.
regards, p
Page Index Toggle Pages: 1