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 › random clusters of pixels from a pic
Page Index Toggle Pages: 1
random clusters of pixels from a pic (Read 571 times)
random clusters of pixels from a pic
Feb 15th, 2010, 2:53am
 
hi , i was thinking about this, maybe you have the answer:

How can i make a program that selects random clusters of pixels from a image? what i mean is that the program selects a group of random pixels that are near one from each other

Are there any strategy for selecting random clusters of pixels from an image?

The only condition i waould like if that it never selects pixels from the borders , is it possible to do something like this?

any idea?

thanks


Seb.
Re: random clusters of pixels from a pic
Reply #1 - Feb 15th, 2010, 3:50am
 
if you want "squarish" clusters simply select a starting poin and an ending point inside the pixels[] array.

if you want circular clusters select a random point, select a random radius and select all the pixels whose distance from the  point is less than radius.

if you do like before but continually change the radius (ie with brutal randoms or with an elegant perlin noise) you'll get irregular clusters.

you could mix the circular and squarish approaches to get more irregular clusters, etc etc

depending on the way you choose you can avoid borders excluding them a priori or prning them from the pixels you find.
Re: random clusters of pixels from a pic
Reply #2 - Feb 15th, 2010, 3:54am
 
Well you can use get() to get a pixel from the screen (though note that "pixels[y*width+x]" is supposed to be quicker), so that just leaves you with how you define the points you need.  If you want to take random points from a rectangular area you could set a random start point for this area (making sure it's away from the borders) and then get points at random from within the rectangle.

Code:
void setup(){
 size(400,300);

 // let's say the rectangle is 100 x100 - actually a square ;)
 int rectW = 100;
 int rectH = 100;
 // start point that offsets by a specified amount from the border
 int offset = 30;
 float startX = offset + random((width-rectW-offset*2));
 float startY = offset + random((height-rectH-offset*2));
 int numberOfPoints = (int) random(100,300);
 for(int i=0; i<numberOfPoints; i++){
   float pointX = random(rectW);
   float pointY = random(rectH);
   // here rather than drawing something you'd do a get() on the coordinates
   noStroke();
   fill(0);
   rect(startX+pointX,startY+pointY,1,1);
 }

}


Now that's fairly crude and probably not what you want; but the principle seems sound.  A better approach would be to get points within the radius of a circle centred on the random location.  That shouldn't be too difficult: instead of randomising x and y you'd just get a random angle between 0 and 360 degrees and a random radius within the threshold and apply a little trigonometry.  And you'd obviously need to change the offset to take account that you'd be getting from the centre rather than a corner...
Page Index Toggle Pages: 1