Code First:
public void contrastImage(BufferedImage img,float clip){//strecth luminance, adjust RGB proportionately
int N=256;//bins
makeHistogram(img,N);
int height=img.getHeight();
int width=img.getWidth();
float min,max,sum=0,total=height*width;
long index,mindex=0,maxdex=N;
for(index=0;index<N;index++){
sum+=hist[(int)index];
if((mindex==0)&&((sum/total)>clip)) mindex=index;
if((maxdex==N)&&((sum/total)>(1-clip))) maxdex=index-1;
}
min=255.0f*mindex/N;
max=255.0f*maxdex/N;
float scale;
scale=(255.0f/(max-min));
int result[][]=new int[width][height];
for(int row=0;row<height;row++){
for(int col=0;col<width;col++){
result[col][row]=img.getRGB(col, row);
Color c=new Color(result[col][row]);
float red=c.getRed();
float blue=c.getBlue();
float green=c.getGreen();
red=scale*(red-min);
blue=scale*(blue-min);
green=scale*(green-min);
if(red>255)red=255;
if(red<0)red=0;
if(blue>255)blue=255;
if(blue<0)blue=0;
if(green>255)green=255;
if(green<0)green=0;
c=new Color((int)red,(int)green,(int)blue);
img.setRGB(col,row,c.getRGB());
}
}
}
Explanation:
In processes where we need to contrast an image, the result is acquired by stretching the histogram curve, forming a comb-like graph. Thus, we first get the max and min intensities and then find the scale factor. Scaling is done for each color channel.
The "clip" helps the user to change the amount of contrast. For example, 0.0025 means 25%.
<<PREV NEXT>>