We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Sorting pixels by brightness
Page Index Toggle Pages: 1
Sorting pixels by brightness (Read 2534 times)
Sorting pixels by brightness
Apr 28th, 2010, 5:39am
 
Hi,

I'm an beginner when it comes to processing but I think processing might be able to produce what I'm in need of.

I would like to have a function that sort an image's pixels by brightness and then creates a new image out of that.

The new image will most likely look like a gradient.

Anyone who have a good idea how I would be able to accomplish this?
Re: Sorting pixels by brightness
Reply #1 - Apr 28th, 2010, 5:47am
 
Look at pixels[] and sort().
Well, you will need to make another array, using brightness() on each entry of pixels[] before sorting.
Re: Sorting pixels by brightness
Reply #2 - Apr 28th, 2010, 6:35am
 
Thanks, I will give it a go and see if I get anywhere.

A short side question: Is there a way of putting out values while running the code for debugging?
Re: Sorting pixels by brightness
Reply #3 - Apr 28th, 2010, 7:01am
 
Ok, I've been able make a script that almost does what I want it to.
Code:
size(400, 400);
int halfImage = width*height/2;

PImage myImage = loadImage("test.jpg");
image(myImage, 0, 0);
int[] sum = new int[width*height];
loadPixels();
for (int q = 0; q < (width*height); q++)
 {
float value = brightness(pixels[q]);
sum[q] = int(value);

}
sum = sort(sum);

color pink = color(255, 102, 204);
loadPixels();
for (int i = 0; i < (width*height)-width; i++) {

pixels[i] = color(0, 0, sum[i]);
}
updatePixels();


But I now loose color information. How do I get the color information with me in the sorting?
Re: Sorting pixels by brightness
Reply #4 - Apr 28th, 2010, 7:32am
 
Then you probably need to use Arrays.sort(T[] a, Comparator< super T> c) by creating a class to store the color information and making an array of objects of such class and making a Comparator for this class...
I can do the code as I find it interesting, but I don't want to spoil your discovering pleasure if you prefer to do it entirely yourself... Smiley
Re: Sorting pixels by brightness
Reply #5 - Apr 28th, 2010, 7:47am
 
Thanks, I might take you up on that offer, but I give it a go first.
Re: Sorting pixels by brightness
Reply #6 - Apr 28th, 2010, 9:53am
 
I'm getting nowhere, I would be very grateful if you would code this for me.
Re: Sorting pixels by brightness
Reply #7 - Apr 29th, 2010, 2:26am
 
The code was ready to show... Smiley
Code:
PImage niceImage;
SortableColor[] colors;
PImage sortedImage;

void setup()
{
size(500, 500);
noLoop();

niceImage = loadImage("D:/Dev/PhiLhoSoft/images/TestImageS.jpg");

image(niceImage, 0, 0);
}

void draw() {} // Enable events

void mousePressed()
{
niceImage.loadPixels();
int colorNb = niceImage.pixels.length;
colors = new SortableColor[colorNb];
println("Making sortable color array");
for (int i = 0; i < colorNb; i++)
{
colors[i] = new SortableColor(niceImage.pixels[i]);
}
println("Sorting the array");
Arrays.sort(colors, new ColorComparator());
println("Done, making the sorted image");
sortedImage = niceImage.get(); // Clone as quick creation way
sortedImage.loadPixels();
for (int i = 0; i < colorNb; i++)
{
sortedImage.pixels[i] = colors[i].originalColor;
}
sortedImage.updatePixels();
image(sortedImage, 100, 100);
println("Done");
redraw();
}

class SortableColor
{
color originalColor;
float brightness;

SortableColor(color c)
{
originalColor = c;
brightness = brightness(c);
}
}

class ColorComparator implements Comparator<SortableColor>
{
int compare(SortableColor sc1, SortableColor sc2)
{
return int(sc1.brightness - sc2.brightness);
}
}
Re: Sorting pixels by brightness
Reply #8 - Apr 29th, 2010, 2:30am
 
Just tried an interesting variant:
Code:
class SortableColor
{
color originalColor;
float brightness;
float hue;

SortableColor(color c)
{
originalColor = c;
brightness = brightness(c);
hue = hue(c);
}
}

class ColorComparator implements Comparator<SortableColor>
{
int compare(SortableColor sc1, SortableColor sc2)
{
if (sc1.brightness != sc2.brightness)
return int(sc1.brightness - sc2.brightness);
// Same brightness, we do a secondary sort on hue
return int(sc1.hue - sc2.hue);
}
}
Re: Sorting pixels by brightness
Reply #9 - Apr 29th, 2010, 2:52am
 
I might be missing something obvious but the code won't run in my processing.

I get this error message:

processing.app.debug.RunnerException: expecting LCURLY, found '<'
     at processing.app.Sketch.preprocess(Sketch.java:1351)
     at processing.app.Sketch.preprocess(Sketch.java:1204)
     at processing.app.Sketch.build(Sketch.java:1590)
     at processing.app.Sketch.build(Sketch.java:1575)
     at processing.app.Editor$DefaultRunHandler.run(Editor.java:1656)
     at java.lang.Thread.run(Thread.java:637)

Re: Sorting pixels by brightness
Reply #10 - Apr 29th, 2010, 4:54am
 
Sorry, I used the new Java 1.5 syntax as allowed by Processing 018x
If you still use Processing 1.1 or below, you have to change that slightly.
Code:
class ColorComparator implements Comparator
{
int compare(Object o1, Object o2)
{
SortableColor sc1 = (SortableColor) o1;
SortableColor sc2 = (SortableColor) o2;
if (!bSortOnHue || sc1.brightness != sc2.brightness)
return int(sc1.brightness - sc2.brightness);
// Same brightness, we do a secondary sort on hue
return int(sc1.hue - sc2.hue);
}
}

// Using to see the two methods:
boolean bSortOnHue;
void mousePressed()
{
bSortOnHue = mouseButton == RIGHT;
// [...]
}
Re: Sorting pixels by brightness
Reply #11 - May 7th, 2010, 4:27am
 
First of all I would exuce myself for being late with thanking you  for the script. Now when I had some time to run it it works fabulously. Especially after the hue sorting was introduced.

But the hue sorting seems flawed for some pictures though.

As in http://www.antonlamberg.se/28.png. Where a group of pixels with obvious higher hue value get placed at the bottom. Any ideas. I tried to debug it myself. But your script locks totally logical and I can't really see why it's happening.
Re: Sorting pixels by brightness
Reply #12 - May 7th, 2010, 4:54am
 
It seems like it sorted the value backward when it came to the hue.

Fixed it by switching:
Code:
 return int(sc1.hue - sc2.hue); 


to
Code:
 return int(sc2.hue - sc1.hue); 




It doesn't make sense to me, but the output seems to be correct.
Re: Sorting pixels by brightness
Reply #13 - May 7th, 2010, 5:21am
 
Don't forget hues are sorted only for identical brightness (ie. if you have n pixels of same brightness, it will then sort them by hue).
You can do the reverse too, sorting by hue, then for same hue, by brightness.

About the switch: as long as you get the result you want, it is OK! Smiley
Page Index Toggle Pages: 1