【问题标题】:Subtract two histograms减去两个直方图
【发布时间】:2019-09-18 11:44:09
【问题描述】:

当你减去两个不同图像的像素分布时,我试图找到留下的残差(图像是二维数组格式)。

我正在尝试做类似下面的事情

import numpy as np
hist1, bins1 = np.histogram(img1, bins=100)
hist2, bins2 = np.histogram(img2, bins=100)
residual = hist1 - hist2

但是,在我上面的方法中,问题是两个图像都有不同的最大值和最小值,当你这样做hist1-hist2 时,hist1-hist2 中每个元素的单独 bin 值不一样。

我想知道是否有另一种优雅的方式来做到这一点。

谢谢。

【问题讨论】:

    标签: python numpy histogram


    【解决方案1】:
    import numpy as np
    nbins = 100
    #minimum value element wise from both arrays
    min = np.minimum(img1, img2)
    #maximum value element wise from both arrays
    max = np.maximum(img1, img2)
    #histogram is build with fixed min and max values
    hist1, _ = numpy.histogram(img1,range=(min,max), bins=nbins)
    hist2, _ = numpy.histogram(img2,range=(min,max), bins=nbins)
    
    #makes sense to have only positive values 
    diff = np.absolute(hist1 - hist2)
    

    【讨论】:

    • 这是一个不错的解决方案。我只想指出 minmax 是保留关键字。
    【解决方案2】:

    您可以在np.histogram() 调用中显式定义bins。如果您将它们设置为两个调用的相同值,那么您的代码就可以工作。

    如果您的值介于 0 到 255 之间,您可以执行以下操作:

    import numpy as np
    hist1, bins1 = np.histogram(img1, bins=np.linspace(0, 255, 100))
    hist2, bins2 = np.histogram(img2, bins=np.linspace(0, 255, 100))
    residual = hist1 - hist2
    

    这样你就有了 100 个边界相同的 bin,现在简单的区别是有意义的(代码没有经过测试,但你明白了)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-07
      • 2012-06-07
      • 2015-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多