Image Enhancment by Histogram Equalization in Java


Tertantang untuk meningkatkan akurasi hasil penelitian (Tugas Akhir), saya coba perbaiki kualitas citra input (praprocess) dengan menggunakan histogram equalization. Konsepnya tidak terlalu rumit, agar sebaran nilai tiap pixel pada tiap citra tidak terlalu berbeda jauh.

Berikut ini adalah hasil dari proses image enhancment dengan menggunakan Histogram Equalization. Sebelah kiri merupakan citra awal, sedangkan sebelah kanan adalah citra setelah proses enhancment.

Berikut ini adalah program Histogram Equalization dengan menggunakan Java (download di sini):

package windu.process;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import 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;
	}

	public float[] equalization(int [] cdf, int pictSize){
		int min = getMinCDF(cdf);
		float e [] = new float[256];
		System.out.println("minimum: "+min);
		System.out.println("pictSize: "+pictSize);
		for(int i = 0; i<256; i++){
			e[i] = (float)((((float)cdf[i]-min)/(float)pictSize)*255);
		}
        for(int i = 0; i<256; i++){
            if(e[i]<0) e[i]=0;
            if(e[i]>255) e[i]=255;
        }
		return e;
	}

	public float [] picEqualized(float [] grayScale, float [] equalization, int w, int h){
		int size = w*h;
		float [] newGS = new float[size];
		int counter = 0;
		for(int i = 0; i<w; i++){
			for(int j = 0; j<h; j++){
				newGS [counter] = equalization[(int)grayScale[counter]]; //convert
				counter++;
			}
		}
		return newGS;
	}

	public void drawImage(float [] newGS, int w, int h)throws IOException{
		int size = w*h;
		int counter = 0;
		BufferedImage im = new BufferedImage(w,h,BufferedImage.TYPE_BYTE_GRAY);
		WritableRaster raster = im.getRaster();
		for(int i = 0; i<w; i++){
			for(int j = 0; j<h; j++){
				raster.setSample(i, j, 0, newGS[counter]);
				counter++;
			}
		}
		ImageIO.write(im, "PNG", new File("noNoisy.png"));
	}

	public static void main (String args[]) throws IOException{
		HistogramEq he = new HistogramEq();
		File file = new File("noisy.jpg");
		BufferedImage x = ImageIO.read(file);
		int width = x.getWidth();
		int height = x.getHeight();
		int size = width * height;
		float grayScale [] = new float[size];
		int histogram [] = new int[256];
		int cdf [] = new int[256];
		float equalized [] = new float[256];
		float picEqualized [] = new float[size];

		grayScale = he.RGB2GS(file);
		histogram = he.histogram(grayScale);
		cdf = he.getCDF(histogram);
		equalized = he.equalization(cdf, size);
		picEqualized = he.picEqualized(grayScale, equalized, width, height);
		he.drawImage(picEqualized, width, height);
		int counter = 0;
	}
}

Note:

  • COMPILE : javac HistogramEq.java
  • RUNNING : java HistogramEq
  • File input ada pada baris 135
  • Sedangkan nama file output ada pada baris ke 130
  • Silahkan sesuaikan nama file input dan output sesuai dengan kebutuhan
  • Pastikan file java dan file citra berada dalam satu folder

About windupurnomo

I'm interested in programming. I am active with several programming such as Java, C #, C, JavaScript, HTML. I'm also develop desktop application (Java Swing), Mobile Application (Android), and Web programming (ASP MVC).
This entry was posted in Image Processing and tagged , , , , , , . Bookmark the permalink.

5 Responses to Image Enhancment by Histogram Equalization in Java

  1. rinanza says:

    thank you mas… haha mangstab

  2. jywebmaster@gmail.com says:

    mas kox setelah di running hitogram nya gx muncul… mohon bantuannya..

  3. ynmly says:

    thanks a lot….but it isnt working in colourfull images

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s