Loading...
Logo
Processing Forum
Hello,

I'm new to sorting algorithms, and in an effort to keep things as simple as possible for myself, I decided to use a bubble sort to sort the pixels of an image. I know that bubble sort is going to be slow, but I'd like to make it work anyhow. When I run the code below nothing happens. 

Is the problem in my sort, or does it have to do with the way I'm filling the array from the pixels? 

Here is what I've got.

Copy code
  1. PImage img;
  2. int increment= 2;

  3. void setup(){
  4.  img = loadImage("image01.png");
  5.  size (img.width, img.height);
  6.  image(img, 0, 0, width, height);
  7. }

  8. void doSort(){
  9.  loadPixels();
  10.   for (int x = 0; x<((width*height)-(increment+2)); x++){
  11.    bubbleSort(pixels,x, x+increment); 
  12.   }
  13.    updatePixels();
  14.  }

  15. void bubbleSort (int x[], int i, int j){
  16.  // loop through the array
  17.  for (i = 0;  i<x.length-1;i++) {
  18.    if (x[i+1] > x[i]) {
  19.    //swap i and j values
  20.     j = x[i];
  21.     x[i] = x[i+1];
  22.     x[i+1] = j;
  23.    }
  24.  }
  25. }
Thanks in advance for the help!

Replies(9)

You have a doSort() method, but it is never called... in fact, there is no draw() function!
I am a fool.  Thanks for pointing that out!  It's working fine now, though it is really slow. 
Below is the full code:

Copy code

  1. PImage img;
  2. int increment= 2;

  3. void setup(){
  4.  img = loadImage("trashS.jpg");
  5.  size (img.width, img.height);
  6.  image(img, 0, 0, width, height);
  7. }

  8. void draw(){
  9.  doSort(); 
  10. }

  11. void doSort(){
  12.  loadPixels();
  13.   for (int x = 0; x<((width*height)-(increment+2)); x++){
  14.    bubbleSort(pixels,x, x+increment); 
  15.   }
  16.    updatePixels();
  17.  }

  18. void bubbleSort (int x[], int i, int j){
  19.  // loop through the array
  20.  for (i = 0;  i<x.length-1;i++) {
  21.    if (x[i+1] > x[i]) {
  22.    //swap i and j values
  23.     j = x[i];
  24.     x[i] = x[i+1];
  25.     x[i+1] = j;
  26.    }
  27.  }
  28. }
How big is the image you are trying to sort?
Agreed regarding doSort().  draw() is not really needed here, though.  

I would like to add a few comments to this:
  1. Conceptually, I enjoyed thinking about your program...sorting the pixels of an image.
  2. It was a little tricky trying to figure out some of your code because your bubbleSort() function had three parameters.  Two of the parameters weren't really necessary.  All you really need to pass to the function is the array itself.  If you need some variables in your bubbleSort() function to help you with the sorting, then declare those as local variables, within the function itself. 
  3. pixels is a one-dimensional array containing all the pixels you want to sort, right?  Since that is the case, it's simpler to rewrite this loop:

Copy code
  1.             for (int x = 0; x<((width*height)-(increment+2)); x++){

            like this:

Copy code
  1.             for (int x = 0; x<pixels.length - 1; x++){

In the end, I tried to make some simple changes to your code to make it a little easier for me to follow.  Here it is :


Copy code
  1. PImage img;
  2. int increment = 2; // not sure this is needed

  3. void setup() {
  4.   img = loadImage("image01.png");
  5.   size (img.width, img.height);
  6.   image(img, 0, 0, width, height);
  7.   doSort();
  8. }

  9. void doSort() {
  10.   loadPixels();
  11.   print(pixels.length);
  12.   for (int x = 0; x<pixels.length - 1; x++) {
  13.     bubbleSort(pixels);
  14.   }
  15.   updatePixels();
  16. }

  17. void bubbleSort (int x[]) {
  18.   // loop through the array
  19.   for (int i = 0;  i<x.length-1;i++) {
  20.     if (x[i+1] > x[i]) {
  21.       //swap i and j values
  22.       int j = x[i];
  23.       x[i] = x[i+1];
  24.       x[i+1] = j;
  25.     }
  26.   }
  27. }






Sorry, one more thing.  I see that Processing has its own sort() function.  Have you tried it?  I tried the following (and used reverse() because it seems you wanted to sort from brightest to darkest...without reverse(), it gets sorted darkest to brightest):

Copy code
  1. PImage img;

  2. void setup() {
  3.   img = loadImage("image01.png");
  4.   size (img.width, img.height);
  5.   img.pixels = reverse(sort(img.pixels));
  6.   image(img, 0, 0, width, height);
  7. }
Ah, I thought the point was to show the sort in real time, like one sort operation per frame!...

Funny effect:
Copy code
  1. PImage img;
  2.  
  3. void setup()
  4. {
  5.   size(100, 100);
  6.   img = loadImage("G:/Images/map.png");
  7.   image(img, 0, 0);
  8. }
  9.  
  10. void draw()
  11. {
  12.   oneSortStep();
  13. }
  14.  
  15. void oneSortStep()
  16. {
  17.   loadPixels();
  18.   for (int i = 0; i < pixels.length - 1; i++)
  19.   {
  20.     if (blue(pixels[i + 1]) < blue(pixels[i]))
  21.     {
  22.       // Swap pixels
  23.       color c = pixels[i];
  24.       pixels[i] = pixels[i + 1];
  25.       pixels[i + 1] = c;
  26.     }
  27.   }
  28.   updatePixels();
  29. }
I used a small image because it is slow enough!
Thanks Philho, that was cool to see! I have some code that does a similar thing, but quicksort makes it run a little faster :) Changing the increment value can yield some interesting results. 

Copy code
  1. PImage img;
  2. int increment=2;
  3.  
  4. void setup() {
  5.   img = loadImage("image.jpeg");
  6.   size(img.width, img.height);
  7.   image(img, 0, 0);


  8. }
  9. void draw() {
  10.     loadPixels();
  11.   for (int x = 0; x < ((height*width)-(increment+2)); x+=increment) {
  12.     quicksort(pixels, x, x+increment);
  13.   }
  14.   updatePixels();

  15. }
  16.  
  17. int partition(int x[], int left, int right) {
  18.   int i = left;
  19.   int j = right;
  20.   int temp;
  21.   int pivot = x[(left+right)/2];
  22.   while (i <= j) {
  23.     while (x[i] < pivot) {
  24.       i++;
  25.     }
  26.     while (x[j] > pivot) {
  27.       j--;
  28.     }
  29.     if (i <= j) {
  30.       temp = x[i];
  31.       x[i] = x[j];
  32.       x[j] = temp;
  33.       i++;
  34.       j--;
  35.     }
  36.   }
  37.   return i;
  38. }
  39.  
  40. void quicksort(int x[], int left, int right) {
  41.   int index = partition(x, left, right);
  42.   if (left < index - 1) {
  43.     quicksort(x, left, index-1);
  44.   }
  45.   if (index < right) {
  46.     quicksort(x, index, right);
  47.   }
  48. }
Hi aferriss, I have spent a lot of time and energy on sorting things over the last few years, much of it in Processing and with images (see: http://www.jeffreythompson.org/RandomHexadecimalValuesSortedNumerically.php).

A few thoughts after you resolve the tech problems:
+ The color model you use will result in really different results - ie: the way RGB is stored vs hex vs HSB.  Generally, HSB is favored for data vis because it gives clean shifts across the spectrum
+ If you really want to geek out, check out the video " Sorting Out Sorting" - it's a 30+ minute early computer animation of different sorting algorithms.  Super nerdy and I love it!
Hi Jeff, Great stuff! 

I'll experiment with HSB and see what I come up with.

The "Sorting Out Sorting" video is awesome, thanks for sharing this!