如何展开螺旋图形?

2024-09-29 19:29:51 发布

您现在位置:Python中文网/ 问答频道 /正文

螺旋线是围绕中心轴旋转的平面曲线

spiral

我有一张基于螺旋的帕金森病患者的画

drawing

如您所见,患者绘图的图像围绕底部螺旋线摆动。我想做的是:“展开”螺旋,这样绘图的振荡和螺旋本身都基于一条直线,也就是说,将螺旋线性化。我该怎么做


Tags: 图像患者绘图中心直线线性化螺旋螺旋线
1条回答
网友
1楼 · 发布于 2024-09-29 19:29:51

这里有两个部分的可能方法

第一部分尝试将螺旋线与图像对齐。最简单的缓和曲线是阿基米德缓和曲线,其中半径和角度线性耦合。通过绘制和查看坐标,可以找到x和y方向的近似比例限制。结果并不完美。也许给定的图像没有被很好地扫描,但仅仅是一张导致变形的照片,或者原始的螺旋不是一个完美的阿基米德螺旋。(另外,强烈建议使用png文件而不是给定的jpg)。不管怎么说,这个尺度足够好,可以让我们了解算法的工作原理,最好是从精确的扫描开始

下一部分将遍历图像的每个像素,并找到与中心相对应的角度和距离(使用第一部分中的缩放)。下一步是找出角度已经旋转了多少次(2π的倍数),选择最接近的匹配。从角度减去半径将使螺旋变直

一些代码来说明这个想法

import numpy as np
from matplotlib import pyplot as plt
import imageio

fig, ax = plt.subplots()

img = imageio.imread('spiral_follow.jpg')
# the image extent is set using trial and error to align with the spiral equation;
# the center of the spiral should end up with coordinates 0,0
x0, x1, y0, y1 = extent = [-17.8, 16, 13, -16.8]
ax.imshow(img, extent=extent)

# t=17.4 is about where the spiral ends; the exact value is not important
t = np.linspace(0, 17.4, 1000) 
r = t
theta = t
sx = r * np.sin(theta)
sy = r * np.cos(theta)

ax.plot(sx, sy, c='lime', lw=6, alpha=0.4) # plot the spiral over the image

# r_p and theta_p are the polar coordinates of the white pixels
r_p = []
theta_p = []
for i in range(img.shape[0]):
    for j in range(img.shape[1]):
        if img[i,j] > 127: # the black pixels are 0, the white are 255; 127 is the middle
            # transform from pixel coordinates (i,j) to the coordinatets of the spiral (x,y)
            x = x0 + j * (x1 - x0) / (img.shape[1] - 1)
            y = y1 + i * (y0 - y1) / (img.shape[0] - 1)
            # transform from carthesian (x,y) to polar coordinates (theta,r)
            theta = np.arctan2(x, y)
            r = np.sqrt(x*x+y*y)
            # the following code tries to find out how many times 2pi should be added to theta
            #   in order to correspond best to r
            k = np.round((r - theta) / (2 * np.pi))
            nearest_theta = theta + 2 * k * np.pi
            theta_p.append(nearest_theta)
            r_p.append(nearest_theta - r)
plt.show()

fig, ax = plt.subplots()
ax.set_facecolor('black')
ax.scatter(theta_p, r_p, s=1, lw=0, color='white')
plt.show()

对齐的螺旋线:

aligned spiral

拉直的螺旋线:

straightened spiral

相关问题 更多 >

    热门问题