0% found this document useful (0 votes)
97 views5 pages

Threshold

This Java code defines a class called TugasCitra2 that performs image processing tasks like loading an image, calculating color histograms, thresholding, and displaying results. It contains methods for loading an image, calculating RGB color values, thresholding based on a given value, and displaying the original and processed images. The main method prompts the user for an image path and threshold value, loads the image, calculates RGB values, performs thresholding, and displays the results.

Uploaded by

prita.pujaastuti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
97 views5 pages

Threshold

This Java code defines a class called TugasCitra2 that performs image processing tasks like loading an image, calculating color histograms, thresholding, and displaying results. It contains methods for loading an image, calculating RGB color values, thresholding based on a given value, and displaying the original and processed images. The main method prompts the user for an image path and threshold value, loads the image, calculates RGB values, performs thresholding, and displays the results.

Uploaded by

prita.pujaastuti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package pkg09021181823009;
import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.awt.Color;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import java.util.Scanner;
import java.awt.*;
import javax.swing.*;

public class TugasCitra2 extends JPanel{


private volatile boolean loaded = false;
private static BufferedImage image;
private static int [][] rgbValue;
private int[][] colourBins;
private static String value = "";
private static JFrame frame;
private static JLabel label;
private int SIZE = 256;

private int NUMBER_OF_COLOURS = 3;


public final int RED = 0;
public final int GREEN = 1;
public final int BLUE = 2;
private int maxY;

public TugasCitra2()
{
colourBins = new int[NUMBER_OF_COLOURS][];

for (int i = 0; i < NUMBER_OF_COLOURS; i++)


colourBins[i] = new int[SIZE];
loaded = false;
}
public void load(String path) throws IOException
{
BufferedImage bi = ImageIO.read(new File(path));
for (int i = 0; i < NUMBER_OF_COLOURS; i++)
{
for (int j = 0; j < SIZE; j++)
colourBins[i][j] = 0;
}
for (int x = 0; x < bi.getWidth(); x++)
{
for (int y = 0; y < bi.getHeight(); y++)
{
Color c = new Color(bi.getRGB(x, y));
colourBins[RED][c.getRed()]++;
colourBins[GREEN][c.getGreen()]++;
colourBins[BLUE][c.getBlue()]++;
}
}
maxY = 0;
for (int i = 0; i < NUMBER_OF_COLOURS; i++)
{
for (int j = 0; j < SIZE; j++)
{
if (maxY < colourBins[i][j])
maxY = colourBins[i][j];
}
}

loaded = true;
}
@Override
public void paint(Graphics g)
{
if (loaded)
{
Graphics2D g2 = (Graphics2D)g;

g2.setColor(Color.white);
g2.fillRect(0, 0, getWidth(), getHeight());

g2.setStroke(new BasicStroke(2));

int xInterval = (int) ((double)getWidth() / ((double)SIZE+1));

g2.setColor(Color.black);

for (int i = 0; i < NUMBER_OF_COLOURS; i++)


{

if (i == RED)
g2.setColor(Color.red);
else if (i == GREEN)
g2.setColor(Color.GREEN);
else if (i == BLUE)
g2.setColor(Color.blue);

for (int j = 0; j < SIZE - 1 ; j++)


{
int value = (int) (((double)colourBins[i][j] / (double)maxY) *
getHeight());
int value2 = (int) (((double)colourBins[i][j+1] / (double)maxY)
* getHeight());

g2.drawLine(j * xInterval, getHeight() - value,


(j+1)*xInterval, getHeight() - value2);
}
}
}
else
super.paint(g);
}
public static void display(BufferedImage image)
{
if(frame == null)
{
frame = new JFrame();
frame.setTitle("IMAGE PROCESSING");
frame.setSize(image.getWidth()+200, image.getHeight()+200);
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
label = new JLabel();
label.setIcon(new ImageIcon(image));
frame.getContentPane().add(label,BorderLayout.CENTER);
frame.setLocationRelativeTo(null);
frame.pack();
frame.setVisible(true);
}
else label.setIcon(new ImageIcon(image));
}
public BufferedImage getImage()
{
return image;
}
public static BufferedImage setImage(String pathFile)
{
File file = new File(pathFile);
try
{
image = ImageIO.read(file);
return image;
}
catch (IOException ex)
{
Logger.getLogger(TugasCitra2.class.getName()).log(Level.SEVERE, null,
ex);
return null;
}
}
public int[][] getRgbValue()
{
return rgbValue;
}
public static void getRGBValue(int width, int height)
{
int counter = 0;

for(int i = 0; i < width; i++)


{
for(int j = 0; j < height; j++)
{
System.out.print(rgbValue[counter][0] + ", " + rgbValue[counter][1]
+ ", " + rgbValue[counter][2] + "\t\t");
counter++;
}
System.out.println("");
}
}
public static BufferedImage setRgbValue(BufferedImage image)
{
int width = image.getWidth();
int height = image.getHeight();

rgbValue = new int[width * height][3];


int counter = 0;
BufferedImage X = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);

for(int i = 0; i < width; i++)


{
for(int j = 0; j < height; j++)
{
int color = image.getRGB(i, j);
int red = (color & 0x00ff0000) >> 16;
int green = (color & 0x0000ff00) >> 8;
int blue = (color & 0x000000ff);

rgbValue[counter][0] = red;
rgbValue[counter][1] = green;
rgbValue[counter][2] = blue;

int rgb = new Color(rgbValue[counter][0], rgbValue[counter][1],


rgbValue[counter][2]).getRGB();
X.setRGB(i, j, rgb);
counter++;

value += red + ", " + green + ", " + blue + "\t\t";


}
}
getRGBValue(width, height);
return X;
}
public void printFileRGBValue(String pathFile)
{
try
{
BufferedWriter writer = new BufferedWriter(new FileWriter(pathFile));
writer.write(value);
writer.close();
}
catch (IOException ex)
{
Logger.getLogger(TugasCitra2.class.getName()).log(Level.SEVERE, null,
ex);
}
}

public BufferedImage Threshold(BufferedImage img, int requiredThresholdValue)


{
int height = img.getHeight();
int width = img.getWidth();
BufferedImage finalThresholdImage = new
BufferedImage(width,height,BufferedImage.TYPE_INT_RGB) ;

for (int x = 0; x < width; x++)


{
try
{
for (int y = 0; y < height; y++)
{
int color = img.getRGB(x, y);
int red = (color & 0x00ff0000) >> 16;
int green = (color & 0x0000ff00) >> 8;
int blue = (color & 0x000000ff);
if((red+green+blue)/3 < (int)(requiredThresholdValue))
finalThresholdImage.setRGB(x, y, 0xFFFFFFFF);
else
finalThresholdImage.setRGB(x, y, 0x00000000);
}
}
catch (Exception e)
{
e.getMessage();
}
}
display(finalThresholdImage);
return finalThresholdImage;
}

public static void main(String[] args) throws IOException


{
Scanner input = new Scanner(System.in);
System.out.println("------- TUGAS KELOMPOK PENGOLAHAN CITRA -------");
System.out.println("Nama Anggota Kelompok : ");
System.out.println("1. Desry Kencana Putri (09021181823164)\n"
+ "2. Muhammad Sholeh (09021281823172)\n"
+ "3. Prita Puja Astuti (09021181823009)");
System.out.println("-----------------------------------------------");

System.out.println("Thresholding");

System.out.println("Contoh Format Masukan : E:\\A.png");


System.out.print("Masukkan Alamat Image - A : ");
String pathA7 = input.next();
System.out.print("Masukkan Nilai Ambang Batas : ");
int threshold = input.nextInt();

BufferedImage A7 = setImage(pathA7);
TugasCitra2 a7 = new TugasCitra2();
a7.setImage(pathA7);
A7 = a7.setRgbValue(a7.getImage());

TugasCitra2 c7 = new TugasCitra2();


BufferedImage C7 = a7.Threshold(A7, threshold);
display(C7);

}
}

You might also like