在matplotlib子图中显示实际大小的不同图像 [英] Displaying different images with actual size in matplotlib subplot

查看:639
本文介绍了在matplotlib子图中显示实际大小的不同图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用python和matplotlib研究一些图像处理算法.我想使用子图在图中显示原始图像和输出图像(例如,输出图像旁边的原始图像).输出图像的大小与原始图像的大小不同.我希望子图以实际大小(或均匀缩放)显示图像,以便我可以比较苹果与苹果".我目前使用:

I'm working on some image processing algorithms using python and matplotlib. I'd like to display the original image and the output image in a figure using a subplot (e.g. the original image next to the output image). The output image(s) are of different size than the original image. I'd like to have the subplot display the images in their actual size (or uniformly scaled) so that I can compare "apples to apples". I currently use:

plt.figure()
plt.subplot(2,1,1)
plt.imshow(originalImage)
plt.subplot(2,1,2)
plt.imshow(outputImage)
plt.show()

结果是我得到了子图,但是两个图像都按比例缩放以使其具有相同的大小(尽管输出图像上的轴与输入图像上的轴不同).明确地说:如果输入图像为512x512,输出图像为1024x1024,则两个图像都将显示为相同大小.

The result is that I get the subplot, but both images are scaled so that they are the same size (despite the fact that the axes on the output image are different than the axes of the input image). Just to be explicit: if the input image is 512x512 and the output image is 1024x1024 then both images are displayed as though they are the same size.

是否有一种方法可以强制matplotlib以各自的实际大小显示图像(最佳解决方案,以便matplotlib的动态重新缩放不会影响所显示的图像),或者缩放图像以使其与显示的大小成正比他们的实际尺寸?

Is there a way to force matplotlib to either display the images at their respective actual sizes (preferable solution so that matplotlib's dynamic rescaling doesn't effect the displayed image) or to scale the images such that they are displayed with sizes proportional to their actual sizes?

推荐答案

这是您正在寻找的答案:

This is the answer that you are looking for:

def display_image_in_actual_size(im_path):

    dpi = 80
    im_data = plt.imread(im_path)
    height, width, depth = im_data.shape

    # What size does the figure need to be in inches to fit the image?
    figsize = width / float(dpi), height / float(dpi)

    # Create a figure of the right size with one axes that takes up the full figure
    fig = plt.figure(figsize=figsize)
    ax = fig.add_axes([0, 0, 1, 1])

    # Hide spines, ticks, etc.
    ax.axis('off')

    # Display the image.
    ax.imshow(im_data, cmap='gray')

    plt.show()

display_image_in_actual_size("./your_image.jpg")

改编自此处.

这篇关于在matplotlib子图中显示实际大小的不同图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆