2

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

 1 year ago
source link: https://blog.51cto.com/u_15506603/6872275
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

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

精选 原创

sgzqc 2023-07-27 20:02:49 博主文章分类:python技巧 ©著作权

文章标签 代码实现 图像处理 文章分类 Python 后端开发 yyds干货盘点 阅读数180

在上一节中我们介绍了白平衡算法的原理,并详细实现了基于白色补丁算法的白平衡实现,本文继续就白平衡的其他算法实现进行展开。

闲话少说,我们直接开始吧!

2. Gray-world Algorithm

灰色世界算法(Gray-world Algotithm)假设图像的平均颜色应该是灰色的。它会相应地调整颜色通道以校正颜色映射关系。

代码实现如下:

def gray_world(image):
    """
    Returns a plot comparison of original and corrected/white balanced image 
    using the Gray World algorithm.

    Parameters
    ----------
    image : numpy array
            Image to process using gray world algorithm
    """
    # Apply the Gray World algorithm
    image_grayworld = ((image * (image.mean() / image.mean(axis=(0, 1)))).clip(0, 255).astype(int))

    # Exclude alpha or opacity channel (transparency)
    if image.shape[2] == 4:
        image_grayworld[:, :, 3] = 255

    # Plot the comparison between the original and gray world corrected images
    fig, ax = plt.subplots(1, 2, figsize=(14, 10))
    ax[0].imshow(image)
    ax[0].set_title('Original Image')
    ax[0].axis('off')

    ax[1].imshow(image_grayworld)
    ax[1].set_title('Gray World Corrected Image')
    ax[1].axis('off')

    plt.show()

# Call the function to apply the Gray World algorithm
gray_world(image)

结果如下:

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

3. 算法优缺点

我们观察上述例子的可视化结果,可以看出基于灰色世界算法提供了比白色补丁算法更温和的结果。这主要是由于灰色平均颜色的假设,它并没有完美地将问号块转换为黄色,但红色、蓝色、白色、黑色和棕色得到了正确的增强。

针对上述实现,可以总结出该算法的优点归纳如下:

  • 简单且计算高效。
  • 假设图像的平均颜色为灰色,这通常是一般场景的合理性假设。

同时其缺点也可以归纳如下:

  • 可能对大面积存在的单一主色较为敏感,导致颜色平衡算法无法正常工作。
  • 假设图像的平均颜色应该是灰色,这种假设可能并不总是正确的。

4. Ground Truth Algorithm

接着我们来介绍最后一种白平衡常用的算法 -- Ground Truth Algorithm,该算法需要图像中已知对象的颜色作为参考色。它根据此参考色来调整颜色通道以更正颜色投射关系。

代码实现如下:

def ground_truth(image, img_patch, mode='mean'):
    """
    Returns a plot comparison of original and corrected/white balanced image 
    using the Ground Truth algorithm.

    Parameters
    ----------
    image : numpy array
            Image to process using ground truth algorithm
    img_patch : numpy array
                Reference image patch
    mode : string, optional
           Calculation mode, either 'mean' or 'max'
    """
    if mode == 'mean':
        image_gt = ((image * (img_patch.mean() / image.mean(axis=(0, 1)))).clip(0, 255).astype(int))
    if mode == 'max':
        image_gt = ((image * 1.0 / img_patch.max(axis=(0, 1))).clip(0, 1))

    # Exclude alpha or opacity channel (transparency)
    if image.shape[2] == 4:
        image_gt[:, :, 3] = 255

    # Plot the comparison between the original and ground truth corrected images
    fig, ax = plt.subplots(1, 2, figsize=(14, 10))
    ax[0].imshow(image)
    ax[0].set_title('Original Image')
    ax[0].axis('off')

    ax[1].imshow(image_gt)
    ax[1].set_title('Ground Truth Corrected Image')
    ax[1].axis('off')

    plt.show()

5. 挑选参考色块

一般来说,考虑到在正常光线下,问号块看起来是这样的:

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

接着,我们挑选问号内的任何正方形块的颜色作为该算法的图像补丁(下图红色矩形框):

from matplotlib.patches import Rectangle
fig, ax = plt.subplots(figsize=(10,10))
ax.set_title('Reference patch in red square')
ax.imshow(image)
ax.add_patch(Rectangle((1800, 800), 50, 50, edgecolor='r', facecolor='none'));

效果如下:

用OpenCV图像处理技巧之白平衡算法(二)_代码实现_03

6. 算法效果

有了上述参考色块,我们可以调用上述实现的ground_truth函数,参考代码如下:

# To visualize the specific reference patch
img_patch = image[3845:3865, 1820:1840]
# Call the function to apply the Ground Truth algorithm
ground_truth(image, img_patch, mode='mean')

得到结果如下:

用OpenCV图像处理技巧之白平衡算法(二)_代码实现_04

使用白色灯光色块作为参考补丁会导致图像看起来过于明亮。这是因为算法试图根据参考补丁的颜色来调整颜色平衡映射关系。如果参考补丁是明亮的源,则算法可能会过度补偿,导致具有褪色颜色的明亮图像,如在上述示例中显而易见的。

7. 其他效果

为了更加充分的展示 Ground Truth Algorithm的效果,这里我们选择鞋子色块作为我们的参考图像补丁,样例代码如下:

# To visualize the specific reference patch
img_patch = image[3845:3865, 1820:1840]
imshow(img_patch);
# Call the function to apply the Ground Truth algorithm
ground_truth(image, img_patch, mode='mean')

补丁展示如下:

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

白平衡后的效果如下:

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

可以看到白平衡处理后的效果和参考色块整体色调基本保持一致。

最后的最后,我们对本系列讲解白平衡算法的三种效果进行汇总,结果如下:

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

本文所介绍的三种白平衡算法都矫正了图像中的颜色投射关系,最终,算法的选择取决于应用程序的具体要求和假设。一般来说,White PatchGray world算法适用于通用颜色校正,而Ground Truth算法非常适合有参考白色补丁的专业应用。

您学废了嘛?

  • 收藏
  • 评论
  • 分享
  • 举报

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK