Python:规范图像曝光 [英] Python: Normalize image exposure

查看:428
本文介绍了Python:规范图像曝光的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个项目,以测量和可视化图像相似度.我的数据集中的图像来自书籍中的图像照片,其中一些具有很高或很低的曝光率.例如,下面的图像来自两本书.顶部的一个是底部的一个的过度曝光重印,其中曝光效果不错:

I'm working on a project to measure and visualize image similarity. The images in my dataset come from photographs of images in books, some of which have very high or low exposure rates. For example, the images below come from two different books; the one on the top is an over-exposed reprint of the one on the bottom, wherein the exposure looks good:

我想用Python规范每个图像的曝光.我以为可以使用以下幼稚的方法来做到这一点,该方法尝试将每个像素值居中在0到255之间:

I'd like to normalize each image's exposure in Python. I thought I could do so with the following naive approach, which attempts to center each pixel value between 0 and 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的图像.

Only after running this did I realize that this normalization will only help images whose lightest value is less than 255 or whose darkest value is greater than 0.

是否有一种简单的方法来标准化图像(如上面的顶部图像)的曝光?对于其他人在这个问题上可以提出的任何想法,我将不胜感激.

Is there a straightforward way to normalize the exposure of an image such as the top image above? I'd be grateful for any thoughts others can offer on this question.

推荐答案

直方图均衡有效这种事情令人惊讶地好.通常对于摄影图像更好,但只要有一些非黑色/白色像素,即使在艺术线条上也很有用.

Histogram equalisation works surprisingly well for this kind of thing. It's usually better for photographic images, but it's helpful even on line art, as long as there are some non-black/white pixels.

它也适用于彩色图像:将频段分开,分别均衡每个频段,然后重新组合.

It works well for colour images too: split the bands up, equalize each one separately, and recombine.

我尝试过您的示例图像:

I tried on your sample image:

使用 libvips :

$ vips hist_equal sample.jpg x.jpg

或者通过Python使用 pyvips :

Or from Python with pyvips:

x = pyvips.Image.new_from_file("sample.jpg")
x = x.hist_equal()
x.write_to_file("x.jpg")

这篇关于Python:规范图像曝光的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆