2

我已经用 Python(来自 Matlab 的)编写了 Hilbert-Peano 空间填充曲线的实现来展平我的 2D 图像:

def hilbert_peano(n):
    if n<=0:
        x=0
        y=0
    else:
        [x0, y0] = hilbert_peano(n-1)
        x = (1/2) * np.array([-0.5+y0, -0.5+x0, 0.5+x0, 0.5-y0])
        y = (1/2) * np.array([-0.5+x0, 0.5+y0, 0.5+y0, -0.5-y0])

    return x,y

但是,经典的 Hilbert-Peano 曲线仅适用于形状为 2 的幂的多维阵列(例如:在二维阵列(图像)的情况下为 256*256 或 512*512)。

有人知道如何将其扩展到任意大小的数组吗?

4

3 回答 3

2

我遇到了同样的问题,并编写了一个算法,可以为 2D 和 3D 中任意大小的矩形生成类似希尔伯特的曲线。55x31 示例:curve55x31

这个想法是递归地应用类似希尔伯特的模板,但在将域尺寸减半时避免奇怪的尺寸。如果维度恰好是 2 的幂,则生成经典的希尔伯特曲线。

def gilbert2d(x, y, ax, ay, bx, by):
    """
    Generalized Hilbert ('gilbert') space-filling curve for arbitrary-sized
    2D rectangular grids.
    """

    w = abs(ax + ay)
    h = abs(bx + by)

    (dax, day) = (sgn(ax), sgn(ay)) # unit major direction
    (dbx, dby) = (sgn(bx), sgn(by)) # unit orthogonal direction

    if h == 1:
        # trivial row fill
        for i in range(0, w):
            print x, y
            (x, y) = (x + dax, y + day)
        return

    if w == 1:
        # trivial column fill
        for i in range(0, h):
            print x, y
            (x, y) = (x + dbx, y + dby)
        return

    (ax2, ay2) = (ax/2, ay/2)
    (bx2, by2) = (bx/2, by/2)

    w2 = abs(ax2 + ay2)
    h2 = abs(bx2 + by2)

    if 2*w > 3*h:
        if (w2 % 2) and (w > 2):
            # prefer even steps
            (ax2, ay2) = (ax2 + dax, ay2 + day)

        # long case: split in two parts only
        gilbert2d(x, y, ax2, ay2, bx, by)
        gilbert2d(x+ax2, y+ay2, ax-ax2, ay-ay2, bx, by)

    else:
        if (h2 % 2) and (h > 2):
            # prefer even steps
            (bx2, by2) = (bx2 + dbx, by2 + dby)

        # standard case: one step up, one long horizontal, one step down
        gilbert2d(x, y, bx2, by2, ax2, ay2)
        gilbert2d(x+bx2, y+by2, ax, ay, bx-bx2, by-by2)
        gilbert2d(x+(ax-dax)+(bx2-dbx), y+(ay-day)+(by2-dby),
                 -bx2, -by2, -(ax-ax2), -(ay-ay2))

def main():
    width = int(sys.argv[1])
    height = int(sys.argv[2])

    if width >= height:
        gilbert2d(0, 0, width, 0, 0, height)
    else:
        gilbert2d(0, 0, 0, height, width, 0)

可在https://github.com/jakubcerveny/gilbert获得 3D 版本和更多文档

于 2019-10-28T15:50:56.313 回答
1

我找到了 Lutz Tautenhahn 的这个页面:

“绘制任意大小的空间填充曲线”(http://lutanho.net/pic2html/draw_sfc.html

该算法没有名称,他没有引用其他任何人,草图表明他自己想出了它。

我想知道这是否适用于 az 阶曲线以及如何实现?

[1]绘制任意大小的空间填充曲线

于 2016-07-19T16:08:39.467 回答
0

正如 Betterdev 所建议的那样,我最终选择了自适应曲线不是那么简单 [1],来计算更大的曲线,然后去掉我的图像形状之外的坐标:

# compute the needed order
order = np.max(np.ceil([np.log2(M), np.log2(N)]))
# Hilbert curve to scan a 2^order * 2^order image
x, y = hilbert_peano(order)
mat = np.zeros((2**order, 2**order))
# curve as a 2D array
mat[x, y] = np.arange(0, x.size, dtype=np.uint)
# clip the curve to the image shape
mat = mat[:M, :N]
# compute new indices (from 0 to M*N)
I = np.argsort(mat.flat)
x_new, y_new = np.meshgrid(np.arange(0, N, dtype=np.uint), np.arange(0, M, dtype=np.uint))
# apply the new order to the grid
x_new = x_new.flat[I]
y_new = y_new.flat[I]

[1] Zhang J.、Kamata S. 和 Ueshige Y.,“任意大小矩形区域的伪希尔伯特扫描算法”

于 2016-07-21T13:57:08.983 回答