2015-03-02 223 views
3

我正在使用python和matplotlib处理一些图像处理算法。我想使用子图(例如,输出图像旁边的原始图像)在图中显示原始图像和输出图像。输出图像的大小与原始图像的大小不同。我希望副图显示图像的实际尺寸(或统一缩放),以便我可以比较“苹果与苹果”。我目前使用:在matplotlib子图中显示具有实际尺寸的不同图像

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

结果是我得到的插曲,但两个图像(缩放,使得它们具有相同的尺寸尽管在输出图像上的轴比的轴不同输入图像)。只是要明确:如果输入图像是512x512,输出图像是1024x1024,那么两幅图像都显示为相同大小。

有没有办法迫使matplotlib以各自的实际尺寸显示图像(最好的解决方案,以便matplotlib的动态重新缩放不会影响显示的图像),或者缩放图像以使它们以大小成比例显示到他们的实际大小?

+1

我认为'figimage'可能对你有用......这个问题可能是[this](http://stackoverflow.com/questions/25960755/how-to-set-imshow-scale)问题的重复。 .. – Ajean 2015-03-02 20:10:13

+0

谢谢。我会看看。是的,看起来像一个副本帖子。猜猜我在搜索时没有看到那个。谢谢! – Doov 2015-03-03 18:08:46

回答

4

这是你正在寻找的答案:从here改编

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