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 › x,y coordinates from a specific color
Page Index Toggle Pages: 1
x,y coordinates from a specific color? (Read 972 times)
x,y coordinates from a specific color?
Apr 20th, 2010, 11:57am
 
Hi, I am new to Processing, so please be easy on me :)

I don't even know if it's possible but I would like my Class to start at the pixel that is the closest to cyan (0,255,255) of my background image.
Is there a way I can do that?
I assume, I have to replace the x,y of my class starting point definition - How could this look like?
Thanks a lot!

This is where I'm at so far:

DLA[] dla;
int amount=1;
PImage img;

void setup() {
 size(1700,900,JAVA2D);
 img = loadImage("backgroundimage.jpg");
 background(img);
 loadPixels();

dla=new DLA[amount];
 
for(int i=0;i<amount;i++){
 dla[i]=new DLA(x,y,round(random(1,10)),round(random(1,10)),round(random(1,10)));
//I would like to insert the x,y of the cyan pixel here....
}
}

void draw() {
for(int i=0;i<amount;i++){  
 dla[i].aggregate();        
}
}

class DLA{
float[] pX, pY;

float startRadius = 10;
int maxP = 1000;
int pCount = 3;
float pSize = 6;
int stickDist = 9;  
int sx,sy;
float weight;  

DLA(int _sx, int _sy, int pCount, float pSize, int stickDist){
 sx=_sx;
 sy=_sy;
 pX = new float[maxP];
 pY = new float[maxP];
 pX[0] = 0;
 pY[0] = 0;  
 weight = 3;
}

void aggregate(){
   pushMatrix();
   strokeWeight( weight);

 float arrayDist, arraySize;
 float maxJumpDist = width;
 int closestElement = 0;
 translate(sx,sy);
 float theta = random(TWO_PI);
 float newElementX=startRadius*sin(theta);
 float newElementY=startRadius*cos(theta);
   
 while (true) {
   for (int i=0; i<pCount; i++) {
     arrayDist = dist(pX[i],pY[i],newElementX,newElementY);
     if (arrayDist<maxJumpDist) {
       maxJumpDist = arrayDist;
       closestElement = i;
     }
   }

   if (maxJumpDist<=stickDist) {
     theta = atan2(newElementY-pY[closestElement], newElementX-pX[closestElement]);
     pX[pCount] = pX[closestElement] + pSize*cos(theta);
     pY[pCount] = pY[closestElement] + pSize*sin(theta);
     stroke(255,150);    
     line(pX[pCount],pY[pCount],pX[closestElement],pY[closestElement]);  
     for (int i=0; i<pCount; i++) {
       arraySize = dist(pX[i],pY[i],0,0);
       if (arraySize>startRadius) {
         startRadius = arraySize;
       }
     }
     weight=weight*0.995;
     pCount++;
     if (pCount == maxP) {
       noLoop();
     }
     break;
   }

   theta = random(TWO_PI);
   newElementX+=random(maxJumpDist)*sin(theta);
   newElementY+=random(maxJumpDist)*cos(theta);
   if (dist(0,0,newElementX,newElementY)>startRadius*2) {
     newElementX=startRadius*sin(theta);
     newElementY=startRadius*cos(theta);
   }
 }
 popMatrix();
 }
 }



Re: x,y coordinates from a specific color?
Reply #1 - Apr 20th, 2010, 12:32pm
 
I did something like this today

Check it out here
http://openprocessing.org/visuals/?visualID=9017
Re: x,y coordinates from a specific color?
Reply #2 - Apr 20th, 2010, 1:13pm
 
I don't have any mouse interaction...sorry, didn't solve my problem.

Anybody else???
Re: x,y coordinates from a specific color?
Reply #3 - Apr 20th, 2010, 6:41pm
 
ok sorry. only a quick respone. i hope it can help you.

run trough the pixelArray with
Code:

int closestPos=0;
PVector closestDist = Integer.MAX_VALUE;
int l = width*height;
PVector Cyan = new PVector(0,255,255);
for(int i=0; i < l; i++) {
color c= pixels[i];
// so whatever you define as nearest, you can use the euclidian // //distance for example
// get the distance of the color on that pixel
float dist = cyan.dist(new PVector(red(c),green(c),blue(c)));
// and if its the nearest we save the position
if(dist < closestDist) {
closestPos = i;
closestDist = dist;
}
}


beware. i didnt compile this. after getting the closest position you need can calculate the real position of that i with a little bit math dividing with the width and stuff. you can also get the colors with the method get(x,y) but checking all pixels with this should be faster and you dont need 2 loops.

bye,
ramin
Re: x,y coordinates from a specific color?
Reply #4 - Apr 21st, 2010, 10:15am
 
Thanks for your response, Ramin.
This looks like the way to go. It also turns out to be way more complex than I thought. I am glad when I manage to piece code together but this one is a tough one for me ( ...did I mention, that I started with Processing and code in general just  this year Wink ? )
It makes sense and all but I don't know how the result would have to look like.
How would I have to integrate it into the code above?

Re: x,y coordinates from a specific color?
Reply #5 - Apr 21st, 2010, 10:21am
 
Mouse Interaction is not necessary, just modify the procedure by passing in the colour your are looking for and then when it finds it, return the the x and y value or just calculate it like ramin  said.
Re: x,y coordinates from a specific color?
Reply #6 - Apr 21st, 2010, 6:40pm
 
yes. i wanted to edit your code but somthing at the beginning made an exception and i dont know what the program should do, so i wrote this lines. like andrewowaun said Smiley. modify the method or write a new one that has PImage and a color as parameter and returns an PVector for the point in the image where the color is nearest to the parameter color.
Page Index Toggle Pages: 1