美文网首页
明暗检测与亮度调节算法

明暗检测与亮度调节算法

作者: Byte猫 | 来源:发表于2019-05-13 11:27 被阅读0次

    一、明暗检测

    图像亮度通俗理解便是图像的明暗程度,它是图片质量的评价参数之一。计算整张图片的亮度有多种方法。

    # coding=utf-8
    import cv2 as cv
    from PIL import Image, ImageStat
    
    def image_brightness1(rgb_image):
        '''
        检测图像亮度(亮度平均值方法)
        '''
        w, h = rgb_image.size
        # 转换为HSV格式
        hsv_image = cv.cvtColor(np.array(rgb_image, 'f'), cv.COLOR_RGB2HSV)
        # 累计总亮度值
        sum_brightness = np.sum(hsv_image[:,:,2])
        area = w*h  # 总像素数
        # 平均亮度
        avg = sum_brightness/area
        return avg
    
    def image_brightness2(rgb_image):
        '''
        检测图像亮度(灰度平均值方法)
        '''
        gray_image = rgb_image.convert('L')
        stat = ImageStat.Stat(gray_image)   
        return stat.mean[0]
    
    def image_brightness3(rgb_image):
        '''
        检测图像亮度(基于经验公式)
        '''
        stat = ImageStat.Stat(rgb_image)   
        r, g, b = stat.mean   
        return math.sqrt(0.241*(r**2) + 0.691*(g**2) + 0.068*(b**2))
    
    def image_brightness4(rgb_image):
        '''
        检测图像亮度(基于RMS)
        '''
        stat = ImageStat.Stat(rgb_image)   
        r, g, b = stat.rms   
        return math.sqrt(0.241*(r**2) + 0.691*(g**2) + 0.068*(b**2))
    
    pil_im = Image.open('test.jpg')
    print(image_brightness1(pil_im))
    print(image_brightness2(pil_im))
    print(image_brightness3(pil_im))
    print(image_brightness4(pil_im))
    

    二、明暗调节

    有了图像亮度后如何进行调整呢?我在这里采用了空域法中的gamma变换,通过如下方法计算gamma值

    def calc_gamma(brightness):
        '''
        计算gamma值
        '''
        return brightness/127.0
    

    然后用得到的gamma值进行亮度变换

    import numpy as np
    from skimage import exposure
    import matplotlib.pyplot as plt
    
    def array_to_image(image_arr):
        '''
        数组还原为图片
        INPUT  -> 数组
        OUTPUT -> 单张图文件
        '''
        if len(image_arr.shape) == 3:  # 格式为(height(rows), weight(colums), 3)
            r = Image.fromarray(np.uint8(image_arr[:,:,0]))
            g = Image.fromarray(np.uint8(image_arr[:,:,1]))
            b = Image.fromarray(np.uint8(image_arr[:,:,2]))
            image = Image.merge("RGB", (r, g, b))
            return image        
        elif len(image_arr.shape) == 2:  # 格式为(height(rows), weight(colums))
            return Image.fromarray(np.uint8(image_arr))
    
    def image_gamma_transform(pil_im, gamma):
        '''
        伽马校正
        INPUT  -> 单张图文件
        OUTPUT -> 处理后的图文件
        '''
        image_arr = np.array(pil_im)
        image_arr2 = exposure.adjust_gamma(image_arr, gamma)
        return array_to_image(image_arr2)
    
    pil_im = Image.open('test.jpg')
    brightness = image_brightness4(pil_im)
    newimage = image_gamma_transform(pil_im, calc_gamma(brightness))
    
    result = np.hstack((np.array(pil_im),np.array(newimage)))
    plt.imshow(result)
    plt.show()
    

    相关文章

      网友评论

          本文标题:明暗检测与亮度调节算法

          本文链接:https://www.haomeiwen.com/subject/mpeboqtx.html