Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 减去两个直方图_Python_Numpy_Histogram - Fatal编程技术网

Python 减去两个直方图

Python 减去两个直方图,python,numpy,histogram,Python,Numpy,Histogram,我试图找到减去两个不同图像的像素分布后留下的残差(图像是2D数组格式) 我正在尝试做下面的事情 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中每个元素的单个bi

我试图找到减去两个不同图像的像素分布后留下的残差(图像是2D数组格式)

我正在尝试做下面的事情

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值不相同

我想知道是否有另一种优雅的方式来做这件事


谢谢。

您可以在
np.histogram()调用中明确定义
bin
。如果您为两个调用都将它们设置为相同的值,那么您的代码将正常工作

如果您的值介于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个具有相同边界的存储箱,而简单的差异现在是有意义的(代码没有经过测试,但您已经知道了它的意思)。

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

如果您的值介于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个具有相同边界的垃圾箱,而简单的差异现在就有了意义(代码没有经过测试,但您已经了解了它的想法)。

这是一个很好的解决方案。我只想指出
min
max
是保留关键字。这是一个很好的解决方案。我只想指出,
min
max
是保留关键字。