Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- String filename = "mountains/06.jpg";
- int thresh = 10;
- int steps = 3;
- boolean saveIt = true;
- color[] px;
- int[] pos = new int[0];
- int[] allPrevPos = new int[0];
- void setup() {
- PImage img = loadImage(filename);
- img.loadPixels();
- size(img.width, img.height);
- background(255);
- for (int step = 0; step < steps; step++) {
- println("STEP: " + (step+1) + "/" + steps);
- px = new color[0];
- if (step == 0) {
- println(" finding edges...");
- findEdges(img);
- }
- else {
- println(" getting new pixels outwards...");
- int[] newPos = new int[0];
- for (int i=0; i<pos.length; i++) {
- // avoid duplicates, both with the current round and with all previously traversed pixels
- // this is necessary for the concentric rings
- // ALSO: the order we test here is important for performance; the simplest test is if the new px is
- // offscreen, next the smaller array of new positions, and finally looking in the largest array
- // of previously traversed px - if the first expr is false, then we skip checking the others!
- int p = pos[i] - width;
- // if (p >= 0 && !Arrays.asList(newPos).contains(p) && !Arrays.asList(allPrevPos).contains(p)) { // U
- if (p >= 0) {
- newPos = append(newPos, p);
- }
- p = pos[i] + 1;
- // if (p < width*height-1 && !Arrays.asList(newPos).contains(p) && !Arrays.asList(allPrevPos).contains(p)) { // R
- if (p < width*height-1) {
- newPos = append(newPos, p);
- }
- p = pos[i] + width;
- // if (p < width*height-1 && !Arrays.asList(newPos).contains(p) && !Arrays.asList(allPrevPos).contains(p)) { // D
- if (p < width*height-1) {
- newPos = append(newPos, pos[i] + width);
- }
- p = pos[i] - 1;
- // if (p >= 0 && !Arrays.asList(newPos).contains(p) && !Arrays.asList(allPrevPos).contains(p)) { // L
- if (p >= 0) {
- newPos = append(newPos, p);
- }
- }
- pos = newPos;
- }
- // get values from list of positions
- println(" retrieving pixel values...");
- for (int i=0; i<pos.length; i++) {
- px = append(px, img.pixels[pos[i]]);
- }
- // sort using built-in Java method
- println(" sorting...");
- px = sort(px);
- // put the pixels back in place!
- println(" updating results...");
- loadPixels();
- for (int i=0; i<px.length; i++) {
- pixels[pos[i]] = px[i];
- }
- updatePixels();
- // add current positions to list of previous
- allPrevPos = concat(allPrevPos, pos);
- }
- // save results
- if (saveIt) {
- println("Saving image...");
- save("export.tiff");
- }
- println("DONE!");
- }
- void findEdges(PImage source) {
- float[][] kernel = {
- {
- -1, -1, -1
- }
- ,
- {
- -1, 9, -1
- }
- ,
- {
- -1, -1, -1
- }
- };
- source.loadPixels();
- for (int y = 1; y < source.height-1; y++) { // skip top and bottom edges
- for (int x = 1; x < source.width-1; x++) { // skip left and right edges
- float sum = 0; // kernel sum for this pixel
- for (int ky = -1; ky <= 1; ky++) {
- for (int kx = -1; kx <= 1; kx++) {
- int position = (y + ky)*source.width + (x + kx); // calculate the adjacent pixel for this kernel point
- float val = red(source.pixels[position]); // image is grayscale, red/green/blue are identical
- sum += kernel[ky+1][kx+1] * val; // multiply adjacent pixels based on the kernel values
- }
- }
- if (sum < thresh) {
- pos = append(pos, y*source.width + x); // add edge px to array
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement