白平衡

本文介绍了一种基于平均亮度的图像白平衡校正方法。该方法通过计算图像的RGB通道平均值,并调整各通道的增益系数来实现颜色校正,使图像看起来更加自然。代码实现了图像读取、尺寸调整、白平衡校正及显示等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

# -*- coding: utf-8 -*-
"""
Created on Fri Jun 22 19:00:54 2018

@author: PC
"""
import cv2
import numpy as np

def whiteBalance(src):
    B,G,R = cv2.split(src)
    B_mean = np.mean(B)
    G_mean = np.mean(G)
    R_mean = np.mean(R)
    gray_mean = (B_mean+G_mean+R_mean) / 3
    B_corf = 0.9  * gray_mean / B_mean
    G_corf = 0.9 * gray_mean / G_mean
    R_corf = 0.9 * gray_mean / R_mean
    
    B_Correct = np.uint8(B_corf * B)
    G_Correct = np.uint8(G_corf * G)
    R_Correct = np.uint8(R_corf * R)
    
    dst = cv2.merge((B_Correct,G_Correct,R_Correct))
    
    return dst

def main():
    img = cv2.imread('C:\\Users\\PC\\Desktop\\program\\skin\\Images\\12345\\4.jpg')
    width, height = img.shape[:2]
    if height>800 :
        rate = height/800
        width1 = int(width/rate)
        height1 = 800
    img = cv2.resize(img,(height1,width1),cv2.INTER_CUBIC)
#    dst = LightNormalization.lightNormalization(img)
    dst = whiteBalance(img)
    cv2.imshow("img", img)
    cv2.imshow("whiteBalance", dst)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    
if __name__ == "__main__":
    main()

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值