Last active
October 6, 2018 03:42
-
-
Save biobii/06cf587f718076120da84004e8a5e919 to your computer and use it in GitHub Desktop.
Filtering Citra Mean, Median dan Batas
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 Filtering; | |
import java.io.File; | |
/** | |
* | |
* @author biobii | |
*/ | |
public class App { | |
public static void main(String[] args) { | |
String file = "/home/biobii/Pictures/bintik.png"; | |
File targetImage = new File(file); | |
Median median = new Median(); | |
median.filter(targetImage); | |
Mean mean = new Mean(); | |
mean.filter(targetImage); | |
Batas batas = new Batas(); | |
batas.filter(targetImage); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 Filtering; | |
import java.awt.Color; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import javax.imageio.ImageIO; | |
/** | |
* | |
* @author biobii | |
*/ | |
public class Batas extends Neighbor implements FilterContract { | |
private String context = "batas"; | |
private BufferedImage image, newImage; | |
private int width, height; | |
public void filter(File file) { | |
try { | |
image = ImageIO.read(file); | |
newImage = ImageIO.read(file); | |
width = image.getWidth(); | |
height = image.getHeight(); | |
for (int row = 0; row < width; row++) { | |
for (int col = 0; col < height; col++) { | |
int red = this.redProcess(context, image, row, col, width, height); | |
int green = this.greenProcess(context, image, row, col, width, height); | |
int blue = this.blueProcess(context, image, row, col, width, height); | |
newImage.setRGB(row, col, new Color(red, green, blue).getRGB()); | |
} | |
} | |
this.output(); | |
} catch (Exception e) { | |
System.out.println("Error:\t" + e.getMessage()); | |
} | |
} | |
public void output() { | |
try { | |
File output = new File("/home/biobii/Pictures/batas.jpg"); | |
ImageIO.write(newImage, "jpg", output); | |
System.out.println("Filter Batas outputed!"); | |
} catch (Exception e) { | |
System.out.println("Error: " + e.getMessage()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 Filtering; | |
import java.io.File; | |
/** | |
* | |
* @author biobii | |
*/ | |
public interface FilterContract { | |
public void filter(File image); | |
public void output(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 Filtering; | |
import java.awt.Color; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import javax.imageio.ImageIO; | |
/** | |
* | |
* @author biobii | |
*/ | |
public class Mean extends Neighbor implements FilterContract { | |
private String context = "mean"; | |
private BufferedImage image, newImage; | |
private int width, height; | |
public void filter(File file) { | |
try { | |
image = ImageIO.read(file); | |
newImage = ImageIO.read(file); | |
width = image.getWidth(); | |
height = image.getHeight(); | |
for (int row = 0; row < width; row++) { | |
for (int col = 0; col < height; col++) { | |
int red = this.redProcess(context, image, row, col, width, height); | |
int green = this.greenProcess(context, image, row, col, width, height); | |
int blue = this.blueProcess(context, image, row, col, width, height); | |
newImage.setRGB(row, col, new Color(red, green, blue).getRGB()); | |
} | |
} | |
this.output(); | |
} catch (Exception e) { | |
System.out.println("Error:\t" + e.getMessage()); | |
} | |
} | |
public void output() { | |
try { | |
File output = new File("/home/biobii/Pictures/mean.jpg"); | |
ImageIO.write(newImage, "jpg", output); | |
System.out.println("Filter Mean outputed!"); | |
} catch (Exception e) { | |
System.out.println("Error: " + e.getMessage()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 Filtering; | |
import java.awt.Color; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import javax.imageio.ImageIO; | |
/** | |
* | |
* @author biobii | |
*/ | |
public class Median extends Neighbor implements FilterContract { | |
private String context = "median"; | |
private BufferedImage image, newImage; | |
private int width, height; | |
public void filter(File file) { | |
try { | |
image = ImageIO.read(file); | |
newImage = ImageIO.read(file); | |
width = image.getWidth(); | |
height = image.getHeight(); | |
for (int row = 0; row < width; row++) { | |
for (int col = 0; col < height; col++) { | |
int red = this.redProcess(context, image, row, col, width, height); | |
int green = this.greenProcess(context, image, row, col, width, height); | |
int blue = this.blueProcess(context, image, row, col, width, height); | |
newImage.setRGB(row, col, new Color(red, green, blue).getRGB()); | |
} | |
} | |
this.output(); | |
} catch (Exception e) { | |
System.out.println("Error:\t" + e.getMessage()); | |
} | |
} | |
public void output() { | |
try { | |
File output = new File("/home/biobii/Pictures/median.jpg"); | |
ImageIO.write(newImage, "jpg", output); | |
System.out.println("Filter Median outputed!"); | |
} catch (Exception e) { | |
System.out.println("Error: " + e.getMessage()); | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 Filtering; | |
import java.awt.Color; | |
import java.awt.image.BufferedImage; | |
import java.util.Arrays; | |
/** | |
* | |
* @author biobii | |
*/ | |
public class Neighbor { | |
private int top, right, bottom, left; | |
private int current, mid; | |
private Color c; | |
public int redProcess(String context, BufferedImage image, int row, int col, int width, int height) { | |
c = new Color(image.getRGB(row, col)); | |
current = c.getRed(); | |
// tetangga atas | |
if (row - 1 < 0) { | |
top = 0; | |
} else { | |
c = new Color(image.getRGB(row - 1, col)); | |
top = c.getRed(); | |
} | |
// tetangga kanan | |
if (col + 1 >= height) { | |
right = 0; | |
} else { | |
c = new Color(image.getRGB(row, col + 1)); | |
right = c.getRed();; | |
} | |
// tetangga bawah | |
if (row + 1 >= width) { | |
bottom = 0; | |
} else { | |
c = new Color(image.getRGB(row + 1, col)); | |
bottom = c.getRed(); | |
} | |
// tetangga kiri | |
if (col - 1 < 0) { | |
left = 0; | |
} else { | |
c = new Color(image.getRGB(row, col - 1)); | |
left = c.getRed(); | |
} | |
int value = 0; | |
int[] values = {current, top, right, bottom, left}; | |
switch (context) { | |
case "median": | |
Arrays.sort(values); | |
value = values[values.length / 2]; | |
break; | |
case "mean": | |
value = Arrays.stream(values).sum() / values.length; | |
break; | |
case "batas": | |
Arrays.sort(values); | |
if (current < values[0]) { | |
current = values[0]; | |
break; | |
} | |
if (current > values[values.length-1]) { | |
current = values[values.length-1]; | |
break; | |
} | |
value = current; | |
break; | |
} | |
return value; | |
} | |
public int greenProcess(String context, BufferedImage image, int row, int col, int width, int height) { | |
c = new Color(image.getRGB(row, col)); | |
current = c.getGreen(); | |
// tetangga atas | |
if (row - 1 < 0) { | |
top = 0; | |
} else { | |
c = new Color(image.getRGB(row - 1, col)); | |
top = c.getGreen(); | |
} | |
// tetangga kanan | |
if (col + 1 >= height) { | |
right = 0; | |
} else { | |
c = new Color(image.getRGB(row, col + 1)); | |
right = c.getGreen();; | |
} | |
// tetangga bawah | |
if (row + 1 >= width) { | |
bottom = 0; | |
} else { | |
c = new Color(image.getRGB(row + 1, col)); | |
bottom = c.getGreen(); | |
} | |
// tetangga kiri | |
if (col - 1 < 0) { | |
left = 0; | |
} else { | |
c = new Color(image.getRGB(row, col - 1)); | |
left = c.getGreen(); | |
} | |
int value = 0; | |
int[] values = {current, top, right, bottom, left}; | |
switch (context) { | |
case "median": | |
Arrays.sort(values); | |
value = values[values.length / 2]; | |
break; | |
case "mean": | |
value = Arrays.stream(values).sum() / values.length; | |
break; | |
case "batas": | |
Arrays.sort(values); | |
if (current < values[0]) { | |
current = values[0]; | |
break; | |
} | |
if (current > values[values.length-1]) { | |
current = values[values.length-1]; | |
break; | |
} | |
value = current; | |
break; | |
} | |
return value; | |
} | |
public int blueProcess(String context, BufferedImage image, int row, int col, int width, int height) { | |
c = new Color(image.getRGB(row, col)); | |
current = c.getGreen(); | |
// tetangga atas | |
if (row - 1 < 0) { | |
top = 0; | |
} else { | |
c = new Color(image.getRGB(row - 1, col)); | |
top = c.getBlue(); | |
} | |
// tetangga kanan | |
if (col + 1 >= height) { | |
right = 0; | |
} else { | |
c = new Color(image.getRGB(row, col + 1)); | |
right = c.getBlue();; | |
} | |
// tetangga bawah | |
if (row + 1 >= width) { | |
bottom = 0; | |
} else { | |
c = new Color(image.getRGB(row + 1, col)); | |
bottom = c.getBlue(); | |
} | |
// tetangga kiri | |
if (col - 1 < 0) { | |
left = 0; | |
} else { | |
c = new Color(image.getRGB(row, col - 1)); | |
left = c.getBlue(); | |
} | |
int value = 0; | |
int[] values = {current, top, right, bottom, left}; | |
switch (context) { | |
case "median": | |
Arrays.sort(values); | |
value = values[values.length / 2]; | |
break; | |
case "mean": | |
value = Arrays.stream(values).sum() / values.length; | |
break; | |
case "batas": | |
Arrays.sort(values); | |
if (current < values[0]) { | |
current = values[0]; | |
break; | |
} | |
if (current > values[values.length-1]) { | |
current = values[values.length-1]; | |
break; | |
} | |
value = current; | |
break; | |
} | |
return value; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment