The Source for Java Technology Collaboration

Home » java.net Forums » Java Desktop Technologies » JAI

Thread: Contrast Example

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 8 - Last Post: Apr 19, 2006 7:48 AM by: husenful
foxdeath

Posts: 8
Contrast Example
Posted: Mar 7, 2006 10:30 PM
  Click to reply to this thread Reply

I am a semi-newbie to JAI. I have been pouring over all of the examples and searching the internet like a madman. Does anyone have a clear example of how to do contrast? I have heard a lot of different methods (lookup table multiplying bands by constant, the rescale operation, etc). I am basically looking for something simple that runs very fast. I need to provide a slidebar control that will allow a user to adjust the contrast easily.

If someone could help me out here I would be eternally grateful.

Thank you.

davince

Posts: 9
Re: Contrast Example
Posted: Mar 8, 2006 6:13 AM   in response to: foxdeath
  Click to reply to this thread Reply

A very, but not extremly valid option for enhancing the contrast is by simply taking the average of the RGB values and doing the following

a = (r + g + b) / 3
c = floating point value between 0 and finite, though I'd set 2 or 3 as top, where 1 = no change

r = a + (r - a) * c;
g = a + (g - a) * c;
b = a + (b - a) * c;

thats all. By specifying c > 1 you'll get more color, and < 1 it'll turn more grayish.

hope it helps.

Two variations:
- setting 'a' as the middle of your color range, so if your color ranges between 0 and 1, you'd set 'a' to 0.5. While if it was between 0 and 255, it'd be 127.
- setting a to the average of several neigbouring pixels. This would be more like a local contrast increase.

Both these options also work for grayscale, while the first mentioned does not.

edit: Added the 2 variations.

ahmetaa

Posts: 21
Re: Contrast Example
Posted: Mar 8, 2006 7:58 AM   in response to: foxdeath
  Click to reply to this thread Reply

i think you can use the "rescale" method. it directly affects the contrast value.

foxdeath

Posts: 8
Re: Contrast Example
Posted: Mar 8, 2006 11:08 AM   in response to: foxdeath
  Click to reply to this thread Reply

Thank you very much, that was exactly what I was looking for. Here is the final code that I came up with. It is a very simple way of implementing contrast that runs very quickly. I hope others can use it:

public static PlanarImage setColor(PlanarImage source,
int red, int green, int blue,
float contrast,
float brightness)
{
// Time the process for performance
long beginTime = (new java.util.Date()).getTime();

// Setup the parameter block for the source image and
// the three parameters for the mean operation
ParameterBlock mpb = new ParameterBlock();
mpb.addSource(source); // The source image
mpb.add(null); // null ROI means whole image
mpb.add(1); // check every pixel horizontally
mpb.add(1); // check every pixel vertically
// Perform the mean operation on the source image
PlanarImage meanImage = JAI.create("mean", mpb, null);
// Retrieve the mean pixel value
double[] mean = (double[])meanImage.getProperty("mean");
// Average the mean of all bands
double sum = 0.0D;
for (int i=0; i < mean.length; i++)
{
sum += mean]]]]]]]]]]

davince

Posts: 9
Re: Contrast Example
Posted: Mar 8, 2006 3:19 PM   in response to: foxdeath
  Click to reply to this thread Reply

Often people add the brightness first and then caculate the contrast. Try it. You'll notice that when you increase the brightness by 20% and the contrast by 40% that it will get really more depth, while if you do the contrast first and then the brightness it'll just get foggy. Anyway, I'm glad I helped.

yleguern

Posts: 2
Re: Contrast Example
Posted: Apr 19, 2006 2:01 AM   in response to: foxdeath
  Click to reply to this thread Reply

Todd,

Thanks for your code.
What's the clamp method you're referring to ?

Thanks,
Yann.

foxdeath

Posts: 8
Re: Contrast Example
Posted: Apr 19, 2006 5:48 AM   in response to: yleguern
  Click to reply to this thread Reply

The clamp method just makes sure the value is an integer between 0 and 255 and then converts it to a byte. Thats all... Glad the code helped. I appreciated the help that I received and that is why I return the favor my posting my code.

Enjoy!

yleguern

Posts: 2
Re: Contrast Example
Posted: Apr 19, 2006 6:34 AM   in response to: foxdeath
  Click to reply to this thread Reply

Ok, now I understand : all occurrences of arrays : [ i] was interpreted as "Italic" by the forum and disappeared from the source.

thanks again

husenful

Posts: 3
Re: Contrast Example
Posted: Apr 19, 2006 7:48 AM   in response to: foxdeath
  Click to reply to this thread Reply

the following example works also very fine and returns results incl. brightness-adjustment, very similar to photoshop "Brightness/Kontrast"...


PlanarImage contrast(PlanarImage myImage, int level, float boost) {

long beginTime = (new java.util.Date()).getTime();

// pre-adjust Brightness to set a centre for continuative contrast-modifications
if (level != 0) {
try {
if (level < -255) level = -255;
if (level > 255) level = 255;
double[] constants = {(float)level, (float)level, (float)level};
ParameterBlock pb = new ParameterBlock();
pb.addSource(myImage);
pb.add(constants);
myImage = JAI.create("addconst", pb, null);
} catch (Exception e) {
e.printStackTrace();
return myImage;
}
}

// start contrast
try {

int numBands = myImage.getNumBands();

// bp is used to compress or pull colorrange
// bp = BoostPixel ;)
float[][][] bp = new float[numBands][2][];


// decrease contrast
if (boost < 0) {
if (boost < -127.5F) boost = -127.5F;
boost = 127.5F + boost;

float innerMax = (255.0F / ]]]]]] 127.5F) boost = 127.5F;
boost = 127.5F - boost;

float innerMax = (255.0F / 2.0F) + boost;
float innerMin = (255.0F / 2.0F) - boost;

for (int i = 0; i < numBands; i++) {
bp[!! i !!][0] =]]]]




 XML java.net RSS