Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 无极值直方图均衡化_Python_Histogram_Cdf - Fatal编程技术网

Python 无极值直方图均衡化

Python 无极值直方图均衡化,python,histogram,cdf,Python,Histogram,Cdf,是否可以在没有极值0和255的情况下进行直方图均衡化 具体来说,我有一个图像,其中许多像素为零。超过一半的像素为零。因此,如果我在那里做直方图均衡化,我基本上会将值1移到值240,这与我想做的直方图均衡化正好相反。 那么,是否有一种方法只计算值1和254之间的直方图均衡化 目前,我的代码如下所示: flat = image.flatten() # get image histogram image_histogram, bins = np.histogram(flat, bins=range(

是否可以在没有极值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

感谢

解决这个问题的一种方法是在制作直方图之前过滤掉不需要的值,然后制作一个从非标准化像素到标准化像素的“转换表”

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

免责声明:我完全没有任何图像处理经验,因此我不知道其有效性:)

我现在正在打电话,因此我无法尝试任何事情,但如果数据中存在NAN,会发生什么?它还能用吗?如果是这样的话,您可以将不需要的NR替换为NaN,并进行规范化,然后将它们重新放置。