【问题标题】:Adaptive Histogram Equalization in PythonPython中的自适应直方图均衡
【发布时间】: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


【解决方案1】:

我查看了实际算法并提出了以下实现。我确信有更好的方法来做到这一点。任何建议表示赞赏。

import numpy as np
import cv2

img = cv2.imread('watch.png',0)
print img
img_size=img.shape
print img_size

img_mod = np.zeros((600, 800))

for i in range(0,img_size[0]-30):
    for j in range(0,img_size[1]-30):
        kernel = img[i:i+30,j:j+30]
        for k in range(0,30):
            for l in range(0,30):
                element = kernel[k,l]
                rank = 0
                for m in range(0,30):
                    for n in range(0,30):
                        if(kernel[k,l]>kernel[m,n]):
                            rank = rank + 1
                img_mod[i,j] = ((rank * 255 )/900)

im = np.array(img_mod, dtype = np.uint8)
cv2.imwrite('target.png',im)

【讨论】:

    猜你喜欢
    • 2013-01-25
    • 2021-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 1970-01-01
    • 2017-12-03
    相关资源
    最近更新 更多