我正在从事一个测量和可视化图像相似性的项目。我数据集中的图像来自书籍中图像的照片,其中一些具有非常高或低的曝光率。例如,下面的图片来自两本不同的书;上图是下图的过度曝光重印,曝光看起来不错:
我想用 Python 规范化每张图像的曝光。我想我可以使用以下天真的方法来做到这一点,它试图将每个像素值居中在 0 到 255 之间:
from scipy.ndimage import imread
import sys
def normalize(img):
'''
Normalize the exposure of an image.
@args:
{numpy.ndarray} img: an array of image pixels with shape:
(height, width)
@returns:
{numpy.ndarray} an image with shape of `img` wherein
all values are normalized such that the min=0 and max=255
'''
_min = img.min()
_max = img.max()
return img - _min * 255 / (_max - _min)
img = imread(sys.argv[1])
normalized = normalize(img)
只有在运行之后我才意识到这种归一化只会帮助最亮值小于 255 或最暗值大于 0 的图像。
是否有一种直接的方法来标准化图像的曝光,例如上面的顶部图像?如果其他人可以就此问题提供任何想法,我将不胜感激。
最佳答案
Histogram equalisation对于这种事情,效果出奇的好。它通常更适合摄影图像,但它甚至对艺术线条也有帮助,只要有一些非黑/白像素。
它也适用于彩色图像:拆分波段,分别均衡每个波段,然后重新组合。
我试过你的示例图片:
使用 libvips :
$ vips hist_equal sample.jpg x.jpg
或从 Python 使用 pyvips :
x = pyvips.Image.new_from_file("sample.jpg")
x = x.hist_equal()
x.write_to_file("x.jpg")
关于Python:标准化图像曝光,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49546179/