简体   繁体   English

减去两个直方图

[英]Subtract two histograms

I am trying to find the residual left behind when you subtract pixel distribution of two different images(the images are in a 2D array format). 当您减去两个不同图像的像素分布(图像为2D阵列格式)时,我试图找到残留的残差。

I am trying to do something like the below 我正在尝试做以下事情

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

However, in my above method the problem is that both the images have different maximum and minimum and when you do hist1-hist2 the individual bin value of each element in hist1-hist2 is not the same. 然而,在我上面的方法的问题是,无论是图像有不同的最大值和最小值,当你做hist1-hist2中每个元素的独立的贮藏价值hist1-hist2是不一样的。

I was wondering if there is an alternative elegant way of doing this. 我想知道是否有另一种优雅的方法可以做到这一点。

Thanks. 谢谢。

You can explicitly define bins in np.histogram() call. 您可以在np.histogram()调用中显式定义bins If you set them to the same value for both calls, then your code would work. 如果您为两个调用将它们设置为相同的值,则您的代码将起作用。

If your values are say between 0 and 255, you could do following: 如果您的值介于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

This way you have 100 bins with the same boundaries and the simple difference now makes sense (the code is not tested but you get the idea). 这样,您有100个具有相同边界的垃圾箱,并且现在简单的区别才有意义(代码未经测试,但您知道了)。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM