我正在尝试使用Python进行直方图匹配,以改善多个重叠栅格的镶嵌过程。我的代码基于以下代码:
http://www.idlcoyote.com/ip_tips/histomatch.html
到目前为止,我已经设法裁剪了两个相邻栅格的重叠区域并展平了阵列。
所以我有两个相同长度的1维数组。
然后,我根据上述网站上的代码编写了以下代码。在所示的代码中,我为gd和bd图像替换了两个非常小的数据集。
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d
bins = range(0,100, 10)
gd_hist = [1,2,3,4,5,4,3,2,1]
bd_hist = [2,4,6,8,10,8,6,4,2]
nPixels = len(gd_hist)
# here we are creating the cumulative distribution frequency for the bad image
cdf_bd = []
for k in range(0, len(bins)-1):
b = sum(bd_hist[:k])
cdf_bd.append(float(b)/nPixels)
# here we are creating the cumulative distribution frequency for the good image
cdf_gd = []
for l in range(0, len(bins)-1):
g = sum(gd_hist[:l])
cdf_gd.append(float(g)/nPixels)
# we plot a histogram of the number of
plt.plot(bins[1:], gd_hist, 'g')
plt.plot(bins[1:], bd_hist, 'r--')
plt.show()
# we plot the cumulative distribution frequencies of both images
plt.plot(bins[1:], cdf_gd, 'g')
plt.plot(bins[1:], cdf_bd, 'r--')
plt.show()
z = []
# loop through the bins
for m in range(0, len(bins)-1):
p = [cdf_bd.index(b) for b in cdf_bd if b < cdf_gd[m]]
if len(p) == 0:
z.append(0)
else:
# if p is not empty, find the last value in the list p
lastval = p[len(p)-1]
# find the bin value at index 'lastval'
z.append(bins[lastval])
plt.plot(bins[1:], z, 'g')
plt.show()
# look into the 'bounds_error'
fi = interp1d(bins[1:], z, bounds_error=False, kind='cubic')
plt.plot(bins[1:], gd_hist, 'g')
plt.show
plt.plot(bins[1:], fi(bd_hist), 'r--')
plt.show()
我的程序成功绘制了直方图和累积频率分布...并且我以为我有一部分要正确地获得转换函数'z'...但是当我在'bd_hist'上使用分布函数'fi'时尝试将其匹配到gd数据集,一切都变得梨形。
我不是数学家,很可能我忽略了一些显而易见的事情。
cdf_bd = np.cumsum(bd_hist) / float(np.sum(bd_hist))