方法一:

        图像中有N个像素,把所有的像素按灰度值的大小进行统计分布,并且统计所有亮度区间中所对应的像素数量n,和区间内的平均亮度。如果n趋于极大值时,那么把它们的对比度作为“对比白”,让其RGB分量全部设为最满。接着,把其它像素点的RGB值也同样处理,处理后全部大于255的值设为255。

import  cv2
 
img = cv2.imread('./zipai3.jpg')

cv2.imshow('img',img)

rows,cols,channels = img.shape

dst = img.copy()
 
a = 1.2
b = 100
for i in range(rows):
    for j in range(cols):
        for c in range(3):
            color = img[i,j][c]*a+b
            if color>255:
                dst[i,j][c]=255
            elif color<0:
                dst[i,j][c]=0
cv2.imshow('dst',dst)
 
cv2.waitKey(0)
cv2.destroyAllWindows()
import cv2
import numpy as np
 
def enhance_mycode(img):
    imgHeight,imgWidth,imgDeep = img.shape
    dst1 = np.zeros((imgHeight, imgWidth, 3), np.uint8)
 
    for i in range(0, imgHeight):
        for j in range(0, imgWidth):
            (b, g, r) = map(int,img[i,j])
            b += 40
            g += 40
            r += 40
            if b > 255:
                b = 255
            if g > 255:
                g = 255
            if r > 255:
                r = 255
            dst1[i, j] = (b, g, r)
    return dst1
'''def enhance_api(img):
    dst2 = np.uint8(np.clip((1.5 * img ), 0, 255))
    return dst2
''' 
img = cv2.imread('zipai3.jpg',1)
dst1 = enhance_mycode(img)
#dst2 = enhance_mycode(img)
 
cv2.imshow('zipai3', img)
cv2.imshow('dst1', dst1)
#cv2.imshow('dst2', dst2)
cv2.waitKey(0)
cv2.destroyAllWindows()

方法二:

        利用addweighted()加权函数来提高清晰度。

import cv2 as cv
import numpy as np
 
 
#粗略的调节对比度和亮度
def contrast_brightness_image(src1, a, g):
    h, w, ch = src1.shape#获取shape的数值,height和width、通道
 
    #新建全零图片数组src2,将height和width,类型设置为原图片的通道类型(色素全为零,输出为全黑图片)
    src2 = np.zeros([h, w, ch], src1.dtype)
    dst = cv.addWeighted(src1, a, src2, 1-a, g)#addWeighted函数说明如下
    cv.imshow("after", dst)
 
src = cv.imread("./zipai3.jpg")
#cv.namedWindow("before", cv.WINDOW_NORMAL)
cv.imshow("before", src)
contrast_brightness_image(src, 1.2, 10) #第一个1.2为对比度  第二个为亮度数值越大越亮
cv.waitKey(0)
cv.destroyAllWindows()

方法三:

        将RGB图像值归一化到[0,1],然后使用函数cvtColor进行色彩空间的转换,接下来可以根据处理灰度图像对比增强的伽马变换或者线性变换调整饱和度和亮度分量,最后转换到RGB色彩空间。

# !/usr/bin/env python
# -*-encoding: utf-8-*-
# author:LiYanwei
# version:0.1
 
 
import numpy as np
import cv2
 
 
def main():
    # 加载图片 读取彩色图像
    image = cv2.imread('./zipai3.jpg', cv2.IMREAD_COLOR)
    # print(image)
    # cv2.imshow("image", image)
    # 图像归一化,且转换为浮点型
    fImg = image.astype(np.float32)
    fImg = fImg / 255.0
    # 颜色空间转换 BGR转为HLS
    hlsImg = cv2.cvtColor(fImg, cv2.COLOR_BGR2HLS)
    l = 100
    s = 100
    MAX_VALUE = 100
    # 调节饱和度和亮度的窗口
    cv2.namedWindow("l and s", cv2.WINDOW_AUTOSIZE)
    def nothing(*arg):
        pass
    # 滑动块
    cv2.createTrackbar("l", "l and s", l, MAX_VALUE, nothing)
    cv2.createTrackbar("s", "l and s", s, MAX_VALUE, nothing)
    # 调整饱和度和亮度后的效果
    lsImg = np.zeros(image.shape, np.float32)
    # 调整饱和度和亮度
    while True:
        # 复制
        hlsCopy = np.copy(hlsImg)
        # 得到 l 和 s 的值
        l = cv2.getTrackbarPos('l', 'l and s')
        s = cv2.getTrackbarPos('s', 'l and s')
        # 1.调整亮度(线性变换) , 2.将hlsCopy[:, :, 1]和hlsCopy[:, :, 2]中大于1的全部截取
        hlsCopy[:, :, 1] = (1.0 + l / float(MAX_VALUE)) * hlsCopy[:, :, 1]
        hlsCopy[:, :, 1][hlsCopy[:, :, 1] > 1] = 1
        # 饱和度
        hlsCopy[:, :, 2] = (1.0 + s / float(MAX_VALUE)) * hlsCopy[:, :, 2]
        hlsCopy[:, :, 2][hlsCopy[:, :, 2] > 1] = 1
        # HLS2BGR
        lsImg = cv2.cvtColor(hlsCopy, cv2.COLOR_HLS2BGR)
        # 显示调整后的效果
        cv2.imshow("l and s", lsImg)
 
        ch = cv2.waitKey(5)
        # 按 ESC 键退出
        if ch == 27:
            break
        elif ch == ord('s'):
            # 按 s 键保存并退出
            # 保存结果
            lsImg = lsImg * 255
            lsImg = lsImg.astype(np.uint8)
            cv2.imwrite("lsImg.jpg", lsImg)
            break
 
    # 关闭所有的窗口
    cv2.destroyAllWindows()
 
 
if __name__ == "__main__":
    main()