-1

经过两天的搜索和尝试,我无法在 java 中实现希尔伯特曲线。

我需要将(例如)64 像素填充到基于希尔伯特曲线的正方形 8x8 图像中。我根据 Zig-Zog 曲线做了同样的事情,但是(我想做一些特征提取)结果并不令人满意。

结果:

结果

有人可以帮帮我吗?

编辑:

例如,如果我要编写一个函数来返回基于 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;
    }
}
}
4

1 回答 1

0

好吧,似乎没有人对我的问题感兴趣,我必须自己做。

此代码自爆可能会返回索引顺序,基于 Hilbert-Curve,我将它从此处提供的 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;
}

}

享受!

于 2015-07-06T22:42:42.413 回答