首页 > 解决方案 > 改变图像的范围并用该范围而不是像素匹配图

问题描述

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

我认为该问题的解决方案是添加我的图像的比例因子(1.93 um/像素),但我不确定在哪里包含这个因子,这样也不会缩放像素的强度。

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()

标签: pythonimagematplotlibscaleaxis

解决方案


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

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

推荐阅读