python - 如何在 Python OpenCV 中增加图像的对比度

标签 python image opencv image-processing computer-vision

我是 Python OpenCV 的新手。我看了一些文档和答案here但我无法弄清楚以下代码的含义:

if (self.array_alpha is None):
    self.array_alpha = np.array([1.25])
    self.array_beta = np.array([-100.0])

# add a beta value to every pixel 
cv2.add(new_img, self.array_beta, new_img)                    

# multiply every pixel value by alpha
cv2.multiply(new_img, self.array_alpha, new_img)  

我了解到,基本上,每个像素都可以转换为 X = aY + b,其中 a 和 b 是标量。。基本上,我已经理解了这一点。但是,我不理解代码以及如何增加对比度。

到目前为止,我已经设法使用 img = cv2.imread('image.jpg',0) 简单地读取图像

谢谢你的帮助

最佳答案

我想推荐一种使用 LAB color space 的方法.

LAB 颜色空间表示三个 channel 的颜色变化。一个亮度 channel 和两个颜色 channel :

  • L channel :表示图像中的亮度
  • a-channel:代表红绿之间的颜色变化
  • b channel :代表黄色和蓝色之间的颜色变化

在下文中,我在 L channel 上执行自适应直方图均衡化并将生成的图像转换回 BGR 颜色空间。这增强了亮度,同时也限制了对比敏感度。我使用 OpenCV 3.0.0 和 python 完成了以下操作:

代码:

import cv2
import numpy as np

img = cv2.imread('flower.jpg', 1)
# converting to LAB color space
lab= cv2.cvtColor(img, cv2.COLOR_BGR2LAB)
l_channel, a, b = cv2.split(lab)

# Applying CLAHE to L-channel
# feel free to try different values for the limit and grid size:
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
cl = clahe.apply(l_channel)

# merge the CLAHE enhanced L-channel with the a and b channel
limg = cv2.merge((cl,a,b))

# Converting image from LAB Color model to BGR color spcae
enhanced_img = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)

# Stacking the original image with the enhanced image
result = np.hstack((img, enhanced_img))
cv2.imshow('Result', result)

结果:

增强后的图片在右边

enter image description here

您可以按原样运行代码。 要了解 CLAHE(对比度受限自适应直方图均衡)是什么,refer this Wikipedia page

关于python - 如何在 Python OpenCV 中增加图像的对比度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39308030/

相关文章:

android - Android 上的 JavaCV 示例(FacePreview)无法正常工作

在 Ubuntu 上导入 PyQt4.QtDeclarative 或 PyQt4.Qt 时 Python 2.7 崩溃

php - 即时创建拇指与上传后创建拇指

带有图像和标签的 Python GTK3 按钮

java - 在 Java 中显示来自数据库的多个图像

visual-studio-2008 - OpenCV 库文件夹在哪里?

python - 使用 Pandas 数据帧时如何避免慢速 for() : loops,?

python - 防止类的函数属性将 self 作为第一个参数传递

python - 读取文本文件时出现语法错误

python - 捕获感兴趣的区域