【发布时间】:2017-09-20 00:59:54
【问题描述】:
我正在尝试在 python 中实现自适应直方图均衡。我拍摄一张图像并将其分割成更小的区域,然后对其应用传统的直方图均衡化。然后我将较小的图像组合成一个并获得最终的结果图像。最终图像在本质上看起来非常块状,并且每个单独区域具有不同的对比度级别。有没有一种方法可以为每个单独的图像保持统一的对比度,使其看起来像单个图像,而不是拼接在一起的较小图像。
import cv2
import numpy as np
from matplotlib import pyplot as plt
from scipy.misc import imsave
from scipy import ndimage
from scipy import misc
import scipy.misc
import scipy
import image_slicer
from image_slicer import join
from PIL import Image
img = 'watch.png'
num_tiles = 25
tiles = image_slicer.slice(img, num_tiles)
for tile in tiles:
img = scipy.misc.imread(tile.filename)
hist,bins = np.histogram(img.flatten(),256,[0,256])
cdf = hist.cumsum()
cdf_normalized = cdf *hist.max()/ cdf.max()
plt.plot(cdf_normalized, color = 'g')
plt.hist(img.flatten(),256,[0,256], color = 'g')
plt.xlim([0,256])
plt.legend(('cdf','histogram'), loc = 'upper left')
cdf_m = np.ma.masked_equal(cdf,0)
cdf_o = (cdf_m - cdf_m.min())*255/(cdf_m.max()-cdf_m.min())
cdf = np.ma.filled(cdf_o,0).astype('uint8')
img3 = cdf[img]
cv2.imwrite(tile.filename,img3)
tile.image = Image.open(tile.filename
image = join(tiles)
image.save('watch-join.png')
【问题讨论】:
-
您确定这种算法方法是您想要的吗?您看到的结果是预期的。当然,平滑度并不高。由于我不熟悉自适应 hist-eq,因此我查阅了维基百科,那里的算法非常不同(基于滑动窗口;没有像您的情况那样的非重叠块)并且显然会产生更平滑的结果。您也可以查看skimage's approach/implementation。
-
考虑到您解决问题的方法,输出是预期的......由于它们的直方图不同,您对每个块的处理方式不同。您是否阅读过有关该问题的任何论文或任何开源代码?没有理由重新发明轮子……
-
@sascha 我发布了一个不同的代码来实现 AHE。您是否可以对其进行审查并提出任何更改以进一步改进它。
标签: python numpy image-processing histogram