在别人帖子上进行了改进,加入了读取文件夹下的图片并进行批量处理并保存的模块
import cv2
import sys
import os
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt
"""
对比度调整算法:
主要是对RGB空间进行调整。设定合适的RGB阈值,并在此阈值基础上计算出合适的调整系数进行对比度调整。
参考CSDN博客:https://blog.csdn.net/maozefa/article/details/7069001
"""
def ContrastAlgorithm(rgb_img, contrast=0.5, threshold=0.5):
img = rgb_img * 1.0
img_out = img
# 增量等于1,按灰度阈值最多调整成八种颜色:
# 黑、红、绿、蓝、黄(255,255,0)、品红(255,0,255)、青(0,255,255)、白
if contrast == 1:
# newRGB = RGB >= Threshold? 255 : 0
mask_1 = img >= threshold * 255.0
rgb1 = 255.0
rgb2 = 0
img_out = rgb1 * mask_1 + rgb2 * (1 - mask_1)
# 增量大于0小于1
elif contrast >= 0:
alpha = 1 - contrast
alpha = 1 / alpha - 1
img_out[:, :, 0] = img[:, :, 0] + (img[:, :, 0] - thres