Java 中基于希尔伯特曲线的索引顺序
Indexing order based on Hilbert curve in Java
经过两天的搜索和尝试,我无法在 java.
中实现 Hilbert Curve
我需要根据希尔伯特曲线将(例如)64 像素填充到 8x8 正方形图像中。我根据 Zig-Zog 曲线做了同样的事情,但是(因为我想做一些特征提取)result 并不令人满意。
结果:
有人能帮帮我吗?
编辑:
例如,如果我要编写一个函数来 return 基于 Zig-Zog 曲线的方阵索引,它将是这样的:
public class Source {
public static void main(String[] args) {
int matrixWidth = 4;
for (int col = 0; col < matrixWidth; col++){
for (int row = 0; row < matrixWidth; row++){
int[] temp = zigZog(row, col, matrixWidth);
System.out.println(temp[0] + ", " + temp[1]);
}
}
}
// >>> zig-zog indexing
public static int[] zigZog(int row, int col, int matrixWidth){
if (col%2 == 1){
int[] temp = {((matrixWidth -1) - row), col};
return temp;
} else {
int[] temp = {row, col};
return temp;
}
}
}
嗯,好像没人对我的问题感兴趣,我只好自己做。
此代码可能 return 指数顺序,基于希尔伯特曲线,我将其从 here 中提供的 python 代码转换而来(坦率地说,它需要我一会儿将其转换为 Java [那里不存在有用的自动转换器],python 真的很难理解,它似乎没有结构):
public class Source {
public static void main(String[] args) {
int current_square_num = 4;
int row_num = 2;
int col_num = 2;
int arg_num = 2;
char[][][][] hilbert_map = new char[current_square_num][row_num][col_num][arg_num];
// >>> 'a': {(0, 0): (0, 'd'), (0, 1): (1, 'a'), (1, 0): (3, 'b'), (1, 1): (2, 'a')}
hilbert_map[0][0][0][0] = '0';
hilbert_map[0][0][0][1] = 'd';
hilbert_map[0][0][1][0] = '1';
hilbert_map[0][0][1][1] = 'a';
hilbert_map[0][1][0][0] = '3';
hilbert_map[0][1][0][1] = 'b';
hilbert_map[0][1][1][0] = '2';
hilbert_map[0][1][1][1] = 'a';
// >>> 'b': {(0, 0): (2, 'b'), (0, 1): (1, 'b'), (1, 0): (3, 'a'), (1, 1): (0, 'c')}
hilbert_map[1][0][0][0] = '2';
hilbert_map[1][0][0][1] = 'b';
hilbert_map[1][0][1][0] = '1';
hilbert_map[1][0][1][1] = 'b';
hilbert_map[1][1][0][0] = '3';
hilbert_map[1][1][0][1] = 'a';
hilbert_map[1][1][1][0] = '0';
hilbert_map[1][1][1][1] = 'c';
// >>> 'c': {(0, 0): (2, 'c'), (0, 1): (3, 'd'), (1, 0): (1, 'c'), (1, 1): (0, 'b')}
hilbert_map[2][0][0][0] = '2';
hilbert_map[2][0][0][1] = 'c';
hilbert_map[2][0][1][0] = '3';
hilbert_map[2][0][1][1] = 'd';
hilbert_map[2][1][0][0] = '1';
hilbert_map[2][1][0][1] = 'c';
hilbert_map[2][1][1][0] = '0';
hilbert_map[2][1][1][1] = 'b';
// >>> 'd': {(0, 0): (0, 'a'), (0, 1): (3, 'c'), (1, 0): (1, 'd'), (1, 1): (2, 'd')}
hilbert_map[3][0][0][0] = '0';
hilbert_map[3][0][0][1] = 'a';
hilbert_map[3][0][1][0] = '3';
hilbert_map[3][0][1][1] = 'c';
hilbert_map[3][0][1][0] = '1';
hilbert_map[3][0][1][1] = 'd';
hilbert_map[3][1][1][0] = '2';
hilbert_map[3][1][1][1] = 'd';
double order = 3;
for (int col = 0; col < Math.pow(2, order); col++) {
for (int row = 0; row < Math.pow(2, order); row++) {
System.out.println("(" + row + ", " + col + "): " + point_to_hilbert(row, col, (int) order, hilbert_map));
}
}
}
public static int point_to_hilbert(int x, int y, int order, char[][][][] hilbert_map) {
int position = 0;
int current_square = 0 /* 'a' */;
for (int i = 0; i < order; i++) {
position = position << 2; // >>> position <<= 2
int quad_x;
int quad_y;
if ((x & (1 << i)) != 0) { // >>> quad_x = 1 if x & (1 << i) else 0
quad_x = 1;
} else {
quad_x = 0;
}
if ((y & (1 << i)) != 0) { // >>> quad_y = 1 if y & (1 << i) else 0
quad_y = 1;
} else {
quad_y = 0;
}
current_square = 0;
// >>> quad_position, current_square = hilbert_map[current_square][(quad_x, quad_y)]
char quad_position = hilbert_map[current_square][quad_x][quad_y][0];
current_square = Character.getNumericValue(hilbert_map[current_square][quad_x][quad_y][1]);
// >>> position |= quad_position
position = position | Character.getNumericValue(quad_position);
}
return position;
}
}
尽情享受吧!
经过两天的搜索和尝试,我无法在 java.
中实现 Hilbert Curve我需要根据希尔伯特曲线将(例如)64 像素填充到 8x8 正方形图像中。我根据 Zig-Zog 曲线做了同样的事情,但是(因为我想做一些特征提取)result 并不令人满意。
结果:
有人能帮帮我吗?
编辑:
例如,如果我要编写一个函数来 return 基于 Zig-Zog 曲线的方阵索引,它将是这样的:
public class Source {
public static void main(String[] args) {
int matrixWidth = 4;
for (int col = 0; col < matrixWidth; col++){
for (int row = 0; row < matrixWidth; row++){
int[] temp = zigZog(row, col, matrixWidth);
System.out.println(temp[0] + ", " + temp[1]);
}
}
}
// >>> zig-zog indexing
public static int[] zigZog(int row, int col, int matrixWidth){
if (col%2 == 1){
int[] temp = {((matrixWidth -1) - row), col};
return temp;
} else {
int[] temp = {row, col};
return temp;
}
}
}
嗯,好像没人对我的问题感兴趣,我只好自己做。
此代码可能 return 指数顺序,基于希尔伯特曲线,我将其从 here 中提供的 python 代码转换而来(坦率地说,它需要我一会儿将其转换为 Java [那里不存在有用的自动转换器],python 真的很难理解,它似乎没有结构):
public class Source {
public static void main(String[] args) {
int current_square_num = 4;
int row_num = 2;
int col_num = 2;
int arg_num = 2;
char[][][][] hilbert_map = new char[current_square_num][row_num][col_num][arg_num];
// >>> 'a': {(0, 0): (0, 'd'), (0, 1): (1, 'a'), (1, 0): (3, 'b'), (1, 1): (2, 'a')}
hilbert_map[0][0][0][0] = '0';
hilbert_map[0][0][0][1] = 'd';
hilbert_map[0][0][1][0] = '1';
hilbert_map[0][0][1][1] = 'a';
hilbert_map[0][1][0][0] = '3';
hilbert_map[0][1][0][1] = 'b';
hilbert_map[0][1][1][0] = '2';
hilbert_map[0][1][1][1] = 'a';
// >>> 'b': {(0, 0): (2, 'b'), (0, 1): (1, 'b'), (1, 0): (3, 'a'), (1, 1): (0, 'c')}
hilbert_map[1][0][0][0] = '2';
hilbert_map[1][0][0][1] = 'b';
hilbert_map[1][0][1][0] = '1';
hilbert_map[1][0][1][1] = 'b';
hilbert_map[1][1][0][0] = '3';
hilbert_map[1][1][0][1] = 'a';
hilbert_map[1][1][1][0] = '0';
hilbert_map[1][1][1][1] = 'c';
// >>> 'c': {(0, 0): (2, 'c'), (0, 1): (3, 'd'), (1, 0): (1, 'c'), (1, 1): (0, 'b')}
hilbert_map[2][0][0][0] = '2';
hilbert_map[2][0][0][1] = 'c';
hilbert_map[2][0][1][0] = '3';
hilbert_map[2][0][1][1] = 'd';
hilbert_map[2][1][0][0] = '1';
hilbert_map[2][1][0][1] = 'c';
hilbert_map[2][1][1][0] = '0';
hilbert_map[2][1][1][1] = 'b';
// >>> 'd': {(0, 0): (0, 'a'), (0, 1): (3, 'c'), (1, 0): (1, 'd'), (1, 1): (2, 'd')}
hilbert_map[3][0][0][0] = '0';
hilbert_map[3][0][0][1] = 'a';
hilbert_map[3][0][1][0] = '3';
hilbert_map[3][0][1][1] = 'c';
hilbert_map[3][0][1][0] = '1';
hilbert_map[3][0][1][1] = 'd';
hilbert_map[3][1][1][0] = '2';
hilbert_map[3][1][1][1] = 'd';
double order = 3;
for (int col = 0; col < Math.pow(2, order); col++) {
for (int row = 0; row < Math.pow(2, order); row++) {
System.out.println("(" + row + ", " + col + "): " + point_to_hilbert(row, col, (int) order, hilbert_map));
}
}
}
public static int point_to_hilbert(int x, int y, int order, char[][][][] hilbert_map) {
int position = 0;
int current_square = 0 /* 'a' */;
for (int i = 0; i < order; i++) {
position = position << 2; // >>> position <<= 2
int quad_x;
int quad_y;
if ((x & (1 << i)) != 0) { // >>> quad_x = 1 if x & (1 << i) else 0
quad_x = 1;
} else {
quad_x = 0;
}
if ((y & (1 << i)) != 0) { // >>> quad_y = 1 if y & (1 << i) else 0
quad_y = 1;
} else {
quad_y = 0;
}
current_square = 0;
// >>> quad_position, current_square = hilbert_map[current_square][(quad_x, quad_y)]
char quad_position = hilbert_map[current_square][quad_x][quad_y][0];
current_square = Character.getNumericValue(hilbert_map[current_square][quad_x][quad_y][1]);
// >>> position |= quad_position
position = position | Character.getNumericValue(quad_position);
}
return position;
}
}
尽情享受吧!