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 › Picture abstractor - optimisation suggestions
Page Index Toggle Pages: 1
Picture abstractor - optimisation suggestions? (Read 481 times)
Picture abstractor - optimisation suggestions?
Apr 29th, 2010, 4:51pm
 
Hi there...

I've written a program which gradually abstracts a starting photo, spreading the colours around. It works by repeatedly drawing a fans of points from pixels in the original photo - a bit like a simple airbrush. The angle of the "spray" is dependent on the hue, but the base angle keeps rotating by 1 degree per second which makes it more interesting as colours then tend to migrate around over time.

This program is part of a larger project and will eventually be a method in that project.

This runs rather slowly - try it out - you will need a 640 by 480 image which you will be prompted to load. I just wondered if anyone had any suggestions for optimisation? Given some of the programs I have seen in Processing and the numbers of particles, I have a feeling it must be possible to write this more efficiently.

The only rewards I can offer are my gratitude and admiration Smiley Smiley Thanks....

Quote:
PImage img;
float angle;  //angle to draw the sprays at
float angleinc=0; //increment - base angle for sprays changes by 1 degree per second
int x, y; //position to draw the sprays from
int timecheck; //need this to increment angle by one degree every second.

void setup()
{
 size(640, 480);
 background(0);
 colorMode(HSB, 360, 255, 255);
 noStroke();
 String getfile = selectInput();
 if(getfile!=null) img = loadImage(getfile);
 set(0, 0, img);
 timecheck=millis();
}

void draw()
{
 loadPixels();
 for(int i=0; i<pixels.length; i++)
 {
   int randno = (int)random(0, 100);
   if(i%100==randno) //only do every 1 in 100 pixels. But keep changing the remainder to stop aliasing effects.
   {
     angle = (hue(pixels[i]) + angleinc)%360; //angle depends on hue. But base angle keeps rotating around, 1 degree per second
     y= (int)(i/width);
     x= i - y*width;
     spray(x, y, pixels[i], radians(angle), 200);
   }
 }
 if(millis()-timecheck>1000)
 {
   angleinc++;
   timecheck=millis();
 }
}


void spray(int xpos, int ypos, color c, float angle, int amount) // draw a 45 degree fan of dots from the initial position at specified angle.
{
 fill(c);
 pushMatrix();
    translate(xpos, ypos);
    rotate(angle); //rotate to the "base" angle of the spray
    for(int i=0; i<amount; i++)
    {
       float deviation = random(-PI/8, PI/8); //random deviation from base angle
       int distance= (int)random(0, 20); //distance from base point
       pushMatrix();
         rotate(deviation);
         rect(distance,0, 1,1);
       popMatrix();
    }
  popMatrix();
}
Page Index Toggle Pages: 1