Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.image.BufferedImage;
  3. import java.io.*;
  4. import javax.imageio.ImageIO;
  5.  
  6.  
  7. public class Threshold {
  8.  
  9. public static BufferedImage threshold (BufferedImage input, int level)
  10. {
  11. int img_height = input.getHeight();
  12. int img_width = input.getWidth();
  13. BufferedImage output_img = new BufferedImage (img_width, img_height, BufferedImage.TYPE_INT_RGB);
  14.  
  15.  
  16. for (int x = 0; x < img_width; x++)
  17. {
  18. for (int y = 0; y < img_height; y++)
  19. {
  20. int r = getRedValue(input, x, y);
  21. int g = getGreenValue(input, x, y);
  22. int b = getBlueValue(input, x, y);
  23.  
  24. int average = (r + g + b) / 3;
  25.  
  26. if (average < level) {
  27. average = 0;
  28. } else if (average > level) {
  29. average = 255;
  30. }
  31.  
  32. Color average_color = new Color(average, average, average);
  33. output_img.setRGB(x, y, average_color.getRGB());
  34. }
  35. }
  36. return output_img;
  37. }
  38. }
  39.  
  40. public static int getRedValue(BufferedImage img, int x, int y) {
  41. Color col = new Color(img.getRGB(x,y));
  42. int red = col.getRed();
  43. return red;
  44. }
  45.  
  46. public static int getGreenValue(BufferedImage img, int x, int y) {
  47. Color col = new Color(img.getRGB(x,y));
  48. int green = col.getGreen();
  49. return green;
  50. }
  51.  
  52. public static int getBlueValue(BufferedImage img, int x, int y) {
  53. Color col = new Color(img.getRGB(x,y));
  54. int blue = col.getBlue();
  55. return blue;
  56. }
  57.  
  58. public static void setColor(BufferedImage img, int x, int y, int r, int g, int b) {
  59. Color c = new Color(r, g, b);
  60. img.setRGB(x, y, c.getRGB());
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement