我正在尝试为灰度颜色的图像编写对比度调整,但到目前为止找不到正确的方法。这就是我想出的:
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from scipy import misc
def fix_contrast(image):
minimumColor = np.amin(image)
maximumColor = np.amax(image)
#avg = (minimumColor - maximumColor)/2 first attempt
avg = np.mean(image) #second attempt
colorDownMatrix = image < avg # also tried
colorUpMatrix = image > avg
#also tried: colorUpMatrix = image > avg * 1.2
# and : colorDownMatrix = image < avg* 0.3
image = image - minimumColor*colorDownMatrix
image = image + maximumColor*colorUpMatrix
lessThen0 = image<0
moreThen255 = image>255
image[lessThen0] = 0
image[moreThen255] = 255
return image
我的一般尝试是将元素减少到 0,即它们“更接近”到 0 的像素,并将元素增加到 255,即“更接近”到 255 的元素。我尝试通过均值函数测量接近度,但在此之前通过算术平均值,但我所有的尝试都没有让我得到任何好的结果。
我离解决方案很近吗?任何提示/技巧都会很棒
原文由 barshopen 发布,翻译遵循 CC BY-SA 4.0 许可协议
我正在学习 Python 和
numpy
并认为我会尝试实现 “查找表” (LUT)。它工作正常,输出图像具有从黑色到白色的完整范围,但我很高兴收到改进建议。关键词:Python、Numpy、PIL、Pillow、图像、图像处理、LUT、查找表、查找、对比度、拉伸。