【发布时间】:2016-05-14 14:31:23
【问题描述】:
好的,伙计们,我一直在尝试为两个图像创建直方图匹配;模板图像和目标图像(目标意味着我想要匹配模板图像的图像)。我没有显示匹配的图像,而是得到一个空白图像。我觉得我对此感到筋疲力尽,因此我来到SO。有人可以指导我正确的方向吗?
任何正确方向的帮助将不胜感激。
def matching(template, target, numberOfBins=256):
templateHist, bins1 = np.histogram(template.flatten(), numberOfBins, density = False)
targetHist, bins2 = np.histogram(target.flatten(), numberOfBins, density = False)
cdfTemplate = templateHist.cumsum() #Cumulative distributed function
cdfTemplate = (255 * cdfTemplate / cdfTemplate[-1]) #normalize
cdfTarget = targetHist.cumsum()
cdfTarget = (255 * cdfTarget / cdfTarget[-1]).astype(np.float64)
im2 = np.interp(template.flatten(), cdfTemplate, bins1[:-1])
im3 = np.interp(im2, cdfTarget, bins2[:-1])
result = im3.reshape((template.shape))
return result
【问题讨论】:
-
表示 cdfTemplate[-1] 返回 0。
-
错误信息告诉你
cdfTemplate[-1]等于0,这导致cdfTemplate中的所有元素都变成了NaN。这意味着templateHist的总和也必须为 0。很难解释为什么会这样,因为它由直方图 bin 计数组成,并且直方图 bin 边缘的自动选择应该确保至少一个 bin包含非零计数。template和target是什么?我猜它们是屏蔽数组? -
我能想到的唯一能让你得到全零 bin 计数的方法是,如果你将一个空数组传递给
np.histogram -
它将一个空数组传入 np.histogram。我读到的每一篇关于直方图匹配的文章都给了我类似的功能和设计。