是否可以在没有极限值0和255的情况下进行直方图均衡化?
具体来说,我有一个图像,其中许多像素为零。所有像素的一半以上为零。因此,如果我进行直方图均衡化,则基本上将值1移至值240,这与我要对直方图均衡化所做的操作恰好相反。 那么,有没有一种方法只能计算值1和254之间的直方图均衡?
此刻,我的代码如下:
flat = image.flatten()
# get image histogram
image_histogram, bins = np.histogram(flat, bins=range(0, number_bins), density=True)
cdf = image_histogram.cumsum() # cumulative distribution function
cdf = 255 * cdf /cdf.max() # normalize
cdf = cdf.astype('uint8')
# use linear interpolation of cdf to find new pixel values
image_equalized = np.interp(flat, bins[:-1], cdf)
image_equalized = image_equalized.reshape(image.shape), cdf
谢谢
答案 0 :(得分:1)
一种解决方法是在制作直方图之前过滤掉不需要的值,然后从非归一化像素制作“转换表” 归一化的像素。
import numpy as np
# generate random image
image = np.random.randint(0, 256, (32, 32))
# flatten image
flat = image.flatten()
# get image histogram
image_histogram, bins = np.histogram(flat[np.where((flat != 0) & (flat != 255))[0]],
bins=range(0, 10),
density=True)
cdf = image_histogram.cumsum() # cumulative distribution function
cdf = 255 * cdf /cdf.max() # normalize
cdf = cdf.astype('uint8')
# use linear interpolation of cdf to find new pixel values
# we make a list conversion_table, where the index is the original pixel value,
# and the value is the histogram normalized pixel value
conversion_table = np.interp([i for i in range(0, 256)], bins[:-1], cdf)
# replace unwanted values by original
conversion_table[0] = 0
conversion_table[-1] = 255
image_equalized = np.array([conversion_table[pixel] for pixel in flat])
image_equalized = image_equalized.reshape(image.shape), cdf
免责声明:我完全没有图像处理方面的经验,因此我不知道其有效性:)