自动白平衡与Grayworld假设

Sap*_*shi 2 python opencv numpy image-processing

我一直在尝试实现以下提供的白平衡算法:https: //pippin.gimp.org/image-processing/chapter-automaticadjustments.html

我用python和opencv来实现它们.我无法产生与网站相同的结果.

例如,在灰色世界的假设中,我使用以下代码:

import cv2 as cv
import numpy as np

def show(final):
    print 'display'
    cv.imshow("Temple", final)
    cv.waitKey(0)
    cv.destroyAllWindows()

def saveimg(final):
    print 'saving'
    cv.imwrite("result.jpg", final)

# Insert any filename with path
img = cv.imread("grayworld_assumption_0.png")
res = img
final = cv.cvtColor(res, cv.COLOR_BGR2LAB)

avg_a = -np.average(final[:,:,1])
avg_b = -np.average(final[:,:,2])

for x in range(final.shape[0]):
    for y in range(final.shape[1]):
        l,a,b = final[x][y]
        shift_a = avg_a * (l/100.0) * 1.1
        shift_b = avg_b * (l/100.0) * 1.1
        final[x][y][1] = a + shift_a
        final[x][y][2] = b + shift_b

final = cv.cvtColor(final, cv.COLOR_LAB2BGR)
final = np.hstack((res, final))
show(final)
saveimg(final)
Run Code Online (Sandbox Code Playgroud)

我得到的结果

代替

我哪里错了?

nor*_*ok2 12

在8位颜色深度的情况下,您正在实现的文档不了解LAB定义的CV内部约定.

特别是:

L: L / 100 * 255
A: A + 128
B: B + 128
Run Code Online (Sandbox Code Playgroud)

我相信这样做是为了提高准确性,因为这样就可以完全使用unsigned int8精度来获得亮度,同时为整个阵列保持一致的无符号数据类型.

改编自您的以下代码应该可行.请注意,这里和那里有一些小的修复(编辑包括在函数中包装有趣的代码),但实际的是在嵌套for循环内.

from __future__ import (
    division, absolute_import, print_function, unicode_literals)

import cv2 as cv
import numpy as np


def show(final):
    print('display')
    cv.imshow('Temple', final)
    cv.waitKey(0)
    cv.destroyAllWindows()

# Insert any filename with path
img = cv.imread('grayworld_assumption_0.png')

def white_balance_loops(img):
    result = cv.cvtColor(img, cv.COLOR_BGR2LAB)
    avg_a = np.average(result[:, :, 1])
    avg_b = np.average(result[:, :, 2])
    for x in range(result.shape[0]):
        for y in range(result.shape[1]):
            l, a, b = result[x, y, :]
            # fix for CV correction
            l *= 100 / 255.0
            result[x, y, 1] = a - ((avg_a - 128) * (l / 100.0) * 1.1)
            result[x, y, 2] = b - ((avg_b - 128) * (l / 100.0) * 1.1)
    result = cv.cvtColor(result, cv.COLOR_LAB2BGR)
    return result

final = np.hstack((img, white_balance_loops(img)))
show(final)
cv.imwrite('result.jpg', final)
Run Code Online (Sandbox Code Playgroud)

编辑:

通过避免循环可以获得相同的结果,但具有更快的性能:

def white_balance(img):
    result = cv.cvtColor(img, cv.COLOR_BGR2LAB)
    avg_a = np.average(result[:, :, 1])
    avg_b = np.average(result[:, :, 2])
    result[:, :, 1] = result[:, :, 1] - ((avg_a - 128) * (result[:, :, 0] / 255.0) * 1.1)
    result[:, :, 2] = result[:, :, 2] - ((avg_b - 128) * (result[:, :, 0] / 255.0) * 1.1)
    result = cv.cvtColor(result, cv.COLOR_LAB2BGR)
    return result
Run Code Online (Sandbox Code Playgroud)

这显然给出了相同的结果:

print(np.all(white_balance(img) == white_balance_loops(img)))
True
Run Code Online (Sandbox Code Playgroud)

但时间差异很大:

%timeit white_balance(img)
100 loops, best of 3: 2 ms per loop

%timeit white_balance_loops(img)
1 loop, best of 3: 529 ms per loop
Run Code Online (Sandbox Code Playgroud)