好的,这就是最终我想到的解决方案。
我将所有的数组[][]转换为BufferedImage对象:
static BufferedImage BImageFrom2DArray(float data[][]) {
int width = data.length;
int height = data[0].length;
BufferedImage myimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int value = (int) ((1f - data[x][y]) * 255f);
myimage.setRGB(y, x, (value << 16) | (value << 8) | value);
}
}
return myimage;
}
使用AffineTransformOp应用仿射变换,插值方法为双三次:
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BICUBIC);
BufferedImage im_transformed = op.filter(im_src, null);
将BufferedImage对象转换回数组[][]:
static float[][] ArrayFromBImage(BufferedImage bimage, int width, int height) {
int max_x = bimage.getWidth();
int max_y = bimage.getHeight();
float[][] array = new float[width][height];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
float red, alpha, value;
int color;
if (x >= max_x || y >= max_y) {
array[y][x] = 0;
} else {
color = bimage.getRGB(x, y);
alpha = (color >> 24) & 0xFF;
red = (color >> 16) & 0xFF;
value = 1f - red / 255;
if (alpha == 0) {
array[y][x] = 0;
} else {
array[y][x] = value;
}
}
}
}
return array;
}