GMU:Processing im Park/Part3: Difference between revisions

From Medien Wiki
(tile sorting example)
Line 1: Line 1:
== Image Sorting ==
== Image Sorting ==


Line 142: Line 141:
     break;
     break;
   }
   }
}
</source>
=== Tile Sorting ===
<source lang="java">
PImage img;
int d = 10;
PImage[] tiles;
int rows, cols, n;
void setup() {
  size(600, 400);
  img = loadImage("fall.jpg");
  cols = (img.width/d);
  rows = (img.height/d);
  n = cols * rows;
  tiles = new PImage[n];
  for (int i = 0; i < n; i++) {
    int x = d * (i % cols);
    int y = d * (i / cols);
    tiles[i] = img.get(x, y, d, d);
   
  }
 
}
void draw() {
 
  sort(tiles);
  // show tiles
  for(int i = 0; i < n; i++) {
   
    PImage tile = tiles[i];
    int y = d * (i / cols);
    int x = d * (i % cols);
   
    image(tile, x, y, d, d);
   
  }
   
}
void sort(PImage[] tiles) {
 
  for(int y = 0; y < rows; y++) {
    for(int x = 0; x < cols-1; x++) {
     
      int pos1 = y * cols + x;
      int pos2 = y * cols + (x+1);
     
      PImage tile1 = tiles[pos1];
      PImage tile2 = tiles[pos2];
     
      if( saturation(average(tile1)) - saturation(average(tile2)) > mouseX ) {
        PImage tmptile = tiles[pos1];
        tiles[pos1] = tiles[pos2];   
        tiles[pos2] = tmptile;
      }
     
    }
  }
}
color average(PImage img) {
  img.loadPixels();
  color[] pxls = img.pixels;
 
  float r = 0, g = 0, b = 0;
  int n = pxls.length;
 
  for(int i = 0; i < n; i++) {
    color c = pxls[i];
    r += red(c);
    g += green(c);
    b += blue(c);
  }
  color average = color(r/n, g/n, b/n);
  return average;
}
}
</source>
</source>

Revision as of 15:02, 16 January 2016

Image Sorting

Pixel Sorting

PImage img;
int id = 1;
int sortMode = 1;

void setup() {
  size(600, 400);
  reset();
}

void reset() {
    img = loadImage("winterpark.jpg");
}

void draw() {
  
  // load em
  img.loadPixels();
  
  color[] pxls = img.pixels;
  
  switch(sortMode) {
    case 1: 
      sortLeft(pxls);
      break;
    case 2:
      sortDown(pxls);
      break;
    case 3:
      sortUp(pxls);
      break;
  }
  
  
  // update em
  img.updatePixels();
  
  // show em
  image(img, 0, 0);
  

}


void sortLeft(color[] pxls) {
  
  // do something with the pixels (remix them)
  for(int y = 0; y < height; y++) {
    
    for(int x = 0; x < width-1 ; x++) {

      // get indices for left and right pixel
      int left = y * img.width + x;
      int right = y * img.width + (x+1);

       // bubble sort step ( exchange pixels according to saturation ... )
       int posx = mouseX - width / 2;
     
       if(green(pxls[right]) - green(pxls[left]) < posx) {
          // exchange color values of those pixels
          color tmp= pxls[right];
          pxls[right] = pxls[left];
          pxls[left] = tmp;
       }  
    }
  }
  
}

void sortDown(color[] pxls) {

  // do something with the pixels (remix them)
  for(int x = 0; x < width; x++) {
    
    for(int y = height - 2; y > 0 ; y--) {

      // get indices for left and right pixel
      int top = y * img.width + x;
      int bottom = (y+1) * img.width + x;
      
       // bubble sort step ( exchange pixels according to saturation ... )
       int posx = mouseX - width / 2;
     
       if(green(pxls[top]) - green(pxls[bottom]) < posx) {
          // exchange color values of those pixels
          color tmp= pxls[top];
          pxls[top] = pxls[bottom];
          pxls[bottom] = tmp;
       }  
    }
  }
}


void sortUp(color[] pxls) {

  // do something with the pixels (remix them)
  for(int x = 0; x < width; x++) {
    
    for(int y = 0; y < height -1 ; y++) {

      // get indices for left and right pixel
      int top = y * img.width + x;
      int bottom = (y+1) * img.width + x;
      
       // bubble sort step ( exchange pixels according to saturation ... )
       int posx = mouseX - width / 2;
     
       if(green(pxls[top]) - green(pxls[bottom]) < posx) {
          // exchange color values of those pixels
          color tmp= pxls[top];
          pxls[top] = pxls[bottom];
          pxls[bottom] = tmp;
       }  
    }
  }
  
}

void keyPressed() {
  switch(key) {
    case ' ':  
      reset();
      break;
    case 's':
      save("sorted-" + id +".jpg");
      id++;
      break;
    case '1':
      sortMode = 1;
      break;
    case '2':
     sortMode = 2;
     break;
    case '3':
     sortMode = 3;
     break;
  }
}

Tile Sorting

PImage img;
int d = 10;
PImage[] tiles;
int rows, cols, n;


void setup() {

  size(600, 400);
  img = loadImage("fall.jpg");

  cols = (img.width/d);
  rows = (img.height/d);

  n = cols * rows;
  tiles = new PImage[n];

  for (int i = 0; i < n; i++) {

    int x = d * (i % cols);
    int y = d * (i / cols);

    tiles[i] = img.get(x, y, d, d);
    
  }
  
}



void draw() {
  
   sort(tiles);
   // show tiles
   for(int i = 0; i < n; i++) {
     
     PImage tile = tiles[i];

     int y = d * (i / cols);
     int x = d * (i % cols);
     
     image(tile, x, y, d, d);
     
   }
    
}

void sort(PImage[] tiles) {
  
  for(int y = 0; y < rows; y++) {
    for(int x = 0; x < cols-1; x++) {
      
      int pos1 = y * cols + x;
      int pos2 = y * cols + (x+1);
      
      PImage tile1 = tiles[pos1];
      PImage tile2 = tiles[pos2];
      
      if( saturation(average(tile1)) - saturation(average(tile2)) > mouseX ) {
        PImage tmptile = tiles[pos1];
        tiles[pos1] = tiles[pos2];    
        tiles[pos2] = tmptile;
      }
      
    }
  }
}


color average(PImage img) { 
  img.loadPixels();
  color[] pxls = img.pixels;
  
  float r = 0, g = 0, b = 0;
  int n = pxls.length;
  
  for(int i = 0; i < n; i++) {
    color c = pxls[i];
    r += red(c);
    g += green(c);
    b += blue(c);
  }
  color average = color(r/n, g/n, b/n);
  return average;
}