Solved

# Contrast and Sharpness Code using JAI

Posted on 2006-04-08
Medium Priority
1,130 Views
Plz tell me code of increase and decrease contrast and sharpen of image using JAI
i have write code for contrast it increase contrast accuratly but decrease code is not giving desired result
0
• 2

LVL 13

Expert Comment

ID: 16411550
You basically need to use a JAILookuptable, here's a function that helps you adjust the contrast and brightness of a picture:

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.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;
}
int average = (int) sum / mean.length;

// Create the lookup table based on the average mean
byte[][] lut = new byte[3][256];
for (int i = 0; i < 256; i++ )
{
lut[0] = clamp((average + (int)((i - average) * contrast)) + red + (int)brightness);
lut[1] = clamp((average + (int)((i - average) * contrast)) + green + (int)brightness);
lut[2] = clamp((average + (int)((i - average) * contrast)) + blue + (int)brightness);
}
LookupTableJAI lookup = new LookupTableJAI(lut);
// Setup the parameter block for the lookup operation
ParameterBlock pb = new ParameterBlock();

// Calculate processing time and report
long endTime = (new java.util.Date()).getTime();
System.out.println("Time to set colors was " + (endTime - beginTime) + " ms");

// Return the resulting image
return JAI.create("lookup", pb, null);
}
0

LVL 13

Accepted Solution

Kelvin_King earned 1500 total points
ID: 16411568

public void processBrightContrast(int ipParam, Image pImage){

Image mImage = pImage;
//Create parameter block
// java.awt.image.renderable.ParameterBlock mPB = new java.awt.image.renderable.ParameterBlock();
//Add image source to parameter block

byte lutData[];
lutData = new byte[256];
for(int j = 0; j < 256; j++)
lutData[j] = clampByte(j + ipParam);

javax.media.jai.LookupTableJAI mLookup = new javax.media.jai.LookupTableJAI(lutData);

java.awt.image.renderable.ParameterBlock pb = new java.awt.image.renderable.ParameterBlock();
pb.add(mImage); // mImage is a normal image here
PlanarImage im = (PlanarImage)JAI.create("awtImage", pb);

try {
// Process the brightness/ darkness
javax.media.jai.RenderedOp mRenOp = javax.media.jai.JAI.create("lookup", im); //mPB, null); <---- this is where the debug crashes

src = this.toImage(mRenOp.getAsBufferedImage());
}
catch(IllegalArgumentException e)
{
System.out.println(e);
}
}

http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/JAITOC.fm.html
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input: