python - 改变图像的范围并用该范围而不是像素匹配图
问题描述
我在 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()
解决方案
如果没有实际数据来尝试,很难确定,但在我看来,您需要缩放的是第二个图的 x 值。就目前而言,您没有提供 x 数组,因此它是根据您提供的 y 数组自动计算的。但这样的事情可能会更好:
ax_prof.plot(np.arange(len(img[:, linexPx]))*umPerPx, img[:, linexPx], 'r')
推荐阅读
- reactjs - Firebase 存储,JSPDF 不保留图像
- node.js - 为什么将电子邮件验证令牌/密码存储在数据库而不是数组中?NodeJs、Express、Typescript
- python - Ansible 模板模块不适用于 jinja 块分配中的循环
- html - 弹性项目的边距会推动另一个弹性项目
- reactjs - 如何在 slate js 中为特定关键字着色
- javascript - 嘿伙计们,谁能给我解释一下这段代码?
- python - 连接不同类型的列表
- python - 在 Tensorflow 2.5 中训练 GAN 时 GPU 利用率不稳定
- node.js - 找不到模块“bson”的声明文件
- gradle - 在 gradle 多模块项目中将插件/runtimeOnly 配置从一个模块注入另一个模块(在同一项目中)