【问题标题】:cv2.imwrite and pyplot.imshow displaying grey scale images differentlycv2.imwrite 和 pyplot.imshow 以不同方式显示灰度图像
【发布时间】:2020-03-29 04:38:44
【问题描述】:

我在hdf5 文件中有一堆医学图像。

 path_to_data = "data/train_large_axial_350_x_350.hdf5"
 data = h5py.File(path_to_data,"r")
 image_list = data["images"]
 image = image_list[5]

如果我这样做了

 cv2.imwrite("maps/image.jpg", image)

我明白了:

但是,如果我这样做了

plt.imshow(image)

我明白了:

这是什么原因?如何绘制图像以使其看起来像 cv2.imwrite 生成的图像?

【问题讨论】:

  • image是什么类型?
  • 这能回答你的问题吗? why cv2.imwrite() changes the color of pics?
  • 色序 BGR、RGB
  • @abhilb 这是一张灰度图——即使是 3 通道,所有通道都是相同的,所以在这种情况下顺序无关紧要。
  • 也许你有一个默认的 matplotlib 颜色图处于活动状态。 plt.imshow(img, cmap='gray') 的结果是什么?

标签: python opencv matplotlib plot


【解决方案1】:

这是因为matplotlib 将图像绘制在图像的灰度值范围内。例如,如果您的图像包含介于 21 到 88 之间的灰度值,则 matplotlib 会使用此范围绘制它,而不是介于 0 到 255 之间。

这可以通过以下方式解决:

# from matplotlib import cm    
plt.imshow(image, cmap=cm.gray, vmin=0, vmax=255)

如需更深入的讨论,请查看this github 问题。

【讨论】:

    猜你喜欢
    • 2021-11-07
    • 1970-01-01
    • 2020-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    相关资源
    最近更新 更多