|
| 1 | +import java.awt.Color; |
| 2 | +import java.awt.image.BufferedImage; |
| 3 | +import java.awt.image.ConvolveOp; |
| 4 | +import java.awt.image.DataBufferByte; |
| 5 | +import java.awt.image.Kernel; |
| 6 | +import java.io.File; |
| 7 | +import java.io.IOException; |
| 8 | +import javax.imageio.ImageIO; |
| 9 | +import java.lang.Math.*; |
| 10 | +import java.util.Arrays; |
| 11 | + |
| 12 | +public class Filter { |
| 13 | + // 轉成灰階值 |
| 14 | + public static BufferedImage toGray(BufferedImage image){ |
| 15 | + int width = image.getWidth(); |
| 16 | + int height = image.getHeight(); |
| 17 | + for(int y = 0; y < height; y++){ |
| 18 | + for(int x = 0; x < width; x++){ |
| 19 | + int p = image.getRGB(x,y); |
| 20 | + int a = (p>>24)&0xff; |
| 21 | + int r = (p>>16)&0xff; |
| 22 | + int g = (p>>8)&0xff; |
| 23 | + int b = p&0xff; |
| 24 | + int avg = (r+g+b)/3; |
| 25 | + p = (a<<24) | (avg<<16) | (avg<<8) | avg; |
| 26 | + image.setRGB(x, y, p); |
| 27 | + } |
| 28 | + } |
| 29 | + return image; |
| 30 | + } |
| 31 | + // 轉成負片效果 |
| 32 | + public static BufferedImage toNegative(BufferedImage image){ |
| 33 | + int width = image.getWidth(); |
| 34 | + int height = image.getHeight(); |
| 35 | + for(int y = 0; y < height; y++){ |
| 36 | + for(int x = 0; x < width; x++){ |
| 37 | + int p = image.getRGB(x,y); |
| 38 | + int alpha = new Color(image.getRGB(x,y)).getAlpha(); |
| 39 | + int red = new Color(image.getRGB(x,y)).getRed(); |
| 40 | + int green = new Color(image.getRGB(x,y)).getGreen(); |
| 41 | + int blue = new Color(image.getRGB(x,y)).getBlue(); |
| 42 | + alpha = 255 - alpha; |
| 43 | + red = 255 - red; |
| 44 | + green = 255 - green; |
| 45 | + blue = 255 - blue; |
| 46 | + p = (alpha<<24) | (red<<16) | (green<<8) | blue; |
| 47 | + image.setRGB(x, y, p); |
| 48 | + } |
| 49 | + } |
| 50 | + return image; |
| 51 | + } |
| 52 | + // 利用 gamma 轉換 |
| 53 | + public static BufferedImage setGamma(BufferedImage image, double gamma){ |
| 54 | + image = toGray(image); |
| 55 | + int width = image.getWidth(); |
| 56 | + int height = image.getHeight(); |
| 57 | + BufferedImage gamma_cor = new BufferedImage(image.getWidth(), image.getHeight(), image.getType()); |
| 58 | + |
| 59 | + for(int y = 0; y < height; y++){ |
| 60 | + for(int x = 0; x < width; x++){ |
| 61 | + |
| 62 | + int p = image.getRGB(x,y); |
| 63 | + int alpha = new Color(image.getRGB(x,y)).getAlpha(); |
| 64 | + int red = new Color(image.getRGB(x,y)).getRed(); |
| 65 | + int green = new Color(image.getRGB(x,y)).getGreen(); |
| 66 | + int blue = new Color(image.getRGB(x,y)).getBlue(); |
| 67 | + |
| 68 | + red = (int) (255 * (Math.pow((double) red / (double) 255, gamma))); |
| 69 | + green = (int) (255 * (Math.pow((double) green / (double) 255, gamma))); |
| 70 | + blue = (int) (255 * (Math.pow((double) blue / (double) 255, gamma))); |
| 71 | + p = (alpha<<24) | (red<<16) | (green<<8) | blue; |
| 72 | + gamma_cor.setRGB(x, y, p); |
| 73 | + } |
| 74 | + } |
| 75 | + return gamma_cor; |
| 76 | + } |
| 77 | + // 胡椒鹽雜訊 |
| 78 | + public static BufferedImage saltpepper(BufferedImage image) |
| 79 | + { |
| 80 | + int height = image.getHeight(); |
| 81 | + int width = image.getWidth(); |
| 82 | + int nSalt = 5; // Percentage of salt |
| 83 | + int nPepper = 5; |
| 84 | + int salt = height * width * nSalt / 100; // Amount of salt |
| 85 | + int pepper = height * width * nSalt / 100; // Amount of pepper |
| 86 | + |
| 87 | + for( int i = 0; i < salt; i++ ) |
| 88 | + { |
| 89 | + int x = (int) (Math.random() * width); |
| 90 | + int y = (int) (Math.random() * height); |
| 91 | + int p = image.getRGB(x,y); |
| 92 | + int alpha = new Color(image.getRGB(x,y)).getAlpha(); |
| 93 | + int red = 255; |
| 94 | + int green = 255; |
| 95 | + int blue = 255; |
| 96 | + |
| 97 | + p = (alpha<<24) | (red<<16) | (green<<8) | blue; |
| 98 | + image.setRGB( x, y, p ); |
| 99 | + } |
| 100 | + |
| 101 | + for( int i = 0; i < pepper; i++ ) |
| 102 | + { |
| 103 | + int x = (int) (Math.random() * width); |
| 104 | + int y = (int) (Math.random() * height); |
| 105 | + int p = image.getRGB(x,y); |
| 106 | + int alpha = new Color(image.getRGB(x,y)).getAlpha(); |
| 107 | + int red = 0; |
| 108 | + int green = 0; |
| 109 | + int blue = 0; |
| 110 | + |
| 111 | + p = (alpha<<24) | (red<<16) | (green<<8) | blue; |
| 112 | + image.setRGB( x, y, p ); |
| 113 | + } |
| 114 | + return image; |
| 115 | + } |
| 116 | + // 3 X 3中值濾波器 |
| 117 | + public static BufferedImage median_filter(BufferedImage image) { |
| 118 | + int maskSize = 3; |
| 119 | + int width = image.getWidth(); |
| 120 | + int height = image.getHeight(); |
| 121 | + int outputPixels[] = new int[width * height]; |
| 122 | + int red[], green[], blue[]; |
| 123 | + int xMin, xMax, yMin, yMax; |
| 124 | + int argb, reD, greenN, bluE; |
| 125 | + /** Median Filter operation */ |
| 126 | + for (int y = 0; y < height; y++) { |
| 127 | + for (int x = 0; x < width; x++) { |
| 128 | + int a = new Color(image.getRGB(x,y)).getAlpha(); |
| 129 | + red = new int[maskSize * maskSize]; |
| 130 | + green = new int[maskSize * maskSize]; |
| 131 | + blue = new int[maskSize * maskSize]; |
| 132 | + int count = 0; |
| 133 | + xMin = x - (maskSize / 2); |
| 134 | + xMax = x + (maskSize / 2); |
| 135 | + yMin = y - (maskSize / 2); |
| 136 | + yMax = y + (maskSize / 2); |
| 137 | + for (int r = yMin; r <= yMax; r++) { |
| 138 | + for (int c = xMin; c <= xMax; c++) { |
| 139 | + if (r < 0 || r >= height || c < 0 || c >= width) { |
| 140 | + /** Some portion of the mask is outside the image. */ |
| 141 | + continue; |
| 142 | + } else { |
| 143 | + argb = image.getRGB(c, r); |
| 144 | + reD = (argb >> 16) & 0xff; |
| 145 | + red[count] = reD; |
| 146 | + greenN = (argb >> 8) & 0xff; |
| 147 | + green[count] = greenN; |
| 148 | + bluE = (argb) & 0xFF; |
| 149 | + blue[count] = bluE; |
| 150 | + count++; |
| 151 | + } |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /** sort red, green, blue array */ |
| 156 | + Arrays.sort(red); |
| 157 | + Arrays.sort(green); |
| 158 | + Arrays.sort(blue); |
| 159 | + |
| 160 | + /** save median value in outputPixels array */ |
| 161 | + int index = (count % 2 == 0) ? count / 2 - 1 : count / 2; |
| 162 | + int p = (a << 24) | (red[index] << 16) | (green[index] << 8) | blue[index]; |
| 163 | + outputPixels[x + y * width] = p; |
| 164 | + } |
| 165 | + } |
| 166 | + for (int y = 0; y < height; y++) { |
| 167 | + for (int x = 0; x < width; x++) { |
| 168 | + image.setRGB(x, y, outputPixels[x + y * width]); |
| 169 | + } |
| 170 | + } |
| 171 | + return image; |
| 172 | + } |
| 173 | + // 3 X 3 平均濾波器 |
| 174 | + public static BufferedImage mean_filter(BufferedImage image){ |
| 175 | + int height = image.getHeight(); |
| 176 | + int width = image.getWidth(); |
| 177 | + for (int x = 1; x < height - 1; x++) { |
| 178 | + for (int y = 1; y < width - 1; y++) { |
| 179 | + Color p1 = new Color(image.getRGB(x - 1 , y - 1)); |
| 180 | + Color p2 = new Color(image.getRGB(x - 1 , y)); |
| 181 | + Color p3 = new Color(image.getRGB(x - 1, y + 1)); |
| 182 | + Color p4 = new Color(image.getRGB(x, y - 1)); |
| 183 | + Color p5 = new Color(image.getRGB(x, y)); |
| 184 | + Color p6 = new Color(image.getRGB(x, y + 1)); |
| 185 | + Color p7 = new Color(image.getRGB(x + 1, y - 1)); |
| 186 | + Color p8 = new Color(image.getRGB(x + 1, y)); |
| 187 | + Color p9 = new Color(image.getRGB(x + 1, y + 1)); |
| 188 | + int alpha_avg = (int)(p1.getAlpha() + p2.getAlpha() + p3.getAlpha() + p4.getAlpha() + p5.getAlpha() + p6.getAlpha() + p7.getAlpha() + p8.getAlpha() + p9.getAlpha()) / 9; |
| 189 | + int red_avg = (int)(p1.getRed() + p2.getRed() + p3.getRed() + p4.getRed() + p5.getRed() + p6.getRed() + p7.getRed() + p8.getRed() + p9.getRed()) / 9; |
| 190 | + int green_avg = (int)(p1.getGreen() + p2.getGreen() + p3.getGreen() + p4.getGreen() + p5.getGreen() + p6.getGreen() + p7.getGreen() + p8.getGreen() + p9.getGreen()) / 9; |
| 191 | + int blue_avg = (int)(p1.getBlue() + p2.getBlue() + p3.getBlue() + p4.getBlue() + p5.getBlue() + p6.getBlue() + p7.getBlue() + p8.getBlue() + p9.getBlue()) / 9; |
| 192 | + |
| 193 | + int p = (alpha_avg<<24) | (red_avg<<16) | (green_avg<<8) | blue_avg; |
| 194 | + |
| 195 | + image.setRGB(x, y, p); |
| 196 | + } |
| 197 | + } |
| 198 | + return image; |
| 199 | + } |
| 200 | + public static BufferedImage sobel_filter(BufferedImage image){ |
| 201 | + BufferedImage Gx, Gy; |
| 202 | + float[] x1 = {-1, 0, 1, -2, 0, 2, -1, 0, 1}; |
| 203 | + float[] y1 = {-1,-2,-1,0,0,0,1,2,1}; |
| 204 | + //2x2 matrix, with those two float arrays. |
| 205 | + Kernel MatrixA = new Kernel(3, 3, x1); |
| 206 | + Kernel MatrixB = new Kernel(3, 3, y1); |
| 207 | + ConvolveOp convolve1 = new ConvolveOp(MatrixA); |
| 208 | + ConvolveOp convolve2 = new ConvolveOp(MatrixB); |
| 209 | + Gx = convolve1.filter(image, null); |
| 210 | + Gy = convolve2.filter(image, null); |
| 211 | + for (int x=0; x<image.getWidth(); x++) { |
| 212 | + for (int y=0; y<image.getHeight(); y++) { |
| 213 | + int derp = Gx.getRGB(x,y); |
| 214 | + int herp = Gy.getRGB(x,y); |
| 215 | + double result = Math.sqrt(Math.pow(derp, 2.0) + Math.pow(herp, 2.0)); |
| 216 | + if(result < 20726564.99) { |
| 217 | + image.setRGB(x,y,Color.white.getRGB()); |
| 218 | + } else { |
| 219 | + image.setRGB(x,y,Color.black.getRGB()); |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + return image; |
| 224 | + } |
| 225 | + public static BufferedImage scale_2_binary(BufferedImage image){ |
| 226 | + int height = image.getHeight(); |
| 227 | + int width = image.getWidth(); |
| 228 | + float threshold = 0; |
| 229 | + for(int y = 0; y < height; y++){ |
| 230 | + for(int x = 0; x < width; x++){ |
| 231 | + int p = image.getRGB(x,y); |
| 232 | + int a = new Color(p).getAlpha(); |
| 233 | + int r = new Color(p).getRed(); |
| 234 | + int g = new Color(p).getGreen(); |
| 235 | + int b = new Color(p).getBlue(); |
| 236 | + // int rgb = 0xFFFF * r + 0xFF * g + b; |
| 237 | + float luminance = (r * 0.2126f + g * 0.7152f + b * 0.0722f) / 255; |
| 238 | + threshold += luminance; |
| 239 | + |
| 240 | + } |
| 241 | + } |
| 242 | + threshold = threshold / (height * width); |
| 243 | + System.out.println(threshold); |
| 244 | + for(int y = 0; y < height; y++){ |
| 245 | + for(int x = 0; x < width; x++){ |
| 246 | + int p = image.getRGB(x,y); |
| 247 | + int a = new Color(p).getAlpha(); |
| 248 | + int r = new Color(p).getRed(); |
| 249 | + int g = new Color(p).getGreen(); |
| 250 | + int b = new Color(p).getBlue(); |
| 251 | + float luminance = (r * 0.2126f + g * 0.7152f + b * 0.0722f) / 255; |
| 252 | + if (luminance > threshold){ |
| 253 | + image.setRGB(x, y, Color.white.getRGB()); |
| 254 | + |
| 255 | + }else{ |
| 256 | + image.setRGB(x, y, Color.black.getRGB()); |
| 257 | + } |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + return image; |
| 262 | + } |
| 263 | +} |
0 commit comments