-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathImageProcess.java
More file actions
366 lines (307 loc) · 13.7 KB
/
ImageProcess.java
File metadata and controls
366 lines (307 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.Arrays;
public class ImageProcess {
public static void main(String args[]){
//讀取資料檔
File file = new File("image/original.jpg");
try {
BufferedImage RGB_img = ImageIO.read(file);
//讀取高度
int height = RGB_img.getHeight();
//讀取寬度
int width = RGB_img.getWidth();
//灰階轉換
int [] gray_img = new int[width * height];
gray_img = Gray(RGB_img);
//負片轉換
Negative(gray_img, width, height);
//Gamma 轉換
int [] gamma_img1 = new int[width * height];
int [] gamma_img2 = new int[width * height];
int [] gamma_img3 = new int[width * height];
gamma_img1 = Gamma(gray_img, width, height, 0.5);
gamma_img2 = Gamma(gray_img, width, height, 1);
gamma_img3 = Gamma(gray_img, width, height, 2);
//胡椒鹽雜訊
int [] peper_img = new int[width * height];
peper_img = Pepper(gamma_img1, width, height);
//中位數
Median(peper_img, width, height);
//平均濾波器
Mean(peper_img, width, height);
//Sobel
Sobel(gamma_img2, width, height);
//二值化
OTSU(gamma_img3, width, height);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//灰階轉換
public static int[] Gray(BufferedImage RGB_img){
int width = RGB_img.getWidth();
int height = RGB_img.getHeight();
int [] pixels = new int[width * height];
int [] pixels_result = new int[width * height];
// Get RGB pixels
RGB_img.getRGB(0, 0, width, height, pixels, 0, width);
/*
RGB Process
int red = (pixel >> 16) & 0xff; 右移16位之後原来的高8位就在低8位的位置上了,再與0xff就只剩下了原来的高8位數值
int green = (pixel >> 8) & 0xff; 右移8位之後原来的中間8位就在低8位的位置上了,再與0xff就只剩下了原来的中間8位數值
int blue = (pixel ) & 0xff; 與0xff就直接得到了低8位数值
*/
for(int i = 0; i < width*height ; i++){
int rgb = pixels[i];
int red = (rgb & 0xff0000) >> 16;
int green= (rgb & 0x00ff00) >> 8;
int blue= rgb & 0x0000ff;
//轉換灰階公式
int gray = (int)(0.299 * red + 0.587 * green + 0.114 * blue); // 由 RGB 來計算 Y 值
pixels[i] = (0xff000000 | gray<<16 | gray<<8 | gray);
pixels_result[i] = gray; //解決黃點問題
}
BufferedImage gray_image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //image 轉 BfferedImage
gray_image.setRGB(0, 0, width, height, pixels, 0, width);
try {
File file_gray = new File("gray.bmp");
ImageIO.write(gray_image, "bmp", file_gray);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pixels_result;
}
//負片 (255-原像素值)
public static void Negative(int[] gray_img, int width, int height){
int [] pixels = new int[gray_img.length];
for(int i = 0; i < gray_img.length; i++){
int negative = 255 - gray_img[i];
pixels[i] = (0xff000000 | negative<<16 | negative<<8 | negative);
}
BufferedImage negative_image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //image 轉 BfferedImage
negative_image.setRGB(0, 0, width, height, pixels, 0, width);
try {
File file_negative = new File("negative.bmp");
ImageIO.write(negative_image, "bmp", file_negative);
}catch (Exception e) {}
}
public static int[] Gamma(int [] gray_img, int width, int height, double gamma_value){
int max = -1;
int min = 256;
int [] pixels = new int[gray_img.length];
int [] pixels_result = new int[gray_img.length];
for(int i = 0; i < gray_img.length; i++){ //找max and min
if(gray_img[i] > max){
max = gray_img[i];
}
if(gray_img[i] < min){
min = gray_img[i];
}
}
/*
Gamma公式
[(p(i,j)-min/max-min)^gamma]*255
p(i,j)為像素點之值,min為圖片中像素之最小值,max為圖片中像素之最大值
*/
for(int i = 0; i < gray_img.length; i++){
double gamma_double = Math.pow((double)(gray_img[i] - min)/(max - min), gamma_value)*255;
int gamma = (int) gamma_double;
pixels[i] = (0xff000000 | gamma << 16 | gamma << 8 | gamma);
pixels_result[i] = gamma;
}
BufferedImage gama_image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //image 轉 BfferedImage
gama_image.setRGB(0, 0, width, height, pixels, 0, width);
try {
File file_gama = new File("gamma_" + gamma_value + ".bmp");
ImageIO.write(gama_image, "bmp", file_gama);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pixels_result;
}
//胡椒鹽雜訊
public static int[] Pepper(int [] gamma_img, int width, int height){
int [] pixels = new int[gamma_img.length];
int [] pepper_result = new int[gamma_img.length];
for(int i = 0; i < gamma_img.length; i++){
pepper_result[i] = gamma_img[i];
}
Random random = new Random();
for(int i = 0; i < gamma_img.length; i++){
int pepper = random.nextInt(10);
if(pepper == 9){
pepper_result[i]=0;
}
else if(pepper == 0){
pepper_result[i]=255;
}
pixels[i] = (0xff000000 | pepper_result[i] << 16 | pepper_result[i] << 8 | pepper_result[i]);
}
BufferedImage pepper_image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //image 轉 BfferedImage
pepper_image.setRGB(0, 0, width, height, pixels, 0, width);
try {
File file_pepper = new File("peper.bmp");
ImageIO.write(pepper_image, "bmp", file_pepper);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return pepper_result;
}
//中值濾波器(將九個值進行排序,利用中間的值取代最大的值(即九宮格右下的值))
public static void Median(int [] peper_img, int width, int height){
int [] pixels = new int[peper_img.length];
int [] median_result = new int[peper_img.length];
for(int i = 0; i < peper_img.length; i++){
median_result[i] = peper_img[i];
}
for(int i = 1; i < width - 1; i++){
for(int j = 1; j < height - 1 ; j++){
int [] window = new int[9];
window[0]=peper_img[width*j + i - width - 1]; //左上
window[1]=peper_img[width*j + i - width]; //上
window[2]=peper_img[width*j + i - width + 1]; //右上
window[3]=peper_img[width*j + i - 1]; //左
window[4]=peper_img[width*j + i]; //中
window[5]=peper_img[width*j + i + 1]; //右
window[6]=peper_img[width*j + i + width - 1]; //左下
window[7]=peper_img[width*j + i + width]; //下
window[8]=peper_img[width*j + i + width + 1]; //右下
Arrays.sort(window);
median_result[width*j + i] = window[4]; //取代
}
}
for(int i = 0; i < peper_img.length; i++){
pixels[i] = (0xff000000 | median_result[i] << 16 | median_result[i] << 8 | median_result[i]);
}
BufferedImage median_image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //image 轉 BfferedImage
median_image.setRGB(0, 0, width, height, pixels, 0, width);
try {
File file_median = new File("median.bmp");
ImageIO.write(median_image, "bmp", file_median);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//平均濾波器(將九宮格每一格數值相加取平均值,並將此數值取代全部值)
public static void Mean(int [] peper_img, int width, int height){
int mean[]=new int[peper_img.length];
int mean_result[]= new int[peper_img.length];
for(int i = 0; i < peper_img.length; i++){
mean_result[i] = peper_img[i];
}
for(int i = 1; i < width - 1; i++){
for(int j = 1; j < height - 1; j++){
int window[]=new int[9];
window[0]=peper_img[width*j + i - width - 1]; //左上
window[1]=peper_img[width*j + i - width]; //上
window[2]=peper_img[width*j + i - width + 1]; //右上
window[3]=peper_img[width*j + i - 1]; //左
window[4]=peper_img[width*j + i]; //中
window[5]=peper_img[width*j + i + 1]; //右
window[6]=peper_img[width*j + i + width - 1]; //左下
window[7]=peper_img[width*j + i + width]; //下
window[8]=peper_img[width*j + i + width + 1]; //右下
int total = 0;
for(int k = 0; k < 9; k++) {
total += window[k];
}
double average = total / 9;
mean_result[width*j + i] = (int)average; //取代
}
}
for(int i = 0; i < peper_img.length; i++){
mean[i] = (0xff000000 | mean_result[i] << 16 | mean_result[i] << 8 | mean_result[i]);
}
BufferedImage mean_image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //image 轉 BfferedImage
mean_image.setRGB(0, 0, width, height, mean, 0, width);
try {
File file_mean = new File("mean.bmp");
ImageIO.write(mean_image, "bmp", file_mean);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//二值化(設門檻值(所有像素之平均值),大於門檻值則設255,小於則設0)
public static void OTSU(int [] gamma_img, int width, int height){
int threshold = 0;
int total = 0;
int [] pixels = new int[width * height];
int [] otsu_result = new int[width * height];
for (int i = 0; i < gamma_img.length ;i++){
otsu_result[i] = gamma_img[i];
}
for(int i = 0; i < gamma_img.length ;i++){
total += gamma_img[i];
}
threshold = total / (width*height);
for(int i = 0; i < gamma_img.length; i++){
if(gamma_img[i] >= threshold)
otsu_result[i] = 255;
else
otsu_result[i] = 0;
pixels[i] = (0xff000000 | otsu_result[i] << 16 | otsu_result[i] << 8 | otsu_result[i]);
}
BufferedImage otsu_image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //image 轉 BfferedImage
otsu_image.setRGB(0, 0, width, height, pixels, 0, width);
try {
File file_otsu = new File("otsu.bmp");
ImageIO.write(otsu_image, "bmp", file_otsu);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//sobel轉換
public static void Sobel(int [] gamma_img, int width, int height){
int [] pixels = new int[gamma_img.length];
int [][] Gx = new int[height][width];
int [][] Gy = new int[height][width];
int [][] G = new int[height][width];
int [][] original = new int[height][width]; //gamma2
int count = 0;
for(int i = 0; i < height; i++){
for(int j = 0; j < width; j++){
original[i][j] = gamma_img[count];
count ++;
}
}
int count2= 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (i == 0 || i == height - 1 || j == 0 || j == width - 1)
Gx[i][j] = Gy[i][j] = G[i][j] = 0; // image boundary cleared
else {
Gy[i][j] =
original[i+1][j-1] + 2*original[i+1][j] + original[i+1][j+1] -
original[i-1][j-1] - 2*original[i-1][j] - original[i-1][j+1];
Gx[i][j] =
original[i-1][j+1] + 2*original[i][j+1] + original[i+1][j+1] -
original[i-1][j-1] - 2*original[i][j-1] - original[i+1][j-1];
//公式(soble = sqrt[(x^2+y^2)])
G[i][j] =(int)(Math.sqrt((Math.pow(Gx[i][j],2) + Math.pow(Gy[i][j],2))));
}
pixels[count2] = (0xff000000 | G[i][j] << 16 | G[i][j] << 8 | G[i][j]);
count2 ++;
}
}
BufferedImage sobel_image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); //image 轉 BfferedImage
sobel_image.setRGB(0, 0, width, height, pixels, 0, width);
try {
File file_sobel = new File("sobel.bmp");
ImageIO.write(sobel_image, "bmp", file_sobel);
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}