Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.image.BufferedImage;
- import java.io.*;
- import javax.imageio.ImageIO;
- public class Threshold {
- public static BufferedImage threshold (BufferedImage input, int level)
- {
- int img_height = input.getHeight();
- int img_width = input.getWidth();
- BufferedImage output_img = new BufferedImage (img_width, img_height, BufferedImage.TYPE_INT_RGB);
- for (int x = 0; x < img_width; x++)
- {
- for (int y = 0; y < img_height; y++)
- {
- int r = getRedValue(input, x, y);
- int g = getGreenValue(input, x, y);
- int b = getBlueValue(input, x, y);
- int average = (r + g + b) / 3;
- if (average < level) {
- average = 0;
- } else if (average > level) {
- average = 255;
- }
- Color average_color = new Color(average, average, average);
- output_img.setRGB(x, y, average_color.getRGB());
- }
- }
- return output_img;
- }
- }
- public static int getRedValue(BufferedImage img, int x, int y) {
- Color col = new Color(img.getRGB(x,y));
- int red = col.getRed();
- return red;
- }
- public static int getGreenValue(BufferedImage img, int x, int y) {
- Color col = new Color(img.getRGB(x,y));
- int green = col.getGreen();
- return green;
- }
- public static int getBlueValue(BufferedImage img, int x, int y) {
- Color col = new Color(img.getRGB(x,y));
- int blue = col.getBlue();
- return blue;
- }
- public static void setColor(BufferedImage img, int x, int y, int r, int g, int b) {
- Color c = new Color(r, g, b);
- img.setRGB(x, y, c.getRGB());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement