GET OFF YOUR PHONE!

Heads up!

Look at the sun.

Appreciate the light.

Come back when you are on a Lap or Desktop.

(This page would also drain your mobile data, trust me)
F a r n F r e u n d/ │ F E E D/

Twin Pics [Rebuilder]

Yet another experiment of finding twin images in the unrendered.

In this script I try to rebuild an image with the pixels of another. One image provides the motif and another one provides the color with which the motif will be rebuild.

It does so by comparing the pixels of both initial images and looking for pixels with matching RGB values. Since I haven't found actual twin images yet, the margin of what counts as a match between colors gets increased with each iteration so th new image eventually gets filled completely

The Code:

PImage shape, colors, canvas;
int[] rendered;
int[] taken; 
int count, margin, widi, heit;

void setup() {
  size(900, 400);
  widi = 300;
  heit = 400;
  shape = loadImage("pines1.jpg");  // The image providing the shape, the conturs, the motif
  colors = loadImage("kiosk1.png");      // the image providing the colors, couldn't thing of a better variable name yet
  canvas = createImage(widi, heit, RGB); // The newly built image
  shape.resize(widi, heit);
  colors.resize(widi, heit);
  // Create arrays to keep track which pixels were already matched
  rendered = new int[widi * heit];
  taken = new int[widi * heit];
  count = 0; // count variable to keep track of progress
  margin = 0; // Start with a margin of 0

  // Load the pixel data
  shape.loadPixels();
  colors.loadPixels();

  // Initialize the rendered and taken arrays, mark all pixels als not taken/rendered
  for (int i = 0; i < widi * heit; i++) {
    rendered[i] = 0;
    taken[i] = 0;
  }
  //put all images on the screen for the first frame
  background(0);
  image(colors, 0, 0);
  image(shape, widi, 0);
  image(canvas, widi * 2, 0);
  saveFrame("frames/####.jpg");
}
// Calculate the euclidean distance between two colors
float colorDistance(int shapeColor, int colorsColor) {

  int redShape = (shapeColor >> 16) & 0xFF;
  int greenShape = (shapeColor >> 8) & 0xFF;
  int blueShape = shapeColor & 0xFF;

  int redColors = (colorsColor >> 16) & 0xFF;
  int greenColors = (colorsColor >> 8) & 0xFF;
  int blueColors = colorsColor & 0xFF;

  return dist(redShape, greenShape, blueShape, redColors, greenColors, blueColors);
}

void draw() {
  // Reload source image pixels for visualization
  canvas.loadPixels();
  colors.loadPixels();
  shape.loadPixels();

  boolean anyMatchFound = false; // A boolean to track if there were any matches in this iteration

  // Iterate through the pixels in the shape image
  for (int i = 0; i < widi * heit; i++) {
    if (rendered[i] == 1) continue;  // Skip already rendered pixels
    int shapeColor = shape.pixels[i];

    // Now check for all pixels in the colors image
    for (int j = 0; j < widi * heit; j++) {
      if (taken[j] == 1) continue;  // Skip already taken pixels
      int colorsColor = colors.pixels[j];
      float distColor = colorDistance(shapeColor, colorsColor);

      // If the distance is within the margin, consider it a match
      if (distColor <= margin) {
        //mark pixels as rendered/taken
        rendered[i] = 1;
        taken[j] = 1;
        canvas.pixels[i] = colorsColor;  // Set the pixel in the new image, position of the shape image, color of the colors image
        
        //set the used pixels in the shape and colors image to black for visualization
        shape.pixels[i] = color(0);
        colors.pixels[j] = color(0);
        anyMatchFound = true; // A match was found
        count++;  
        break;  // Stop searching for a match for this pixel
      }
    }
  }

  // If any match was found, update the images and save a frame
  if (anyMatchFound) {
    canvas.updatePixels();
    colors.updatePixels(); 
    shape.updatePixels();
    saveFrame("framesPINES1eucl/####.jpg");
  }

  margin++;  // Increase margin to allow for more lenient matching in the next iteration
  println(margin);

  // Display the images, save a frame
  image(colors, 0, 0);        
  image(shape, widi, 0);  
  image(canvas, widi * 2, 0); 
  saveFrame("framesPINES1eucl/####.jpg");
  
  // Stop the draw loop once all pixels are processed
  if (count == totalPixels) {
    noLoop();
    println("Completed!");
    exit();
  }
}

In this example the pixels are matched by looking at the euclidian distance of two colors in the 3 Axis RGB color space. But one could also match by greyscale value too:

float colorDistance(int c1, int c2) {
  // Calculate the Euclidean distance between two colors
  int redShape = (shapeColor >> 16) & 0xFF;
  int greenShape = (shapeColor >> 8) & 0xFF;
  int blueShape = shapeColor & 0xFF;
  int avgShape = (redShape+greenShape+blueShape)/3;

  int redColors = (colorsColor >> 16) & 0xFF;
  int greenColors = (colorsColor >> 8) & 0xFF;
  int blueColors = colorsColor & 0xFF;
  int avgColors = (r2+g2+b3)/3;

  return abs(avgShape-avgColros);  // distance between the greyscale values
}

Which will lead to different images:

It shares the same issue of slowness with that earlier script from Close Encounters, especially when calculating the euclidian distance.

But for a Video installation I have in mind, I will most likely re-code it in Python, eventually. I reckon NumPy could handle this messe more efficiently.

Right now it takes both my own machine and the Mac Desktop I have at my disposal way too long. I have hardrives to fill!

Related:

Close Encounters

Tags

💬