如何用 Python 绘制图像的均衡直方图?
How to plot the equalized histogram of an image with Python?
这是我的代码。变量 img 是原始图像。变量eq是均衡后的图像。
from matplotlib.pyplot import imread, imshow, show, subplot, title, get_cmap, hist
from skimage.exposure import equalize_hist
img = imread('images/city.tif')
eq = equalize_hist(img)
subplot(221); imshow(img, cmap=get_cmap('gray')); title('Original')
subplot(222); hist(img.flatten(), 256, range=(0,256)); title('Histogram of origianl')
subplot(223); imshow(eq, cmap=get_cmap('gray')); title('Histogram Equalized')
subplot(224); hist(eq.flatten(), 256, range=(0,256));
show()
现在当我 运行 代码时,我得到了原始的直方图就好了。但是均衡的直方图是不正确的。这是我所有的输出
我做错了什么?!?!
编辑:来自 的内置 matlab 命令适用于特定图像
它似乎正在将图像从 uint8
格式(0 到 255 之间的整数值)转换为 float32
或 float64
格式(0 到 1 之间的浮点值)包括的)。试试 eq = np.asarray(equalize_hist(img) * 255, dtype='uint8')
.
这是我的代码。变量 img 是原始图像。变量eq是均衡后的图像。
from matplotlib.pyplot import imread, imshow, show, subplot, title, get_cmap, hist
from skimage.exposure import equalize_hist
img = imread('images/city.tif')
eq = equalize_hist(img)
subplot(221); imshow(img, cmap=get_cmap('gray')); title('Original')
subplot(222); hist(img.flatten(), 256, range=(0,256)); title('Histogram of origianl')
subplot(223); imshow(eq, cmap=get_cmap('gray')); title('Histogram Equalized')
subplot(224); hist(eq.flatten(), 256, range=(0,256));
show()
现在当我 运行 代码时,我得到了原始的直方图就好了。但是均衡的直方图是不正确的。这是我所有的输出
我做错了什么?!?!
编辑:来自
它似乎正在将图像从 uint8
格式(0 到 255 之间的整数值)转换为 float32
或 float64
格式(0 到 1 之间的浮点值)包括的)。试试 eq = np.asarray(equalize_hist(img) * 255, dtype='uint8')
.