Hilbert-Peano 曲线扫描任意大小的图像

Hilbert-Peano curve to scan image of arbitrary size

我已经在 Python 中编写了 Hilbert-Peano space 填充曲线的实现(来自 Matlab)来展平我的 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 二维数组(图像))。

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

我通过 Lutz Tautenhahn 找到了这个页面:

"Draw A Space-Filling Curve of Arbitrary Size" (http://lutanho.net/pic2html/draw_sfc.html)

算法没有名字,他没有引用任何其他人,草图表明他自己想出了这个算法。

我想知道这对于 z 顺序曲线是否可行以及如何实现?

[1]Draw A Space-Filling Curve of Arbitrary Size

我最终选择了,正如 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.,"A Pseudo-Hilbert Scan Algorithm for Arbitrarily-Sized Rectangle Region"

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

想法是递归地应用类 Hilbert 模板,但在将域维度减半时避免奇数大小。如果维度恰好是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 版本和更多文档