Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- PImage input;
- boolean[][] edge;
- int[] pix;
- float cutoff = 0.0135;
- String file = "in4.jpg";
- void setup() {
- input = loadImage(file);
- int w = 500;
- size(w, int(w*(input.height/(input.width + 0f))));
- input.loadPixels();
- pix = input.pixels;
- edge = new boolean[input.width][input.height];
- checkHoriz(input, 2);
- checkVert(input, 2);
- //checkHoriz(input,4);
- //checkVert(input,4);
- //checkHoriz(input,8);
- //checkVert(input,8);
- //checkHoriz(input, 10);
- //checkVert(input, 10);
- noLoop();
- draw();
- }
- void draw() {
- PGraphics g = createGraphics(input.width, input.height);
- g.loadPixels();
- for (int x = 0; x < g.width; x ++) {
- for (int y = 0; y < g.height; y ++) {
- if (edge[x][y]) g.pixels[y*g.width+x] = color(0);
- else g.pixels[y*g.width+x] = color(255);
- }
- }
- g.updatePixels();
- image(g, 0, 0, width, height);
- }
- void keyReleased(){
- if(key == ' '){
- save("out/" + file);
- }
- }
- void checkHoriz(PImage image, int z) {
- int w = z;
- int h = z;
- // size of rect = 2w * h
- for (int x = 0; x < image.width; x += w) {
- for (int y = 0; y < image.height; y += h) {
- if (edge(x, y, w, h, w, 0)) {
- put(edge, x, y, w, h, true);
- }
- }
- }
- }
- void checkVert(PImage image, int z) {
- int w = z;
- int h = z;
- // size of rect = 2w * h
- for (int x = 0; x < image.width- w; x += w) {
- for (int y = 0; y < image.height-h; y += h) {
- if (edge(x, y, w, h, 0, h)) {
- put(edge, x, y, w, h, true);
- }
- }
- }
- }
- void put(boolean[][] b, int x, int y, int w, int h, boolean c) {
- for (int ix = x; ix < x + w; ix ++) {
- for (int iy = y; iy < y + h; iy ++) {
- if (ix < 0 || iy < 0 || ix >= b.length || iy >= b[ix].length) continue;
- b[ix][iy] = true;
- }
- }
- }
- boolean edge(int x, int y, int w, int h, int iw, int ih) {
- int[] avg = avg(x, y, w, h);
- int[] avg1 = avg(x+iw, y+ih, w, h);
- float tf = 0;
- for (int i = 0; i < avg.length; i ++) {
- int d = abs(avg[i]-avg1[i]);
- float f= d/(256.0*3);
- tf += f;
- if (f >= 1) println(f);
- if (f > cutoff) return true;
- }
- return false;
- //return tf > cutoff*w*h;
- }
- int[] avg(int x, int y, int w, int h) {
- int r = 0;
- int g = 0;
- int b = 0;
- int n = w*h;
- for (int ix = x; ix < x + w; ix ++) {
- for (int iy = y; iy < y + h; iy ++) {
- if (ix <= 0 || iy <= 0 || ix >= input.width || iy >= input.height) continue;
- int c = pix[iy*input.width+ix];
- r += red(c);
- g += green(c);
- b += blue(c);
- }
- }
- return new int[] {
- r/n, g/n, b/n
- };
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement