Secara umum, citra berwarna dapat dikonversi ke citra berskala keabuan menggunakan rumus 1 berikut :
I=a x R+b x G+c x B, dengan a+b+c=1
dengan R menyatakan nilai komponen merah, G menyatakan nilai komponen hijau, dan B menyatakan nilai komponen biru. Misalnya, sebuah piksel mempunyai komponen R, G, B sebagai berikut:
R = 50
G = 70
B = 61
Jika a, b, dan c pada rumus 1 dibuat sama, akan diperoleh hasil seperti berikut :
I = (50 + 70 + 60) / 3 = 60
Salah satu contoh rumus yang biasa dipakai untuk mengubah ke skala keabuan yaitu rumus 2 berikut :
I=0,2989 x R+0,5870 x G+0,1141 x B
Implementasi di Java :
package grayscale;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
/**
*
* @author David
*/
public class GrayScale {
BufferedImage image;
int width;
int height;
public GrayScale() {
try {
File input = new File("cool.jpg");
image = ImageIO.read(input);
width = image.getWidth();
height = image.getHeight();
for(int i=0; i<height; i++){
for(int j=0; j<width; j++){
Color c = new Color(image.getRGB(j, i));
int red = (int)(c.getRed() * 0.299);
int green = (int)(c.getGreen() * 0.587);
int blue = (int)(c.getBlue() *0.114);
Color newColor = new Color(red+green+blue,
red+green+blue,red+green+blue);
image.setRGB(j,i,newColor.getRGB());
}
}
File ouptut = new File("grayscale.jpg");
ImageIO.write(image, "jpg", ouptut);
} catch (Exception e) {}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
GrayScale obj = new GrayScale();
}
}
Hasil Eksekusi :
citra hasil konversi |
Komentar
Posting Komentar