APPENDIX TAMPILAN GUI CONTRAST STRETCHING DAN HISTOGRAM EQUALIZATION

Full text

(1)

APPENDIX

TAMPILAN GUI CONTRAST STRETCHING DAN HISTOGRAM EQUALIZATION

private void formWindowOpened(java.awt.event.WindowEvent evt) { JFileChooser file = new JFileChooser();

file.setCurrentDirectory(new

File(System.getProperty("user.home")));

FileNameExtensionFilter filter = new

FileNameExtensionFilter("*.Images", "jpg","gif","png");

file.addChoosableFileFilter(filter);

int result = file.showSaveDialog(null);

if(result == JFileChooser.APPROVE_OPTION){

File selectedFile = file.getSelectedFile();

String path = selectedFile.getAbsolutePath();

}

else if(result == JFileChooser.CANCEL_OPTION){

System.out.println("No File Select");

}

originalPanel1.LoadImage(""+file.getSelectedFile());

//originalPanel1.LoadImage("/home/gabriel/Desktop/Skripsi/

girl.jpg");

originalPanel1.repaint();

process2.LoadImage(""+file.getSelectedFile());

process2.repaint();

psnr1.LoadImage("/home/gabriel/Desktop/Skripsi/girl.jpg");

psnr1.msepsnr();

hEpanel1.LoadImage(""+file.getSelectedFile());

}

BUTTON BROWSE, CONTRAST STRETCHING, HISTOGRAM EQUALIZATION DAN PSNR

private void

contraststretchingActionPerformed(java.awt.event.ActionEvent evt) {

process2.LoadImage("/home/gabriel/Desktop/Skripsi/CSgirl.jp g");

process2.ContrasStretchh();

}

A

(2)

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

psnr1.msepsnr();

// jTextField1.setText("",msepsnr());

}

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {

try {

// TODO add your handling code here:

//hEpanel1.LoadImage("/home/gabriel/Desktop/Skripsi/He girl.jpg");

hEpanel1.LoadImage("/home/gabriel/Desktop/Skripsi/CSHE/

HEtig.jpg");

hEpanel1.HE();

} catch (IOException ex) {

Logger.getLogger(ContrastDemo.class.getName()).log(Lev el.SEVERE, null, ex);

}

}

private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {

JFileChooser file = new JFileChooser();

file.setCurrentDirectory(new

File(System.getProperty("user.home")));

FileNameExtensionFilter filter = new

FileNameExtensionFilter("*.Images", "jpg","gif","png");

file.addChoosableFileFilter(filter);

int result = file.showSaveDialog(null);

if(result == JFileChooser.APPROVE_OPTION){

File selectedFile = file.getSelectedFile();

String path = selectedFile.getAbsolutePath();

}

else if(result == JFileChooser.CANCEL_OPTION){

System.out.println("No File Select");

}

originalPanel1.LoadImage(""+file.getSelectedFile());

}

private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {

psnr21.msepsnr();

}

B

(3)

CONTRAST STRETCHING PROGRAMS : LOAD IMAGE, FORMULA, IMAGE OUTPUT

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.io.File;

import java.io.IOException;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.imageio.ImageIO;

public class Process extends javax.swing.JPanel { BufferedImage img;

public Process() { initComponents();

img = null;

}

private void initComponents() {

javax.swing.GroupLayout layout = new

javax.swing.GroupLayout(this);

this.setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.LEADING)

.addGap(0, 400, Short.MAX_VALUE) );

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.LEADING)

.addGap(0, 300, Short.MAX_VALUE) );

}

public void LoadImage(String FilePath) { try {

img = ImageIO.read(new File(FilePath));

} catch (IOException ex) {

Logger.getLogger(OriginalPanel.class.getName()).log(Le vel.SEVERE, null, ex);

} }

public void ContrasStretchh() { new Thread(new Runnable() { public void run() {

C

(4)

int width = img.getWidth();

int height = img.getHeight();

int min = 100;

int max = 200;

System.out.println("width=" + width + " height=" + height);

System.out.println("min=" + min + " max=" + max);

try {

int[] r = new int[width * height];

int[] g = new int[width * height];

int[] b = new int[width * height];

int[] e = new int[width * height];

int[] data = new int[width * height];

img.getRGB(0, 0, width, height, data, 0, width);

for (int i = 0; i < (height * width); i++) { r[i] = (int) ((data[i] >> 16) & 0xff);

g[i] = (int) ((data[i] >> 8) & 0xff);

b[i] = (int) (data[i] & 0xff);

r[i] = (int) (1.0 * (r[i] - min) / (max - min) * 255);

g[i] = (int) (1.0 * (g[i] - min) / (max - min) * 255);

b[i] = (int) (1.0 * (b[i] - min) / (max - min) * 255);

if (r[i] > 255) { r[i] = 255;

}

if (g[i] > 255) { g[i] = 255;

}

if (b[i] > 255) { b[i] = 255;

}

if (r[i] < 0) { r[i] = 0;

}

if (g[i] < 0) { g[i] = 0;

}

if (b[i] < 0) { b[i] = 0;

}

D

(5)

e[i] = (r[i] << 16) | (g[i] << 8) | b[i];

}

img.setRGB(0, 0, width, height, e, 0, width);

ImageIO.write(img, "jpg" ,new File("CStiger.jpg"));

} catch (Exception e) {

System.err.println("Error: " + e);

Thread.dumpStack();

}

repaint();

}

}).start();

}

public void paint(Graphics g) { super.paint(g);

if (img != null) {

g.drawImage(img, 0, 0, null);

} } }

HISTOGRAM EQUALIZATION PROGRAMS : LOAD IMAGE, FORMULA, IMAGE OUTPUT

import java.awt.Graphics;

import java.awt.image.BufferedImage;

import java.awt.image.BufferedImage;

import java.awt.image.WritableRaster;

import java.io.File;

import java.io.IOException;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.imageio.ImageIO;

public class HEpanel extends javax.swing.JPanel { BufferedImage buf;

public HEpanel() { initComponents();

buf=null;

}

E

(6)

private void initComponents() {

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);

this.setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.LEADING)

.addGap(0, 400, Short.MAX_VALUE) );

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.LEADING)

.addGap(0, 300, Short.MAX_VALUE) );

}

public void LoadImage(String FilePath){

try {

buf=ImageIO.read(new File(FilePath));

} catch (IOException ex) {

Logger.getLogger(OriginalPanel.class.getName()).log(Leve l.SEVERE, null, ex);

} }

public float [] getRGB(File file) throws IOException{ //membaca rgb

BufferedImage buf = ImageIO.read(file);

int width = buf.getWidth(); //lebar int height = buf.getHeight(); //tinggi int size = width * height; //ukuran gambar int c = 0, counter = 0,r,g,b;

float [] rgb = 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; //r //replace RGB value with avg

g= (c&0x0000ff00)>>8;//g b = c&0x000000ff;//b

//System.out.println("rgb: " + r + ", " + g + ",

" + b);

counter++;

} }

return rgb;

}

//grYSCALE

public float [] GRAYSCALE (File file) throws IOException{

//mengubah grayscale

BufferedImage buf = ImageIO.read(file);

F

(7)

int width = buf.getWidth();

int height = buf.getHeight();

int size = width * height;

//System.out.println("size="+size);

int c = 0, counter = 0, r, g, b;

float [] grayScale = new float[size];

//convert to grayscale

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) (r+g+b)/3;//calculate average

//System.out.println("rgb: " + r + ", " + g + ",

" + b);

counter++;

} }

return grayScale;

}

public int [] histogram(float[] grayScale){ //panjang histogram array 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;

//System.out.println("picNum:"+pixNum);

}

return pixNum;

}

//CDF = Cumulative Distributif Function = menghitung jumlah freq pixel

//Ukuran gambar: PxL, tingkat abu-abu dari 0 hingga 255, array i ukuran 256 dan inisialisasi dengan 0.

public int [] getCDF(float[] grayScale,int [] histogram){

int [] cdf = new int[256];

double [] Cp = new double [256];

double [] floorRounding = new double [256];

int cum = 0;

double kali=0;

double frek =0;

double probability =0;

double pembulatan=0;

double size = grayScale.length;

for(int i = 0; i<256; i++){

probability = histogram[i]/size;

G

(8)

frek = histogram[i];

cum += frek;//frek cdf[i] = cum;

kali = cdf[i]*20;

Cp[i]= kali;

pembulatan = cdf[i]*20;

floorRounding[i]= pembulatan;

System.out.println("pixel ke:"+i);

// System.out.println("frek:"+frek);

//System.out.println("probability:"+probability);

System.out.println("probability:"+String.format("%2.6f

",probability));

//System.out.println("total frek:"+size);

System.out.println("cumulative:"+cum);

System.out.println("CP:"+kali);

System.out.println("Pembulatan:"+String.format("%2.0f",p embulatan));

}

return cdf;

}

public int getMinCDF(int [] cdf){ //mendapatkan hasil minimum cdf dari intensitas pixel

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){ //mendapatkan hasil max cdf dari intensitas pixel

int maxCDF = 0;

for(int i = 0; i<256; i++){

if(cdf[i]>maxCDF && cdf[i]!=256) maxCDF = cdf[i];

}

return maxCDF;

}

public float[] ekualisasi(int [] cdf, int size,float []

probabilities,float [] cumulative){

int min = getMinCDF(cdf);

int max = getMaxCDF(cdf);

float e [] = new float[256];

// System.out.println("minimum: "+min);

//System.out.println("maximum: "+max);

//System.out.println("pictSize: "+size);

for(int i = 0; i<256; i++){

H

(9)

e[i] = (float)((((float)cdf[i]-min)/(float)size)*255);

//menghitung 0-255 min=1 pic size=jumlah freq pixel cdf[i] dari intensitas terendah ke terbesar

}

for(int i = 0; i<256; i++){

if(e[i]<0) e[i]=0; //ambang batas if(e[i]>255) e[i]=255;

}

return e;

}

public float [] citraEkualisasi(float [] grayScale, float []

ekualisasi, int w, int h){ //mendapatkan hasil ekualisasi 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] = ekualisasi[(int)grayScale[counter]]; //convert

counter++;

} }

return newGS;

}

public void buatGambar(float [] newGS, int w, int h)throws IOException{

int size = w*h;

int counter = 0;

//int c=0;

BufferedImage im = new BufferedImage(w,h,BufferedImage.TYPE_BYTE_GRAY); // membuat image baru hasil ekualisasi

WritableRaster raster = im.getRaster();

for(int i = 0; i<w; i++){

for(int j = 0; j<h; j++){

//System.out.println("x,y: " + j + ", " + i);baca posisi

// float [] grayscale= im.getRGB(j, i);

// printPixelRGB(pixel);//baca rgb // System.out.println("");

raster.setSample(i, j, 0, newGS[counter]);

counter++;

}

ImageIO.write(buf, "jpg", new File("/home/gabriel/Desktop/Skripsi/CSHE/HEgirl9.jpg"));

}

//output }

public void HE() throws IOException{ //cetak HE he = new HE();

I

(10)

File file = new File("/home/gabriel/Desktop/Skripsi/CSHE/girl.jpg");//input

BufferedImage x = ImageIO.read(file);

int width = x.getWidth();

int height = x.getHeight();

int size = width * height;

float getrgb [] = new float[size];

float grayScale [] = new float[size]; //param int histogram [] = new int[256];

int cdf [] = new int[256];

float probabilities [] =new float[256];

float cumulative [] =new float[256];

float ekualisasi [] = new float[256];

float citraEkualisasi [] = new float[size];

//menampilkan/cetak hasil getrgb = he.getRGB(file);

grayScale = he.GRAYSCALE(file);

histogram = he.histogram(grayScale);

cdf = he.getCDF(histogram);

probabilities=he.getProbabilities(cdf,size);

cumulative=he.getCumulative(cdf,probabilities,size);

ekualisasi = he.ekualisasi(cdf, size,probabilities,cumulative);

citraEkualisasi =he.citraEkualisasi(grayScale, ekualisasi, width, height);

he.buatGambar(citraEkualisasi, width, height);

int counter = 0;

}

public void paint(Graphics g){

super.paint(g);

if(buf!=null){

g.drawImage(buf, 0, 0, null);

} } }

MSE AND PSNR PROGRAMS : LOAD IMAGE, FORMULA, CALCULATE IMAGE QUALITY RESULT

package contrasstretch;

import java.awt.Component;

import java.awt.Graphics;

import java.awt.image.*;

import java.io.*;

import java.util.*;

import java.awt.image.BufferedImage;

import java.io.File;

import java.util.logging.Level;

import java.util.logging.Logger;

import javax.imageio.ImageIO;

import java.io.IOException;

J

(11)

import java.awt.image.Raster;

import java.lang.*;

public class psnr extends javax.swing.JPanel { BufferedImage img,img2;

public psnr() {

initComponents();

img=null;

img2=null;

}

private void initComponents() {

javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);

this.setLayout(layout);

layout.setHorizontalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.LEADING)

.addGap(0, 400, Short.MAX_VALUE) );

layout.setVerticalGroup(

layout.createParallelGroup(javax.swing.GroupLayout.Ali gnment.LEADING)

.addGap(0, 300, Short.MAX_VALUE) );

}// </editor-fold>

public void LoadImage(String FilePath){

try {

img=ImageIO.read(new File(FilePath));

} catch (IOException ex) {

Logger.getLogger(OriginalPanel.class.getName()).log(Leve l.SEVERE, null, ex);

} }

public void msepsnr(){

try{

File f1 = new File("/home/gabriel/Desktop/Skripsi/coc3.jpg");

File f2 = new File("/home/gabriel/Desktop/Skripsi/pubg.jpg");

img =ImageIO.read(f1);

img2 =ImageIO.read(f2);

} catch (Exception e) { System.err.println("Error: " + e);

Thread.dumpStack();

K

(12)

}

assert (img.getType() == img2.getType()//mendefinisakan perintah boolean && img.getHeight() == img2.getHeight()

&& img.getWidth() == img2.getWidth());

double mse = 0;

int width = img.getWidth();

int height = img.getHeight();

Raster r1 = img.getRaster(); //mendapatkan pixel Raster r2 = img2.getRaster();

for (int j = 0; j < width; j++) { for (int i = 0; i < height; i++) {

mse += Math.pow(r1.getSample(j, i, 0) - r2.getSample(j, i, 0), 2);

} }

System.out.println("MSE = " + mse);

mse /= (double) (width * height);

double psnr = Math.log10(Math.pow(255, 2) / mse); //angka,pangkat math pow

System.out.println("PSNR = " + psnr);

//jLabel2.setText(String.format(" " ,psnr));

} }

L

(13)

Figure

Updating...

References

Updating...

Related subjects :