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 & HelpSyntax Questions › New programmer: keeping images on screen
Page Index Toggle Pages: 1
New programmer: keeping images on screen (Read 772 times)
New programmer: keeping images on screen
Apr 12th, 2009, 7:43am
 
Hey all

I'm new to Processing but am doing a uni project which needs the image on screen to change depending on the position of a tracking ball which tracks an infrared light captured by my webcam. So far I've been able to get the image to change as the light moves, but I need the previous image to remain on the screen until the new one comes up - at the moment there's a lot of flashing images!

Also, my code is made up of edited code from different sources, and I'm not sure exactly what some of the phrases mean so if anyone can 'translate' for me that would be great! (I've highlighted the ones I mean)

Here's the code so far...


//Import the video library:
import processing.video.*;

Capture myCapture;  // copy of the video library
float worldRecord = 2000.0;  // initialise the worldrecord
int xFound = width/2;   // initialise the location of the tracking ball
int yFound = height/2;
boolean goodTrack = false;  // whether or not we have something in the image
// that's close enough to our tracked colour
color targetColor = color(255,255,255);  // the target colour: white

void setup()
{
 // set window size:
 size(1260,740);
 frameRate(200);

 myCapture = new Capture(this, width, height, 300);
}

//this gets called whenever there's a new frame of video available:

void captureEvent(Capture myCapture)
{
 // read the myCamera and update the pixel array:
 myCapture.read();
 // we don't have a pixel that matches our colour yet:
 goodTrack = false;

 // scan all the pixels looking for a match:
 for(int row=0; row<height; row++)  {
   for(int column=0; column<width; column++)  {  //for each column
     // get the colour of this pixel
     // find pixel in linear array using formula: pos = row*rowWidth+column:
     color pixelColor = myCapture.pixels[row*width+column];
     // calculate the difference between the pixel's color
     // and our desired color:
     float diff = abs(red(targetColor) - red(pixelColor)) +
       abs(green(targetColor) - green(pixelColor)) +
       abs(blue(targetColor) - blue(pixelColor))/3;

     if (diff<= worldRecord){ // if this is closest to our target color
       worldRecord = diff;
       yFound = row; // mark the spot for drawing it later
       xFound = column;
       goodTrack = true;
     }
   }
 }
}

void draw()
{
 PImage b;
 // draw my camera image on the screen:
 image(myCapture, 0, 0);
 // if we get a good color match, draw a dot there:
 if (goodTrack) {
   // fill(targetColor);
   // ellipse(xFound, yFound, 15, 15);
   if (xFound<30) {
     b = loadImage("Image1.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=30)&&(xFound<60)) {
     b = loadImage("Image2.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=60)&&(xFound<90)) {
     b = loadImage("Image3.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=90)&&(xFound<120)) {
     b = loadImage("Image4.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=120)&&(xFound<150)) {
     b = loadImage("Image5.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=150)&&(xFound<180)) {
     b = loadImage("Image6.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=180)&&(xFound<210)) {
     b = loadImage("Image7.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=210)&&(xFound<240)) {
     b = loadImage("Image8.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=240)&&(xFound<270)) {
     b = loadImage("Image9.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=270)&&(xFound<300)) {
     b = loadImage("Image10.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=300)&&(xFound<330)) {
     b = loadImage("Image11.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=330)&&(xFound<360)) {
     b = loadImage("Image12.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=360)&&(xFound<390)) {
     b = loadImage("Image13.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=390)&&(xFound<420)) {
     b = loadImage("Image14.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=420)&&(xFound<450)) {
     b = loadImage("Image15.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=450)&&(xFound<480)) {
     b = loadImage("Image16.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=480)&&(xFound<510)) {
     b = loadImage("Image17.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=510)&&(xFound<540)) {
     b = loadImage("Image18.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=540)&&(xFound<570)) {
     b = loadImage("Image19.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=570)&&(xFound<600)) {
     b = loadImage("Image20.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=600)&&(xFound<630)) {
     b = loadImage("Image21.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=630)&&(xFound<660)) {
     b = loadImage("Image22.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=660)&&(xFound<690)) {
     b = loadImage("Image23.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=690)&&(xFound<720)) {
     b = loadImage("Image24.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=720)&&(xFound<750)) {
     b = loadImage("Image25.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=750)&&(xFound<780)) {
     b = loadImage("Image26.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=780)&&(xFound<810)) {
     b = loadImage("Image27.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=810)&&(xFound<840)) {
     b = loadImage("Image28.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=840)&&(xFound<870)) {
     b = loadImage("Image29.jpg");
     image(b, 0, 0);
   }
   if ((xFound>=870)&&(xFound<900)) {
     b = loadImage("Image30.jpg");
     image(b, 0, 0);
   }
 }
}

Any help would be greatly appreciated  Smiley

Thanks

Jo
Re: New programmer: keeping images on screen
Reply #1 - Apr 13th, 2009, 2:49am
 
Just a quick advice: pre-load all images in an array. Lengthly operations like loadImage, involving disk reading, should be avoided in draw().
Also you repeat image(b, 0, 0): you can put only one at the end of the tests.
If you declare PImage b outside of draw(), its value will persist after draw(), to the next one, so it will remain on screen.
And the lengthly series of tests can certainly be replaced by a loop.
Re: New programmer: keeping images on screen
Reply #2 - Apr 13th, 2009, 7:31am
 
the loop should be something like this:

for (i=30; i<900; i+= 30){
 if ((xFound>= i )&&(xFound< i+30 )) {
   //
 }
}
Re: New programmer: keeping images on screen
Reply #3 - May 4th, 2009, 6:40am
 
thanks for the help, very much appreciated Smiley
Re: New programmer: keeping images on screen
Reply #4 - May 4th, 2009, 1:18pm
 
hey again

i tried the advice you gave me, with some extra code i found about arrays, but keep getting error messages

here's the new code:

Code:
//Import the video library:
import processing.video.*;

Capture myCapture; // copy of the video library
float worldRecord = 2000.0; // initialise the worldrecord
int xFound = width/2; // initialise the location of the tracking ball
int yFound = height/2;
boolean goodTrack = false; // whether or not we have something in the image
// that's close enough to our tracked colour
color targetColor = color(0,0,255); // the target colour: white

void setup()
{
// set window size:
size(1260,740);
frameRate(200);

myCapture = new Capture(this, width, height, 300);
}

//this gets called whenever there's a new frame of video available:

void captureEvent(Capture myCapture)
{
// read the myCamera and update the pixel array:
myCapture.read();
// we don't have a pixel that matches our colour yet:
goodTrack = false;

// scan all the pixels looking for a match:
for(int row=0; row<height; row++) {
for(int column=0; column<width; column++) { //for each column
// get the colour of this pixel
// find pixel in linear array using formula: pos = row*rowWidth+column:
color pixelColor = myCapture.pixels[row*width+column];
// calculate the difference between the pixel's color
// and our desired color:
float diff = abs(red(targetColor) - red(pixelColor)) +
abs(green(targetColor) - green(pixelColor)) +
abs(blue(targetColor) - blue(pixelColor))/3;

if (diff<= worldRecord){ // if this is closest to our target color
worldRecord = diff;
yFound = row; // mark the spot for drawing it later
xFound = column;
goodTrack = true;
}
}
}
}

PImage[] images = new PImage[30];
PImage b;
// draw my camera image on the screen:

void draw()
{
image(myCapture, 0, 0);
// if we get a good color match, draw a dot there:
if (goodTrack) {
// fill(targetColor);
// ellipse(xFound, yFound, 15, 15);
for (int i=30; i<900; i+= 30){
if ((xFound>=i )&&(xFound< i+30 )) {
images[i] = loadImage("Image" + i + ".jpg");
image(b, 0, 0);
}
}
}
}


the error message i get with that is "ArrayIndexOutOfBoundsException: 330"

I renamed my images "Image0, Image 30, Image 60" etc as I think that's what's needed, although I'm not sure if that's right.

I probably have put the code in wrong but any help would be great.

Thanks

Jo
Re: New programmer: keeping images on screen
Reply #5 - May 4th, 2009, 3:55pm
 
You've declared your array to have a length of 30.  That means its indices are at 0,1,2,3,4,...,29.  The value of i in your loop however is 30,60,90,... which is why you're getting that particular error:

Code:
for (int i=30; i<900; i+= 30){
if ((xFound>=i )&&(xFound< i+30 )) {
 images[i] = loadImage("Image" + i + ".jpg");
 image(b, 0, 0);
}
   }


So...  your loop should go from 0 to 29 so that i is a valid index in your array.  You'll just need to create a local variable to handle the value used in the condition and image reference.  i.e.:

Code:
for (int i=0; i<30; i++){
 // take the value of i and make appropriate adjustments ;)
 int myLimit = (i+1)*30;

if ((xFound>=myLimit  )&&(xFound< myLimit +30 )) {
 images[i] = loadImage("Image" + myLimit  + ".jpg");
 image(b, 0, 0);
}
   }


Can't promise the rest of your code will work...  In fact if I were you I'd be tempted to start from scratch, creating individual sketches for each bit of functionality so you can get your head round how it works; then incorporate it into a final sketch.
Re: New programmer: keeping images on screen
Reply #6 - May 14th, 2009, 8:32am
 
ok i'll have a go at both those options! thanks
Page Index Toggle Pages: 1