gpt4 book ai didi

python - 局部直方图均衡化

转载 作者:太空狗 更新时间:2023-10-30 01:15:36 27 4
gpt4 key购买 nike

我正在尝试使用 python 进行一些图像分析(我必须使用 python)。我需要同时进行全局和局部直方图均衡。全局版本运行良好,但本地版本使用 7x7 封装,结果非常差。

这是全局版本:

   import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from scipy import ndimage,misc
import scipy.io as io
from scipy.misc import toimage
import numpy as n
import pylab as py
from numpy import *

mat = io.loadmat('image.mat')
image=mat['imageD']

def histeq(im,nbr_bins=256):
#get image histogram
imhist,bins = histogram(im.flatten(),nbr_bins,normed=True)
cdf = imhist.cumsum() #cumulative distribution function
cdf = 0.6 * cdf / cdf[-1] #normalize
#use linear interpolation of cdf to find new pixel values
im2 = interp(im.flatten(),bins[:-1],cdf)
#returns image and cumulative histogram used to map
return im2.reshape(im.shape), cdf

im=image
im2,cdf = histeq(im)

为了制作本地版本,我正在尝试使用像这样的通用过滤器(使用与之前加载的相同的图像):

   def func(x):
cdf=[]
xhist,bins=histogram(x,256,normed=True)
cdf = xhist.cumsum()
cdf = 0.6 * cdf / cdf[-1]
im_out = interp(x,bins[:-1],cdf)
midval=interp(x[24],bins[:-1],cdf)
return midval

print im.shape
im3=ndimage.filters.generic_filter(im, func,size=im.shape,footprint=n.ones((7,7)))

对于为什么第二个版本不起作用,有人有任何建议/想法吗?我真的被卡住了,任何意见将不胜感激!提前致谢!

最佳答案

您可以使用 scikit-image执行全局和局部直方图均衡的库。从链接中自豪地窃取,下面是片段。均衡是用圆盘形内核(或足迹)完成的,但您可以通过设置 kernel = np.ones((N,M)) 将其更改为正方形。

import numpy as np
import matplotlib
import matplotlib.pyplot as plt

from skimage import data
from skimage.util import img_as_ubyte
from skimage import exposure
import skimage.morphology as morp
from skimage.filters import rank

# Original image
img = img_as_ubyte(data.moon())

# Global equalize
img_global = exposure.equalize_hist(img)

# Local Equalization, disk shape kernel
# Better contrast with disk kernel but could be different
kernel = morp.disk(30)
img_local = rank.equalize(img, selem=kernel)

fig, (ax_img, ax_global, ax_local) = plt.subplots(1, 3)

ax_img.imshow(img, cmap=plt.cm.gray)
ax_img.set_title('Low contrast image')
ax_img.set_axis_off()

ax_global.imshow(img_global, cmap=plt.cm.gray)
ax_global.set_title('Global equalization')
ax_global.set_axis_off()

ax_local.imshow(img_local, cmap=plt.cm.gray)
ax_local.set_title('Local equalization')
ax_local.set_axis_off()

plt.show()

Results of equalization

关于python - 局部直方图均衡化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20086032/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com