【问题标题】:Matplotlib difference between two imagesMatplotlib 两张图片的区别
【发布时间】:2013-10-04 20:46:05
【问题描述】:

我的图像(4000x2000 像素)源自同一张图像,但只有不到 1% 的像素存在细微差异。我想并排绘制两个图像并 highlight 阵列的不同区域(通过 highlight 我的意思是我想要不同的像素跳出来,但仍然显示与其值匹配的颜色。到目前为止,我一直在使用未填充的矩形来勾勒这些像素的边缘。我可以在小图像中很好地做到这一点(~ 50x50) 与:

    fig=figure(figsize=(20,15))
    ax1=fig.add_subplot(1,2,1)
    imshow(image1,interpolation='nearest',origin='lower left')
    colorbar()
    ax2=fig.add_subplot(122,sharex=ax1, sharey=ax1)
    imshow(image2,interpolation='nearest',origin='lower left')
    colorbar()
    #now show differences
    Xspots=im1!=im2
    Xx,Xy=nonzero(Xspots)
    for x,y in zip(Xx,Xy):
           rect=Rectangle((y-.5,x-.5),1,1,color='w',fill=False,ec='w')
           ax1.add_patch(rect)
           ax2.add_patch(rect)

但是,当图像非常大时,这不会很好。奇怪的事情发生了,例如当我放大补丁时消失了。此外,这种方式很糟糕,因为当我放大/缩小时加载东西需要很长时间。

我觉得必须有更好的方法来做到这一点,也许只有一个补丁可以确定所有东西的位置,而不是一大堆补丁。我可以在 imshow 图像上做一个散点图,但我不知道如何修复它,以便在我放大/缩小时点将完全保持像素的大小。

有什么想法吗?

【问题讨论】:

    标签: matplotlib plot patch figure


    【解决方案1】:

    我会尝试使用 alpha 通道:

    import copy
    N, M = 20, 40
    test_data = np.random.rand(N, M)
    mark_mask = np.random.rand(N, M) < .01 # mask 1%
    # this is redundant in this case, but in general you need it
    my_norm = matplotlib.colors.Normalize(vmin=0, vmax=1)
    # grab a copy of the color map
    my_cmap = copy.copy(cm.get_cmap('cubehelix'))
    c_data= my_cmap(my_norm(test_data))
    c_data[:, :, 3] = .5 # make everything half alpha
    c_data[mark_mask, 3] = 1 # reset the marked pixels as full opacity
    # plot it
    figure()
    imshow(c_data, interpolation='none')
    

    不知道这是否适用于您的数据。

    【讨论】:

    • 这同样适用于小图像(尺寸约为 20x40),但不适用于较大的图像(尺寸大于 ~1000x1000)。它给我留下了一个灰色的背景,即使我从较大的图像放大到小尺寸的图像,背景仍然看起来是灰色的,而不是它应该看起来的 0.5 alpha 版本。还有其他想法吗?
    • 放大后效果很好。您使用的是什么颜色图?
    • 实际上现在我正在使用更快的处理器/更好的显卡的计算机上尝试它,它实际上看起来相当不错。谢谢!
    猜你喜欢
    • 2011-03-24
    • 2022-08-14
    • 1970-01-01
    • 2011-10-31
    • 2019-12-14
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多