Contrast Stretching Dengan Java
Contrast Stretching Dengan Java
java.awt.image.BufferedImage;
java.awt.image.WritableRaster;
java.io.File;
java.io.IOException;
javax.imageio.ImageIO;
/**
*
* @author Windu Purnomo
*/
public class HistogramEq{
public int[][] getRGB(File file) throws IOException{
BufferedImage buf = ImageIO.read(file);
int width = buf.getWidth();
int height = buf.getHeight();
int size = width * height;
int c = 0, counter = 0;
int [][] rgb = new int[3][size];
for(int i = 0; i<width; i++){
for(int j = 0; j<height ; j++){
c = buf.getRGB(i,j);
rgb[0][counter] = (c&0x00ff0000)>>16;
rgb[1][counter] = (c&0x0000ff00)>>8;
rgb[2][counter] = c&0x000000ff;
counter++;
}
}
return rgb;
}
public float [] RGB2GS (File file) throws IOException{
BufferedImage buf = ImageIO.read(file);
int width = buf.getWidth();
int height = buf.getHeight();
int size = width * height;
int c = 0, counter = 0, r, g, b;
float [] grayScale = new float[size];
for(int i = 0; i<width; i++){
for(int j = 0; j<height ; j++){
c = buf.getRGB(i,j);
r = (c&0x00ff0000)>>16;
g = (c&0x0000ff00)>>8;
b = c&0x000000ff;
grayScale[counter] = (float) (0.3 * r + 0.59 * g + 0.11 * b);
counter++;
}
}
return grayScale;
}
public int [] histogram(float[] grayScale){
int [] pixNum = new int [256];
int size = grayScale.length;
for(int c = 0; c<256; c++){
int sum = 0;
for(int i = 0; i<size; i++) if(grayScale[i]==c) sum++;
pixNum[c] = sum;
}
return pixNum;
}
//CDF = Cumulative Distributif Function
public int [] getCDF(int [] histogram){
int [] cdf = new int [256];
int cum = 0;
for(int i = 0; i<256; i++){
cum += histogram[i];
cdf[i] = cum;
}
return cdf;
}
public int getMinCDF(int [] cdf){
int minCDF = 257;
for(int i = 0; i<256; i++){
if(cdf[i]<minCDF && cdf[i]!=0) minCDF = cdf[i];
}
return minCDF;
}
public int getMaxCDF(int [] cdf){
int maxCDF = 0;
for(int i = 0; i<256; i++){
if(cdf[i]>maxCDF) maxCDF = cdf[i];
}
return maxCDF;
}
Oleh :