改变图像的范围并用该范围而不是像素匹配绘图

Changing extent of image and matching plot with that extent rather than pixels

我在 matplotlib 中创建了一个带有两个子图的图形,第一个子图显示了一个图像,上面绘制了一条线,第二个子图显示了该线上的像素值图。我在图像上添加了一个范围,以便轴显示图像的比例而不是像素距离,我希望第二个子图的 x 轴显示相同的比例距离,但我无法计算了解如何更改该轴,因为我正在从表示图像的数组中提取数据。

我认为解决问题的方法是添加图像的比例因子 (1.93 um/pixel),但我不确定在何处包含该因子,因此也无法缩放像素强度。

umPerPx = 1.93
linexPx = 225
liney1Px, liney2Px = 50, 100
linexUm = 225 * umPerPx
liney1Um, liney2Um = 50 * umPerPx, 100 * umPerPx

img = cv2.imread(test.tif, -1)
img_h_px, img_w_px = img.shape
img_h_um, img_w_um = img_h_px * umPerPx, img_w_px * umPerPx
fig = plt.figure(figsize=(12, 4))

ax_img = fig.add_subplot(1, 2, 1)
ax_img.set_title(imgDirList[i][:-4])
ax_img.imshow(img, cmap='gray', extent=[0, img_w_um, img_h_um, 0])
ax_img.plot([linexUm, linexUm], [liney1Um, liney2Um], 'r', linewidth=1)
ax_img.set_ylabel('Distance (microns)')

ax_prof = fig.add_subplot(1, 2, 2)
ax_prof.set_title('Intensity Profile')
ax_prof.plot(img[:, linexPx], 'r')
ax_prof.set_ylim(min(img[liney1Px:liney2Px, linexPx]), max(img[liney1Px:liney2Px, linexPx]))
ax_prof.set_xlim([liney1Px, liney2Px])
ax_prof.set_xlabel('Distance (pixels)')
ax_prof.set_ylabel('Intensity')

plt.show()

如果没有实际数据来尝试,很难确定,但在我看来,您需要缩放的是第二个情节的 x-value。就目前而言,您没有提供 x 数组,因此它是根据您提供的 y 数组自动计算的。但这样的事情可能会更好:

ax_prof.plot(np.arange(len(img[:, linexPx]))*umPerPx, img[:, linexPx], 'r')