Advertisement
Guest User

Untitled

a guest
Mar 5th, 2015
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. PImage input;
  2. boolean[][] edge;
  3. int[] pix;
  4. float cutoff = 0.0135;
  5. String file = "in4.jpg";
  6.  
  7. void setup() {
  8.   input = loadImage(file);
  9.   int w = 500;
  10.   size(w, int(w*(input.height/(input.width + 0f))));
  11.   input.loadPixels();
  12.   pix = input.pixels;
  13.   edge = new boolean[input.width][input.height];
  14.   checkHoriz(input, 2);
  15.   checkVert(input, 2);
  16.   //checkHoriz(input,4);
  17.   //checkVert(input,4);
  18.   //checkHoriz(input,8);
  19.   //checkVert(input,8);
  20.   //checkHoriz(input, 10);
  21.   //checkVert(input, 10);
  22.   noLoop();
  23.   draw();
  24. }
  25.  
  26. void draw() {
  27.   PGraphics g = createGraphics(input.width, input.height);
  28.   g.loadPixels();
  29.   for (int x = 0; x < g.width; x ++) {
  30.     for (int y = 0; y < g.height; y ++) {
  31.       if (edge[x][y]) g.pixels[y*g.width+x] = color(0);
  32.       else g.pixels[y*g.width+x] = color(255);
  33.     }
  34.   }
  35.   g.updatePixels();
  36.   image(g, 0, 0, width, height);
  37. }
  38.  
  39. void keyReleased(){
  40.   if(key == ' '){
  41.     save("out/" + file);
  42.   }
  43. }
  44.  
  45. void checkHoriz(PImage image, int z) {
  46.   int w = z;
  47.   int h = z;
  48.   // size of rect = 2w * h
  49.   for (int x = 0; x < image.width; x += w) {
  50.     for (int y = 0; y < image.height; y += h) {
  51.       if (edge(x, y, w, h, w, 0)) {
  52.         put(edge, x, y, w, h, true);
  53.       }
  54.     }
  55.   }
  56. }
  57.  
  58. void checkVert(PImage image, int z) {
  59.   int w = z;
  60.   int h = z;
  61.   // size of rect = 2w * h
  62.   for (int x = 0; x < image.width- w; x += w) {
  63.     for (int y = 0; y < image.height-h; y += h) {
  64.       if (edge(x, y, w, h, 0, h)) {
  65.         put(edge, x, y, w, h, true);
  66.       }
  67.     }
  68.   }
  69. }
  70.  
  71. void put(boolean[][] b, int x, int y, int w, int h, boolean c) {
  72.   for (int ix = x; ix < x + w; ix ++) {
  73.     for (int iy = y; iy < y + h; iy ++) {
  74.       if (ix < 0 || iy < 0 || ix >= b.length || iy >= b[ix].length) continue;
  75.       b[ix][iy] = true;
  76.     }
  77.   }
  78. }
  79.  
  80. boolean edge(int x, int y, int w, int h, int iw, int ih) {
  81.   int[] avg = avg(x, y, w, h);
  82.   int[] avg1 = avg(x+iw, y+ih, w, h);
  83.   float tf = 0;
  84.   for (int i = 0; i < avg.length; i ++) {
  85.     int d = abs(avg[i]-avg1[i]);
  86.     float f= d/(256.0*3);
  87.     tf += f;
  88.     if (f >= 1) println(f);
  89.     if (f > cutoff) return true;
  90.   }
  91.   return false;  
  92.   //return tf > cutoff*w*h;
  93. }
  94.  
  95. int[] avg(int x, int y, int w, int h) {
  96.   int r = 0;
  97.   int g = 0;
  98.   int b = 0;
  99.   int n = w*h;
  100.   for (int ix = x; ix < x + w; ix ++) {
  101.     for (int iy = y; iy < y + h; iy ++) {
  102.       if (ix <= 0 || iy <= 0 || ix >= input.width || iy >= input.height) continue;
  103.       int c = pix[iy*input.width+ix];
  104.       r += red(c);
  105.       g += green(c);
  106.       b += blue(c);
  107.     }
  108.   }
  109.   return new int[] {
  110.     r/n, g/n, b/n
  111.   };
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement