使用希尔伯特曲线分析图像
本文关键字:曲线 图像 | 更新日期: 2023-09-27 18:33:23
我想使用希尔伯特曲线的"路径"访问图像的每个像素。我在这里找到了希尔伯特-cuve的递归实现,但我不知道如何在图像上应用它。有一张宽度和高度相等的图片重要吗?
否,但请确保曲线是自适应的或大于图像。自适应曲线非常复杂。
这是一种为非平方数组生成类似希尔伯特曲线的算法。
这个想法是递归应用类似希尔伯特的模板,但在将域维度减半时避免奇数大小。如果维度恰好是 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))
更多信息、示例等,可在此处获得:https://github.com/jakubcerveny/gilbert