Membuat Thresholding Citra Dengan Pemrograman Java NetBeans



Thresholding adalah proses mengubah citra berderajat keabuan menjadi citra biner atau hitam putih sehingga dapat diketahui daerah mana yang termasuk obyek dan background dari citra secara jelas. Citra hasil thresholding biasanya digunakan lebih lanjut untuk proses pengenalan obyek serta ekstraksi fitur.

Langkah-langkah membuat pemrograman Thresholding Citra sebagai berikut:


Buat beberapa class java yang akan digunakan.

ImageGrayscale.java

ImageGrayscale.java
package image;

import java.awt.Image;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorConvertOp;

public class ImageGrayscale {
   
    private Image originalImage;
    private int imageHeight,  imageWidth;
   
   
    public ImageGrayscale(Image originalImage) {
        this.originalImage = originalImage;
        imageWidth = this.originalImage.getWidth(null);
        imageHeight = this.originalImage.getHeight(null);
    }
    public BufferedImage grayOut() {
        BufferedImage bi = ImageUtil.toBufferedImage(originalImage);
        ColorConvertOp colorConvert = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_GRAY), null);
        return colorConvert.filter(bi, bi);
    }
}

ImageResizer.java

ImageResizer.java
package image;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

public class ImageResizer {
    private Image sourceImage;
    private int sourceImageWidth,  sourceImageHeight;
   
    public ImageResizer() {
    }
    public ImageResizer(Image sourceImage) {
        this.sourceImage = sourceImage;
        sourceImageWidth = sourceImage.getWidth(null);
        sourceImageHeight = sourceImage.getHeight(null);
    }
   
    public BufferedImage resizeTo(int desiredWidth, int desiredHeight, Color bgColor) {
        BufferedImage targetImage = new BufferedImage(desiredWidth, desiredHeight, BufferedImage.TYPE_INT_RGB);
        Graphics graphics = targetImage.getGraphics();
        graphics.setColor(bgColor);
        graphics.fillRect(0, 0, desiredWidth, desiredHeight);
       
        float ratio = 0;
       
        float targetImageRatio = (float) desiredWidth / (float) desiredHeight;
        float sourceImageRatio = (float) sourceImageWidth / (float) sourceImageHeight;
        int x = 0, y = 0;
        int newWidth = 0, newHeight = 0;
       
        if (targetImageRatio < sourceImageRatio) {
            ratio = (float) desiredWidth / (float) sourceImageWidth;
        } else {
            ratio = (float) desiredHeight / (float) sourceImageHeight;
        }
       
        newWidth = (int) (sourceImageWidth * ratio);
        newHeight = (int) (sourceImageHeight * ratio);
        x = (desiredWidth - newWidth) / 2;
        y = (desiredHeight - newHeight) / 2;
       
        Image tempImage = sourceImage.getScaledInstance(newWidth, newHeight, 0);
        graphics.drawImage(tempImage, x, y, null);
       
        return targetImage;
    }
}

ImageFilter.java

ImageFilter.java
public class ImageFilter {
    private Image originalImage;
    private int imageHeight,  imageWidth;

    public ImageFilter() {
    }
   

    public ImageFilter(Image originalImage) {
        this.originalImage = originalImage;
        imageWidth = this.originalImage.getWidth(null);
        imageHeight = this.originalImage.getHeight(null);
    }
   
    public BufferedImage threshold(int threshold) {
        BufferedImage bufferedImage = ImageUtil.toBufferedImage(originalImage);
       
        for (int i = 0; i < imageHeight; i++) {
            for (int j = 0; j < imageWidth; j++) {
                int rgb = bufferedImage.getRGB(j, i);
                bufferedImage.setRGB(j, i, thresholdPixelOp(rgb, threshold));
              
            }
        }
        return bufferedImage;
    }
   
    private int thresholdPixelOp(int rgb, int threshold) {
        int alpha = rgb & (0xFF << 24);
        int r = (rgb >> 16) & 0xFF;
        int binary = (r >= threshold) ? 0 : 255;
       
        return alpha | binary << 8 | binary << 16 | binary;
    }
}


Setelah beberapa class java sudah dibuat, kemudian buat class GUI menggunakan JFrame Form

Berikut ini potongan program GUI yang digunakan

frmGambar.java
public class frmGambar extends javax.swing.JFrame {

    private File file = null;
    private File dir = null;
    private Image image = null;

private void menuPilihActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        JFileChooser fileChooser = new JFileChooser();
        System.out.println("filechoser :" + fileChooser.getSelectedFiles());
        if (file != null) {
            fileChooser.setCurrentDirectory(file);
        }
        int option = fileChooser.showOpenDialog(getContentPane());
        System.out.println("option  :" + option + " -- " + JFileChooser.APPROVE_OPTION);
        if (option == JFileChooser.APPROVE_OPTION) {
            file = fileChooser.getSelectedFile();

            System.out.println("file :" + file);
            try {
                image = ImageIO.read(file);
            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, ex.getMessage(), "Exception", JOptionPane.ERROR_MESSAGE);
            }

            image = new ImageResizer(image).resizeTo(lblImage.getWidth(), lblImage.getHeight(), Color.WHITE);
            lblImage.setIcon(new ImageIcon(image));

        } else {
            JOptionPane.showMessageDialog(getContentPane(), "File Not Found");
        }
    }
private void btnProsesActionPerformed(java.awt.event.ActionEvent evt) {
        // TODO add your handling code here:
        try {
            ImageGrayscale grayscale = new ImageGrayscale(image);
            lblGrayscale.setIcon(new ImageIcon(grayscale.grayOut()));


            image = new ImageFilter(image).threshold(200);
            lblThreshold.setIcon(new ImageIcon(image)); 

        } catch (NullPointerException ex) {
            JOptionPane.showMessageDialog(null, "Silahkan pilih gambar!");
        }
    }


Membuat Thresholding Citra Dengan Pemrograman Java NetBeans Membuat Thresholding Citra Dengan Pemrograman Java NetBeans Reviewed by antkecil on 20:07 Rating: 5

Post Comments

No comments:

Powered by Blogger.