trouble reading images.
in
Programming Questions
•
1 year ago
I am trying to modify this DLA script to respond to an image loaded in the background.
I can't get it to work for some reason. It seems to be responding to parts of the image and not others.
What should be happening is the points should become more dense in the areas where the red value is 58 and less dense where it is 256. This is modulated with the sticking probability parameter.
This is the image I am using.
Can someone help? This is the code am using.
- //list of current circles
- ArrayList circles;
- //radius of the circle
- public float diameter = 4;
- float diameter_sq = sq(diameter);
- //where to add new circles
- int w = 1000;
- int h = 500;
- float rr = 0;
- float gg = 255;
- float bb = 0;
- float max_distance = w;
- //maximum move in x or y direction at each step
- float max_move = 1;
- //tie this variable into an image value
- public float sticking_probability = 1;
- float max_x, max_y, min_x, min_y;
- PVector dla_center;
- float dla_radius;
- float bounds_addition = 50;
- float bounds_scalar = 1.5;
- //use bucking for quick interestion
- HashMap buckets = new HashMap();
- // FORREST ADDED TWO FUNCTIONS, fillArea and addCell
- // fill an area with random green
- void fillArea() {
- fill(rr,gg,bb);
- }
- //int[][] cp = new int[w][h];
- public float[] allc = new float[h+w*w];
- // addCell takes the x,y position and plots a random sized dot.
- void addCell(float xPosition, float yPosition) {
- // for right now, we use center mode. It may be better to use corner mode.
- ellipseMode(CENTER);
- ellipse(xPosition, yPosition, diameter/2, diameter/2);
- }
- void setup() {
- size(w,h);
- background(255);
- fill(rr,gg,bb);
- loadPixels();
- // Since we are going to access the image's pixels too
- PImage img = loadImage("aw.png");
- image(img, 0, 0);
- img.loadPixels();
- for (int y = 0; y < height; y++) {
- for (int x = 0; x < width; x++) {
- int loc = x + y*width;
- // The functions red(), green(), and blue() pull out the 3 color components from a pixel.
- float r = red(img.pixels[loc]);
- //println(r);
- allc[loc] = r;
- // Image Processing would go here
- // If we were to change the RGB values, we would do it here, before setting the pixel in the display window.
- // Set the display pixel to the image pixel
- //pixels[loc] = color(r,g,b);
- }
- }
- smooth();
- noStroke();
- circles = new ArrayList();
- //seed it with an initial circle in the middle
- PVector first_circle = new PVector(width/2.0,height/2.0);
- //draw circle
- //ellipse(first_circle.x, first_circle.y, diameter, diameter);
- // FORREST ADDED TO FUNCTION CALLS
- fillArea();
- addCell(first_circle.x, first_circle.y);
- dla_center = new PVector();
- max_x = min_x = dla_center.x = first_circle.x;
- max_y = min_y = dla_center.y = first_circle.y;
- dla_radius = 1;
- circles.add(first_circle);
- //add first_circle to the bucket
- int x_index = int(first_circle.x/diameter);
- int y_index = int(first_circle.y/diameter);
- Integer index = new Integer(((x_index <<16) >>> 16) | (y_index << 16));
- if(buckets.containsKey(index)) ((ArrayList) buckets.get(index)).add(first_circle);
- else {
- ArrayList new_bucket = new ArrayList();
- new_bucket.add(first_circle);
- buckets.put(index, new_bucket);
- }
- }
- void draw() {
- addCircle();
- }
- void addCircle() {
- //computer lower and upper bounds of space
- float bound_radius = dla_radius*bounds_scalar+bounds_addition;
- float bound_radius_sq = sq(bound_radius);
- //rect(lower_bound_x,lower_bound_y,upper_bound_x-lower_bound_x, upper_bound_y-lower_bound_y);
- fill(rr,gg,bb);
- //make a new circle on the bounds at a random angle
- float angle = random(0,TWO_PI);
- PVector current_circle = new PVector(cos(angle)*bound_radius+dla_center.x,sin(angle)*bound_radius+dla_center.y);
- //move the circle randomly until it hits the an existing circle
- while(true) {
- if(intersect(current_circle)) {
- int loc = int(current_circle.x) + int(current_circle.y)*width;
- // if(xPosition<height && yPosition<width){
- // sticking_probability = allc[loc]/1000;
- // }
- if(current_circle.x>height && current_circle.y<width && current_circle.y>0){
- //println("uuu");
- //println(allc[loc]);
- if(allc[loc] == 58.0){
- //sticking_probability = allc[loc]/1000;
- sticking_probability = 0.01;
- }
- if(allc[loc] == 255.0){
- sticking_probability = 1;
- //println("zzzz");
- }
- //println("shit2");
- //fill(allc[loc]);
- //println(sticking_probability);
- //println("zzz2");
- }
- if(random(1.0) < sticking_probability) {
- gg -= 0.2;
- bb += 0.2;
- //println(allc[loc]);
- break;
- }
- else {
- //move away
- }
- }
- current_circle.add(random(-max_move, max_move),random(-max_move, max_move),0);
- //wrap around so it cannot get too far away
- //get the vector from dla_center to current_circle
- PVector center_to_circle = PVector.sub(current_circle,dla_center);
- float sq_dist = sq(center_to_circle.x)+sq(center_to_circle.y);
- if(sq_dist > bound_radius_sq) {
- float prev_dist = sqrt(sq_dist);
- float new_dist = 2*bound_radius-prev_dist;
- center_to_circle.mult(-new_dist/prev_dist);
- current_circle.x = center_to_circle.x+dla_center.x;
- current_circle.y = center_to_circle.y+dla_center.y;
- }
- }
- // ellipse(current_circle.x, current_circle.y, diameter, diameter);
- fillArea();
- //int loc = int(current_circle.x) + int(current_circle.y)*width;
- //println(height);
- //println(current_circle.x);
- //println(width);
- //println(current_circle.y);
- //println("l");
- //if(current_circle.x>height && current_circle.y<width && current_circle.y>0){
- //sticking_probability = allc[loc]/1000+0.00001;
- //println("shit");
- //println(sticking_probability);
- //println("zzz");
- //}
- //else{
- //sticking_probability = 0.25;
- //println("yo");
- //}
- addCell(current_circle.x, current_circle.y);
- //update bounds
- PVector center_to_circle = PVector.sub(current_circle,dla_center);
- float sq_dist = sq(center_to_circle.x)+sq(center_to_circle.y);
- if(sq_dist > sq(dla_radius)) {
- float curr_dist = sqrt(sq_dist);
- float to_move = (curr_dist-dla_radius)/2;
- dla_radius+=to_move;
- center_to_circle.mult(to_move/curr_dist);
- dla_center.add(center_to_circle);
- }
- circles.add(current_circle);
- //add circle to the buckets
- int x_index = int(current_circle.x/diameter);
- int y_index = int(current_circle.y/diameter);
- Integer index = new Integer(((x_index <<16) >>> 16) | (y_index << 16));
- if(buckets.containsKey(index)) ((ArrayList) buckets.get(index)).add(current_circle);
- else {
- ArrayList new_bucket = new ArrayList();
- new_bucket.add(current_circle);
- buckets.put(index, new_bucket);
- }
- }
- boolean intersect(PVector circle) {
- ArrayList near_circles = new ArrayList();
- int x_index = int(circle.x/diameter);
- int y_index = int(circle.y/diameter);
- //get nearby points
- Integer index = new Integer(((x_index <<16) >>> 16) | (y_index << 16));
- if(buckets.containsKey(index)) near_circles.addAll((ArrayList) buckets.get(index));
- index = new Integer((((x_index-1) <<16) >>> 16) | (y_index << 16));
- if(buckets.containsKey(index)) near_circles.addAll((ArrayList) buckets.get(index));
- index = new Integer((((x_index+1) <<16) >>> 16) | (y_index << 16));
- if(buckets.containsKey(index)) near_circles.addAll((ArrayList) buckets.get(index));
- index = new Integer(((x_index <<16) >>> 16) | ((y_index-1) << 16));
- if(buckets.containsKey(index)) near_circles.addAll((ArrayList) buckets.get(index));
- index = new Integer((((x_index-1) <<16) >>> 16) | ((y_index-1) << 16));
- if(buckets.containsKey(index)) near_circles.addAll((ArrayList) buckets.get(index));
- index = new Integer((((x_index+1) <<16) >>> 16) | ((y_index-1) << 16));
- if(buckets.containsKey(index)) near_circles.addAll((ArrayList) buckets.get(index));
- index = new Integer(((x_index <<16) >>> 16) | ((y_index+1) << 16));
- if(buckets.containsKey(index)) near_circles.addAll((ArrayList) buckets.get(index));
- index = new Integer((((x_index-1) <<16) >>> 16) | ((y_index+1) << 16));
- if(buckets.containsKey(index)) near_circles.addAll((ArrayList) buckets.get(index));
- index = new Integer((((x_index+1) <<16) >>> 16) | ((y_index+1) << 16));
- if(buckets.containsKey(index)) near_circles.addAll((ArrayList) buckets.get(index));
- //find intersection with nearby points
- for(Iterator it = near_circles.iterator();it.hasNext();) {
- PVector circle2 = (PVector) it.next();
- float dist_sq = sq(circle2.x-circle.x)+sq(circle2.y-circle.y);
- if(dist_sq < diameter_sq) return true;
- }
- return false;
- }
- void mouseClicked() {
- println(mouseX);
- println(mouseY);
- println(allc[mouseX + mouseY* width]);
- }
1