用OpenCV图像处理技巧之白平衡算法(一)

1. 引言

欢迎继续来到我们的图像处理系列,在这里我们将探讨白平衡的关键技术。如果大家曾经拍过一张看起来暗淡、褪色或颜色不自然的照片,那么此时大家就需要了解到白平衡技术的重要性。在本文中,我们将深入探讨白平衡的概念,并探索各种算法来提高图像的成像质量。
闲话少说,我们直接开始吧!

2. 定义

白平衡是一种用于校正由不同照明条件引起的图像中的颜色校正的技术。这是一个调整图像颜色对比度的过程,使白色看起来像白色,使黑色看起来像黑色。这对于确保图像中的颜色是准确的并且对人眼来说是自然的是非常重要的。

3. 加载样例图像

好的,按照惯例我们还是首先导入我们需要的python库,如下:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from skimage import io, img_as_ubyte
from skimage.io import imread, imshow
from matplotlib.patches import Rectangle

接着使用以下代码加载我们的样例图像,代码如下:

from skimage import io
import matplotlib.pyplot as plt

image = io.imread('qmark.png')
plt.figure(figsize=(10,10))
plt.title('Original Image')
plt.imshow(image)
plt.show()

得到结果如下:
在这里插入图片描述

4. 统计数据分析

为了分析图像中的统计信息,让我们使用以下函数:

def calc_color_overcast(image):
    # Calculate color overcast for each channel
    red_channel = image[:, :, 0]
    green_channel = image[:, :, 1]
    blue_channel = image[:, :, 2]

    # Create a dataframe to store the results
    channel_stats = pd.DataFrame(columns=['Mean', 'Std', 'Min', 'Median', 'P_80', 'P_90', 'P_99', 'Max'])

    # Compute and store the statistics for each color channel
    for channel, name in zip([red_channel, green_channel, blue_channel], ['Red', 'Green', 'Blue']):
        mean = np.mean(channel)
        std = np.std(channel)
        minimum = np.min(channel)
        median = np.median(channel)
        p_80 = np.percentile(channel, 80)
        p_90 = np.percentile(channel, 90)
        p_99 = np.percentile(channel, 99)
        maximum = np.max(channel)

        channel_stats.loc[name] = [mean, std, minimum, median, p_80, p_90, p_99, maximum]

    return channel_stats

得到结果如下:
在这里插入图片描述
从上面数据帧中的结果可以明显看出,图像中出现了蓝色伪影。仔细分析具有每个百分位数的最高平均值、中值都是蓝色通道最大。

5. White Patch Algorithm

白色补丁算法是图像处理中最常用的一种颜色平衡方法,旨在通过缩放颜色通道来校正图像中的颜色投射,从而使每个通道中最亮的像素变为白色。这是通过假设图像中最亮的像素是白色来实现的。

相应的代码实现如下:

def white_patch(image, percentile=100):
    """
    Returns a plot comparison of original and corrected/white balanced image 
    using the White Patch algorithm.

    Parameters
    ----------
    image : numpy array
            Image to process using white patch algorithm
    percentile : integer, optional
                  Percentile value to consider as channel maximum
    """
    white_patch_image = img_as_ubyte(
        (image * 1.0 / np.percentile(image, 
                                     percentile, 
                                     axis=(0, 1))).clip(0, 1))
    # Plot the comparison between the original and white patch corrected images
    fig, ax = plt.subplots(1, 2, figsize=(10, 10))
    ax[0].imshow(image)
    ax[0].set_title('Original Image')
    ax[0].axis('off')

    ax[1].imshow(white_patch_image, cmap='gray')
    ax[1].set_title('White Patch Corrected Image')
    ax[1].axis('off')

    plt.show()

# Read the input image
image = imread('qmark.png')

# Call the function to implement white patch algorithm
white_patch(image, 100)

结果如下:
在这里插入图片描述
使用默认参数 percentile=100并没有明显改善我们的图像,因为图像具有中RGB最大值已经为[255255255],观察上一章节中的统计信息,可以看到其中最大值和99百分位数都是255。

为了解决上述问题,我们可以将像素值的较低百分位数视为最大值,而不是绝对最大值。因此,让我们尝试下85百分位:

white_patch(image, 85)

结果如下:
在这里插入图片描述
使用第85个百分位正确地改善了图像的颜色。可以看到,以下颜色已正确分配:

问号块:黄色
马里奥帽子:红色
背包:棕色
鞋子:黑色和白色
墙壁:不同深浅的蓝色

6. 优缺点分析

我们接下来分析上述算法的优缺点,其有点可以总结如下:

  • 简单易用。
  • 可以有效地纠正具有主要白色区域或中性灰色区域的图像中的白平衡问题。
  • 当图像中有明显的明亮区域时,效果很好。

其缺点可以概括如下:

  • 假设图像中最亮的颜色是白色,这可能并不总是正确的。
  • 如果假设不成立,可能会导致过度校正,从而导致不自然的颜色或伪影。
  • 可能导致图像的某些区域出现颜色偏移或伪影。

7. 总结

本文重点介绍了使用White Patch Algorithm进行白平衡进而改善图像成像质量的算法细节,并总结了该算法的优缺点并给出了相应的代码示例。

您学废了嘛?

参考

猜你喜欢

转载自blog.csdn.net/sgzqc/article/details/131876394