如何使用opencv python自动调整扫描图像的对比度和亮度

ISH*_*WAL 1 python opencv brightness contrast image-preprocessing

我想在不同的照明条件下自动调整手机拍摄的彩色图像的亮度和对比度。请帮助我,我是 OpenCV 新手。

来源: 输入图像

结果: 结果

我所寻求的更多的是本地化的转变。本质上,我希望阴影尽可能亮,如果可能的话,完全消失,并使图像的较暗像素变得更暗,对比度更高,而亮像素变得更白,但不要达到曝光过度或任何其他情况的程度像那样。

我已经尝试过CLAHE,,,,等等Histogram Equalization,但没有任何效果。Binary ThresholdingAdaptive Thresholding

我最初的想法是,我需要中Highlights和并使较暗的像素更接近平均值,同时保持文本和线条尽可能暗。然后也许可以做一个对比滤镜。但我无法得到结果请帮助我。

fmw*_*w42 9

这是在 Python/OpenCV 中执行此操作的一种方法。

  • 读取输入
  • 增加对比度
  • 将原始图像转换为灰度图像
  • 自适应阈值
  • 使用阈值图像使对比度增强图像上的背景变白
  • 保存结果

输入:

在此输入图像描述

import cv2
import numpy as np

# read image
img = cv2.imread("math_diagram.jpg")

# convert img to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# do adaptive threshold on gray image
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 21, 15)

# make background of input white where thresh is white
result = img.copy()
result[thresh==255] = (255,255,255)

# write results to disk
cv2.imwrite("math_diagram_threshold.jpg", thresh)
cv2.imwrite("math_diagram_processed.jpg", result)

# display it
cv2.imshow("THRESHOLD", thresh)
cv2.imshow("RESULT", result)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)

阈值图像:

在此输入图像描述

结果:

在此输入图像描述