Advertisement
Guest User

Sort Image With Edge Detection

a guest
Jan 11th, 2013
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.83 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. String filename = "mountains/06.jpg";
  4. int thresh = 10;
  5. int steps = 3;
  6. boolean saveIt = true;
  7.  
  8. color[] px;
  9. int[] pos = new int[0];
  10. int[] allPrevPos = new int[0];
  11.  
  12. void setup() {
  13.  
  14.   PImage img = loadImage(filename);
  15.   img.loadPixels();
  16.  
  17.   size(img.width, img.height);
  18.   background(255);
  19.  
  20.   for (int step = 0; step < steps; step++) {
  21.  
  22.     println("STEP: " + (step+1) + "/" + steps);
  23.     px = new color[0];
  24.  
  25.     if (step == 0) {
  26.       println("  finding edges...");
  27.       findEdges(img);
  28.     }
  29.     else {
  30.       println("  getting new pixels outwards...");
  31.       int[] newPos = new int[0];
  32.       for (int i=0; i<pos.length; i++) {
  33.        
  34.         // avoid duplicates, both with the current round and with all previously traversed pixels
  35.         // this is necessary for the concentric rings
  36.        
  37.         // ALSO: the order we test here is important for performance; the simplest test is if the new px is
  38.         // offscreen, next the smaller array of new positions, and finally looking in the largest array
  39.         // of previously traversed px - if the first expr is false, then we skip checking the others!
  40.         int p = pos[i] - width;
  41.         // if (p >= 0 && !Arrays.asList(newPos).contains(p) && !Arrays.asList(allPrevPos).contains(p)) {                // U
  42.         if (p >= 0) {
  43.           newPos = append(newPos, p);
  44.         }
  45.         p = pos[i] + 1;
  46.         // if (p < width*height-1 && !Arrays.asList(newPos).contains(p) && !Arrays.asList(allPrevPos).contains(p)) {    // R
  47.         if (p < width*height-1) {
  48.           newPos = append(newPos, p);
  49.         }
  50.         p = pos[i] + width;
  51.         // if (p < width*height-1 && !Arrays.asList(newPos).contains(p) && !Arrays.asList(allPrevPos).contains(p)) {    // D
  52.         if (p < width*height-1) {
  53.           newPos = append(newPos, pos[i] + width);
  54.         }
  55.         p = pos[i] - 1;
  56.         // if (p >= 0 && !Arrays.asList(newPos).contains(p) && !Arrays.asList(allPrevPos).contains(p)) {                // L
  57.         if (p >= 0) {
  58.           newPos = append(newPos, p);
  59.         }
  60.       }
  61.       pos = newPos;
  62.     }
  63.  
  64.     // get values from list of positions
  65.     println("  retrieving pixel values...");
  66.     for (int i=0; i<pos.length; i++) {
  67.       px = append(px, img.pixels[pos[i]]);
  68.     }
  69.  
  70.     // sort using built-in Java method
  71.     println("  sorting...");
  72.     px = sort(px);
  73.  
  74.     // put the pixels back in place!
  75.     println("  updating results...");
  76.     loadPixels();
  77.     for (int i=0; i<px.length; i++) {
  78.       pixels[pos[i]] = px[i];
  79.     }
  80.     updatePixels();
  81.    
  82.     // add current positions to list of previous
  83.     allPrevPos = concat(allPrevPos, pos);
  84.   }
  85.  
  86.   // save results
  87.   if (saveIt) {
  88.     println("Saving image...");
  89.     save("export.tiff");
  90.   }
  91.  
  92.   println("DONE!");
  93. }
  94.  
  95.  
  96. void findEdges(PImage source) {
  97.  
  98.   float[][] kernel = {
  99.     {
  100.       -1, -1, -1
  101.     }
  102.     ,
  103.     {
  104.       -1, 9, -1
  105.     }
  106.     ,
  107.     {
  108.       -1, -1, -1
  109.     }
  110.   };
  111.  
  112.   source.loadPixels();
  113.   for (int y = 1; y < source.height-1; y++) {           // skip top and bottom edges
  114.     for (int x = 1; x < source.width-1; x++) {          // skip left and right edges
  115.       float sum = 0;                                    // kernel sum for this pixel
  116.       for (int ky = -1; ky <= 1; ky++) {
  117.         for (int kx = -1; kx <= 1; kx++) {
  118.           int position = (y + ky)*source.width + (x + kx);    // calculate the adjacent pixel for this kernel point
  119.           float val = red(source.pixels[position]);           // image is grayscale, red/green/blue are identical
  120.           sum += kernel[ky+1][kx+1] * val;                    // multiply adjacent pixels based on the kernel values
  121.         }
  122.       }
  123.  
  124.       if (sum < thresh) {
  125.         pos = append(pos, y*source.width + x);           // add edge px to array
  126.       }
  127.     }
  128.   }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement