-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageVisualizer.java
More file actions
482 lines (421 loc) · 18.7 KB
/
Copy pathImageVisualizer.java
File metadata and controls
482 lines (421 loc) · 18.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
package heyingzhe;
import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
import java.util.Map;
import java.util.HashMap;
import java.awt.Color;
public class ImageVisualizer extends JFrame {
private BufferedImage image;
// 生成普通灰度图像
private void createImage(int[][] data) {
int max = Integer.MIN_VALUE;
int min = Integer.MAX_VALUE;
for (int[] datum : data) {
for (int j = 0; j < datum.length; j++) {
if (datum[j] != -9999) {
max = Math.max(max, datum[j]);
min = Math.min(min, datum[j]);
}
}
}
double scale = 255.0 / (max - min);
image = new BufferedImage(data[0].length, data.length, BufferedImage.TYPE_INT_RGB);
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
if (data[i][j] != -9999) {
int value = (int)((data[i][j] - min) * scale);
image.setRGB(j, i, getColor(value).getRGB());
}
}
}
}
// 根据数值设置颜色
private Color getColor(int value) {
int threshold = 70;
Color color;
if (value < threshold) {
double ratio = (double) value / threshold;
int r = (int) (122 + (ratio * (227 - 122)));
int g = (int) (170 + (ratio * (227 - 170)));
int b = (int) (128 + (ratio * (181 - 128)));
color = new Color(r, g, b);
} else {
double ratio = (double) (value - threshold) / (255 - threshold);
int r = (int) (227 + (ratio * (225 - 227)));
int g = (int) (227 + (ratio * (119 - 227)));
int b = (int) (181 + (ratio * (7 - 181)));
color = new Color(r, g, b);
}
return color;
}
// 对double型数组进行可视化 设置为灰度图
public void convertArrayToImage(double[][] array) {
int width = array[0].length;
int height = array.length;
// 查找数组的最大值和最小值
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
min = Math.min(min, array[i][j]);
max = Math.max(max, array[i][j]);
}
}
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 将数组的值转换为0到255的灰度值,并设置到图像中
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int gray = (int) ((array[i][j] - min) / (max - min) * 255);
int rgb = (gray << 16) | (gray << 8) | gray;
image.setRGB(j, i, rgb);
}
}
}
// 对int型数组可视化为灰度图
public void convertArrayToImage(int[][] array) {
int width = array[0].length;
int height = array.length;
// 查找数组的最大值和最小值
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (array[i][j] != -9999) {
min = Math.min(min, array[i][j]);
max = Math.max(max, array[i][j]);
}
}
}
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 将数组的值转换为0到255的灰度值,并设置到图像中
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (array[i][j] != -9999) {
int gray = (int) ((array[i][j] - min) / (double)(max - min) * 255);
int rgb = (gray << 16) | (gray << 8) | gray;
image.setRGB(j, i, rgb);
}
}
}
}
// 可视化流域图
public void createWaterAreaImage(int[][] array, int maxValue) {
int width = array[0].length;
int height = array.length;
// 查找数组的最小值(有效值)
int min = Integer.MAX_VALUE;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (array[i][j] != -9999 && array[i][j] <= maxValue) {
min = Math.min(min, array[i][j]);
}
}
}
// 创建图像对象
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 遍历数组,将有效值映射为灰度,并绘制到图像中
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (array[i][j] != -9999 && array[i][j] <= maxValue) {
// 计算灰度值
int gray = (int) ((array[i][j] - min) / (double) (maxValue - min) * 255);
gray = Math.max(0, Math.min(255, gray)); // 确保灰度值在0-255范围内
// 将灰度值转换为RGB格式
int rgb = (gray << 16) | (gray << 8) | gray;
image.setRGB(j, i, rgb);
} else {
// 对于无效值或超出最大值的值,绘制为黑色
image.setRGB(j, i, 0x000000);
}
}
}
}
// 可视化流域图
public void createWaterAreaImage(double[][] array, double maxValue) {
int width = array[0].length;
int height = array.length;
// 查找数组的最大值和最小值
double min = Double.MAX_VALUE;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (array[i][j] != -9999 && array[i][j] <= maxValue) {
min = Math.min(min, array[i][j]);
}
}
}
double max = maxValue;
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 将数组的值转换为0到255的灰度值,并设置到图像中
// 遍历数组,将有效值映射为灰度,并绘制到图像中
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (array[i][j] != -9999 && array[i][j] <= maxValue) {
// 计算灰度值
int gray = (int) ((array[i][j] - min) / (double) (maxValue - min) * 255);
gray = Math.max(0, Math.min(255, gray)); // 确保灰度值在0-255范围内
// 将灰度值转换为RGB格式
int rgb = (gray << 16) | (gray << 8) | gray;
image.setRGB(j, i, rgb);
} else {
// 对于无效值或超出最大值的值,绘制为黑色
image.setRGB(j, i, 0x000000);
}
}
}
}
// 创建流向图
public void createFlowDirectionImage(int[][] flowDirectionData) {
// 定义流向和颜色的映射关系
Map<Integer, Color> flowDirectionToColor = new HashMap<>();
flowDirectionToColor.put(0, Color.WHITE);
flowDirectionToColor.put(1, Color.RED);
flowDirectionToColor.put(2, Color.ORANGE);
flowDirectionToColor.put(4, Color.YELLOW);
flowDirectionToColor.put(8, Color.GREEN);
flowDirectionToColor.put(16, Color.BLUE);
flowDirectionToColor.put(32, Color.CYAN);
flowDirectionToColor.put(64, Color.MAGENTA);
flowDirectionToColor.put(128, Color.BLACK);
int width = flowDirectionData[0].length;
int height = flowDirectionData.length;
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 遍历流向数据,根据流向设置像素颜色
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int flowDirection = flowDirectionData[i][j];
Color color = flowDirectionToColor.getOrDefault(flowDirection, Color.WHITE);
image.setRGB(j, i, color.getRGB());
}
}
}
/**
* 渐变图 累积流
* 该方法将累积流数组中大于阈值的部分转为渐变图
* @param accumulationArray
* @param threshold
*/
public void convertFlowAccumulationToImage(double[][] accumulationArray, double threshold) {
int width = accumulationArray[0].length;
int height = accumulationArray.length;
// 查找非负值的最小值和最大值
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (accumulationArray[i][j] >= 0) { // 只考虑非负值
min = Math.min(min, accumulationArray[i][j]);
max = Math.max(max, accumulationArray[i][j]);
}
}
}
// 如果没有找到有效值,则返回空图像
if (min == Double.MAX_VALUE || max == Double.MIN_VALUE) {
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
return;
}
// 计算阈值为最大值和最小值的中间值
//double threshold = (max + min) / 2;
// 创建 BufferedImage 对象
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 将大于阈值的部分值映射为灰度值
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (accumulationArray[i][j] < threshold) {
// 如果值小于阈值,设置为黑色(无色)
image.setRGB(j, i, 0x000000); // 黑色表示无色
} else {
// 映射到灰度值,数值越接近阈值越黑,越接近最大值越白
int gray = (int) ((accumulationArray[i][j] - threshold) / (max - threshold) * 255);
gray = Math.max(0, Math.min(255, gray)); // 确保灰度值在 0-255 之间
int rgb = (gray << 16) | (gray << 8) | gray; // 将灰度值转换为RGB格式
image.setRGB(j, i, rgb);
}
}
}
}
/**
* 二值图 累积流
* 该函数将累积流数组中大于阈值的部分转化为二值图
* @param accumulationArray 累积流数组
* @param threshold 绘制图像中的最小累积流
*/
public void convertFlowAccumulationToBWImage(double[][] accumulationArray, double threshold) {
int width = accumulationArray[0].length;
int height = accumulationArray.length;
// 创建 BufferedImage 对象
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 遍历累积流数组,并根据阈值设置像素颜色
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
// 如果累积流大于阈值,绘制成白色,否则绘制成黑色
if (accumulationArray[i][j] > threshold) {
image.setRGB(j, i, 0xFFFFFF); // 白色
} else {
image.setRGB(j, i, 0x000000); // 黑色
}
}
}
}
/*
* 上色方法,传入二维数组和RGB起终色
* @parameters:二维数组,颜色
* @Color startColor =
* Color startColor = Color.decode("#09fff"); // 蓝色
* Color endColor = Color.decode("#ec2f4b"); // 红色
* imageProcessor.colorize(data, startColor, endColor);
*/
public void colorize(double[][] data, Color startColor, Color endColor) {
int rows = data.length;
int cols = data[0].length;
// 计算数据的最小值和最大值
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (data[i][j] < min) {
min = data[i][j];
}
if (data[i][j] > max) {
max = data[i][j];
}
}
}
// 创建一个新的BufferedImage
image = new BufferedImage(cols, rows, BufferedImage.TYPE_INT_RGB);
// 为每个点上色
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// 获取当前点的值
double value = data[i][j];
// 计算当前值在最小值和最大值之间的归一化百分比
double normalized = (value - min) / (max - min);
// 根据归一化值插值计算当前颜色
Color color = interpolateColor(startColor, endColor, normalized);
// 设置当前像素点的颜色
image.setRGB(j, i, color.getRGB());
}
}
}
// 分级上色方法,传入二维数组和RGB起终色,以及等级数量
public void colorizeWithLevels(double[][] data, Color startColor, Color endColor, int levels) {
int rows = data.length;
int cols = data[0].length;
// 计算数据的最小值和最大值
double min = Double.MAX_VALUE;
double max = Double.MIN_VALUE;
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (data[i][j] < min) {
min = data[i][j];
}
if (data[i][j] > max) {
max = data[i][j];
}
}
}
// 创建一个新的BufferedImage
image = new BufferedImage(cols, rows, BufferedImage.TYPE_INT_RGB);
// 计算每个等级的数值区间
double range = max - min;
double levelSize = range / levels;
// 为每个点上色
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// 获取当前点的值
double value = data[i][j];
// 根据数值所在的等级来选择颜色
int level = (int) ((value - min) / levelSize);
level = Math.min(level, levels - 1); // 确保不超出最大等级
// 根据等级和颜色比例计算颜色
Color color = interpolateColor(startColor, endColor, (double) level / (levels - 1));
// 设置当前像素点的颜色
image.setRGB(j, i, color.getRGB());
}
}
}
// 分级上色方法,传入二维数组和RGB起终色,以及等级数量
public void colorizeWithLevelsMonth(double[][] data, Color startColor, Color endColor, int levels, double min,
double max) {
int rows = data.length;
int cols = data[0].length;
// 创建一个新的BufferedImage
image = new BufferedImage(cols, rows, BufferedImage.TYPE_INT_RGB);
// 计算每个等级的数值区间
double range = max - min;
double levelSize = range / levels;
// 为每个点上色
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// 获取当前点的值
double value = data[i][j];
// 根据数值所在的等级来选择颜色
int level = (int) ((value - min) / levelSize);
level = Math.min(level, levels - 1); // 确保不超出最大等级
// 根据等级和颜色比例计算颜色
Color color = interpolateColor(startColor, endColor, (double) level / (levels - 1));
// 设置当前像素点的颜色
image.setRGB(j, i, color.getRGB());
}
}
}
// 计算两种颜色之间的渐变色
private Color interpolateColor(Color startColor, Color endColor, double ratio) {
int r = (int) (startColor.getRed() + (endColor.getRed() - startColor.getRed()) * ratio);
int g = (int) (startColor.getGreen() + (endColor.getGreen() - startColor.getGreen()) * ratio);
int b = (int) (startColor.getBlue() + (endColor.getBlue() - startColor.getBlue()) * ratio);
return new Color(r, g, b);
}
@Override
public void paint(Graphics g) {
g.drawImage(image, 0, 0, this);
}
public void saveImage(String path, String format) throws IOException {
File outputfile = new File(path);
ImageIO.write(image, format, outputfile);
}
public void showImage() {
// Calculate the window size as a fraction of the image size
double scale = 0.5; // Set this to a value less than 1
int windowWidth = (int) (image.getWidth() * scale);
int windowHeight = (int) (image.getHeight() * scale);
// Ensure the window is not larger than the screen
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
windowWidth = Math.min(windowWidth, screenSize.width);
windowHeight = Math.min(windowHeight, screenSize.height);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setSize(windowWidth, windowHeight);
setLocationRelativeTo(null);
setVisible(true);
}
/**
* 提取河流线并修改 BufferedImage。
*
* @param dem DEM 数组 (int[][])
* @param flowDirection 流向数组 (int[][])
* @param threshold 海拔阈值
*/
public void extractRiverLines(int[][] dem, int[][] flowDirection, int threshold) {
int height = dem.length;
int width = dem[0].length;
// 初始化 BufferedImage 对象
image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 遍历数组,根据条件绘制像素
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (dem[i][j] == -9999) {
// 无数据区域,绘制为黑色
image.setRGB(j, i, Color.BLACK.getRGB());
} else if (dem[i][j] < threshold && flowDirection[i][j] == 0) {
// 满足条件:海拔小于阈值且流向为 0,绘制为白色
image.setRGB(j, i, Color.WHITE.getRGB());
} else {
// 其他情况,绘制为黑色
image.setRGB(j, i, Color.BLACK.getRGB());
}
}
}
}
}