Pixel sorting problem

Hi !

I’m very new to processing and I have a problem with a code I’m working on. I have written some of it, but now I’m stuck finishing it.

I am working with pixel sorting of an image in processing 3.0 and I have written this code (with help from examples I’ve found). With this I’m able to sort the pixels by their brightness etc.

Can anyone help me if I want to distribute the pixels in another way, but still by their brightness?

ex. maybe when the code has sorted 500 pixels it jumps to another random spot and starts sorting? (off course without sorting pixels already sorted/swaped)?

Right now I’m just ordering them horizontally. How could I make an order that’s more organic in some spots… where some of the pixels may be floating across the frame in another way?

I hope someone is able or in the mood to help. Thanks beforehand! :slight_smile:

spanien600x480_brightness

PImage img;
PImage sorted;

void setup() {
  size(600, 480);
  
  img = loadImage("kbhnv600x480.jpg");
  sorted = img.get();
  sorted.loadPixels();

  
  for (int i = 0; i < sorted.pixels.length; i++) {
    float record = -1;
    int selectedPixel = i;
    for (int j = i; j < sorted.pixels.length; j++) {
      color pix = sorted.pixels[j];
  
      float b = brightness(pix);
      if (b > record) {
        selectedPixel = j;
        record = b;
      }
    }


    color temp = sorted.pixels[i];
    sorted.pixels[i] = sorted.pixels[selectedPixel];
    sorted.pixels[selectedPixel] = temp;
  }

  sorted.updatePixels();
}

void draw() {
  background(0);
  image(sorted, 0, 0);
  save("kbhnv600x480_brightness.jpg");
}
1 Like

You could sort a section of the image by changing the bounds of the loop. And you could keep track of the sorted pixels in a boolean array. I would put it into a function so you could call it multiple times.

boolean[] hasBeenSorted;

// in setup after created the sorted image (replaces current sorting)
hasBeenSorted = new boolean[sorted.pixels.length];
for (int i = 0; i < 10; i++) { // sort 10 sections
  sortSection(sorted, int(random(sorted.pixels.length)), 500);
}

void sortSection(PImage sorted, int start, int sectionSize) {
  sorted.loadPixels();
  int end = start + sectionSize;
  if (end > sorted.pixels.length) end = sorted.pixels.length; // avoid error if section goes past the end of pixels
  for (int i = start; i < end; i++) {
    if (!hasBeenSorted[i]) {
      float record = -1;
      // ... fill in the rest of this
      for (int j = i; j < end; j++) { // only line that is different
      // ... 
      sorted.pixels[selectedPixel] = temp;
      hasBeenSorted[i] = true;
    }
  }
  sorted.updatePixels();
}

FYI I didn’t test the code above so it might have bugs just to give you an idea. Could also check out Jeff Thompson’s pixels sorting, n-px block looks like a similar idea just not random. There are also some other cool ideas in there.

Side Note: In future please format your code all your code using the </> in the text editor on the forum. It makes it much easier to read and you can also edit this post by clicking on the pencil icon at the bottom of the post.

1 Like

I have been looking a lot on Jeff Thompson’s work - it is very nice. I can’t understand/read how the codes work though, so that’s why I’m trying to begin with more simple codes :slight_smile:

Thank you so much for the code. The problem I seem to get is that hasBeenSorted is declared in setup, but I am using it in void sortSection.

Can anyone tell me how to access it?

1 Like

try like:

boolean[] hasBeenSorted;

void setup() {
//...
hasBeenSorted = new boolean[sorted.pixels.length];
for (int i = 0; i < 10; i++) { // sort 10 sections
  sortSection(sorted, int(random(sorted.pixels.length)), 500);
}
//...
}

this way it is declared global,
and filled by setup
and available everywhere.

@figraham actually told you ( in words ) already when he say

// in setup

1 Like