当只给出两个值时,interpolation="bicubic"
会进行一些外推.相反,您可以使用interpolation="bilinear"
.
要获得三次曲线的插值值,可以将[[0, 1], [0, 1]]
更改为三次函数.
以下是提取每种情况的像素值并将其显示为曲线的代码.
import matplotlib.pyplot as plt
import seaborn as sns
import numpy as np
fig, axs = plt.subplots(ncols=3, nrows=2, sharey='row', figsize=(15, 6))
for i, interp in enumerate(['bicubic', 'bilinear', 'bilinear']):
if i < 2:
gradient = [[0, 1], [0, 1]]
else:
x = np.linspace(0, 1, 100)
gradient = ((3 - 2 * x) * x * x).reshape(1, -1)
img = axs[0, i].imshow(gradient,
interpolation=interp,
cmap=sns.cubehelix_palette(start=0, rot=0, hue=0, light=1, dark=0.7, as_cmap=True),
extent=[0, 1, 0, 1],
alpha=1, aspect="auto")
axs[0, i].set_title(f'interpolation={interp} {"linear" if i < 2 else "cubic"} gradient')
axs[0, i].set_xticks([])
axs[0, i].set_yticks([])
# show the color value of the central row as a curve
data = img.make_image(renderer=None)[0] # get image data as a numpy NxMx4 array
values = data[data.shape[0] // 2, :, :3].mean(axis=1) # get central row and average r, g and b
axs[1, i].step(np.arange(len(values)), values, color='dodgerblue')
axs[1, i].axhline(255, color='red', ls='--')
axs[1, i].text(len(values), 255, "\n255 (100% white) ", color='red', va='center', ha='right')
axs[1, i].set_title(f'start pixel: {values[0]:.0f}')
axs[1, i].margins(x=0)
axs[1, 0].set_ylabel('color values')
plt.tight_layout()
plt.show()