样条曲线形状的抗锯齿图像

时间:2021-05-29 15:20:58

标签: python

我的目标是创建 3 个图像,一个是随机样条的抗锯齿图像,然后是另外两个在 0 和 1 之间缩放的图像,用于显示样条在每个点的“水平”或“垂直”程度。

from scipy.interpolate import CubicSpline, griddata
import numpy as np
import matplotlib.pyplot as plt


def create_random_line():

    # Create random spline
    x = np.array([1, 15, 30, 49])
    y = np.random.uniform(1, 50, 4)
    f = CubicSpline(x, y, bc_type='natural')

    x_new = np.linspace(0, 49, 100)
    y_new = f(x_new)
    y_new_deriv = f(x_new, 1)
    y_angles = np.array([math.atan2(tt, 1) for tt in y_new_deriv])

    # Plot the spline, derivative and angle
    plt.figure(2)
    plt.clf()
    plt.subplot(3,1,1)
    plt.plot(x, y, 'x')
    plt.xlim((0, 50))
    plt.ylim((0, 50))
    plt.plot(x_new, y_new)
    plt.subplot(3,1,2)
    plt.plot(x_new, y_new_deriv)
    plt.subplot(3,1,3)
    plt.plot(x_new, np.rad2deg(y_angles))
    plt.ylim((-90, 90))
    plt.show()

    # Create image of spline
    image = np.zeros((50, 50))
    scaled_angle_maps = np.zeros((50, 50, 2))
    for xx, yy, rr in zip(y_new, x_new, np.rad2deg(y_angles)):

        image[int(np.round(xx)), int(np.round(yy))] = 1
        scaled_angle_maps[int(np.round(xx)), int(np.round(yy)), 0] = np.clip(1 - (np.abs(rr)/90), 0, 1)
        scaled_angle_maps[int(np.round(xx)), int(np.round(yy)), 1] = np.clip(np.mod(np.abs(rr),90)/90, 0, 1)

    return image, scaled_angle_maps

# Create random spline image
image, scaled_angle_maps = create_random_line()

# Plot
plt.figure(1)
plt.clf()
plt.subplot(2,2,1)
plt.imshow(image)
plt.gray()
plt.colorbar()
plt.ylim((0,50))

plt.subplot(2,2,3)
plt.imshow(scaled_angle_maps[:,:,0])
plt.ylim((0,50))
plt.colorbar()
plt.title('horizontal')

plt.subplot(2,2,4)
plt.imshow(scaled_angle_maps[:,:,1])
plt.ylim((0,50))
plt.colorbar()
plt.title('vertical')
plt.show()

image of spline

但是,我想要这种抗锯齿。我一直在阅读 Wu 的算法,但大多数实现似乎都是针对直线的。我尝试从 matplotlib 画布创建它,但效果不佳。

然后,第二,我希望图像中的样条具有任意厚度,尽管我想我可以在创建图像后scipy.ndimage.grey_dilation

那么,我是否缺少一种创建随机样条图像的简单方法?感觉应该有一个更简单的方法来做到这一点。

0 个答案:

没有答案