白平衡算法

发布于 2024-07-29 09:05:20 字数 1542 浏览 9 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

渔村楼浪 2024-08-05 09:05:20

GIMP 显然使用了一种非常简单的自动白平衡算法。
http://docs.gimp.org/en/gimp-layer-white -balance.html

白平衡命令通过分别拉伸红色、绿色和蓝色通道来自动调整活动图层的颜色。 为此,它会丢弃红色、绿色和蓝色直方图两端的像素颜色(这些直方图仅被图像中 0.05% 的像素使用),并尽可能地拉伸剩余范围。 结果是,与拉伸对比度相比,在直方图外边缘很少出现的像素颜色(可能是灰尘等)不会对用于拉伸直方图的最小值和最大值产生负面影响。 然而,与“拉伸对比度”一样,生成的图像可能会出现色调变化。

有比这里描述的更多的调整,因为我第一次尝试实现这项工作似乎适用于大多数照片,但其他照片似乎有伪影或包含太多的红绿或蓝色:/

GIMP apparently uses a very simple algorithm for automatic white balancing.
http://docs.gimp.org/en/gimp-layer-white-balance.html

The White Balance command automatically adjusts the colors of the active layer by stretching the Red, Green and Blue channels separately. To do this, it discards pixel colors at each end of the Red, Green and Blue histograms which are used by only 0.05% of the pixels in the image and stretches the remaining range as much as possible. The result is that pixel colors which occur very infrequently at the outer edges of the histograms (perhaps bits of dust, etc.) do not negatively influence the minimum and maximum values used for stretching the histograms, in comparison with Stretch Contrast. Like “Stretch Contrast”, however, there may be hue shifts in the resulting image.

There is a bit more tweaking than is described here since my first attempt at implementing this works seems to work for most photos but other photos seem to have artifacts or contain too much of either red green or blue :/

情绪少女 2024-08-05 09:05:20

一个相对简单的算法是对屏幕上最亮和最暗像素的色调(HSV 或 HSL)进行平均。 在紧要关头,仅使用最亮的像素。 如果最亮和最暗之间的色调差异太大,请选择明亮的像素。 如果黑暗接近黑色,则选择明亮的像素。

为什么还要关注暗像素? 有时,黑暗并不接近黑色,而是暗示环境光或雾或霾。

如果您是 Photoshop 的重度用户,这对您来说很有意义。 照片中的高光与物体的底层颜色无关(或微弱相关)。 它们是光线色偏的最佳表现,除非图像曝光过度以至于一切都淹没了 CCD。

然后调整所有像素的色调。

您将需要快速的 RGB 到 HSV 和 HSV 到 RGB 函数。 (但也许您可以使用 LUT 或线性插值在 RGB 中进行像素校正。)

您不想使用平均像素颜色或最流行的颜色。 那就是疯狂。

要快速找到最亮的颜色(和最暗的颜色),您可以使用 RGB,但您应该有绿色、红色和蓝色的乘数。 在 RGB 显示器上,255 绿色比 255 红色更亮,而 255 红色又比 255 蓝色更亮。 我的脑海里曾经有过很好的乘数,但可惜的是,它们已经从我的记忆中消失了。 你也许可以用谷歌搜索他们。

对于没有高光的图像,此操作将失败。 例如,哑光漆墙。 但我不知道你能对此做些什么。


这个简单的算法还有很多改进之处。 您可以平均多个明亮像素、对图像进行网格化并从每个单元格中抓取亮像素和暗像素等。实现算法后,您会发现一些明显的调整。

A relatively simple algorithm is to average the hues (in HSV or HSL) of the brightest and darkest pixels on the screen. In a pinch, go with the brightest pixel only. If the hues between brightest and darkest are too different, go with the bright pixel. If the dark is near black go with the bright pixel.

Why even look at the dark pixel? Sometimes the dark is not near black, and hints at the ambient light or fog or haze.

This will make sense to you if you're a heavy Photoshop user. Highlights in a photo are unrelated (or weakly related) to the underlying color of the object. They are your best representation of the color cast of the light, unless the image is so overexposed that everything has overwhelmed the CCDs.

Then adjust the hues of all pixels.

You'll need fast RGB to HSV and HSV to RGB functions. (But maybe you can work in RGB for the pixel corrections with a LUT or linear interpolation.)

You don't want to go by average pixel color or most popular color. That way lies madness.

To quickly find the brightest color (and the darkest one), you can work in RGB, but you should have multipliers for green, red, and blue. On an RGB monitor, 255 green is brighter than 255 red which is brighter than 255 blue. I used to have good multipliers in my head, but alas, they have fled my memory. You can probably google for them.

This will fail in an image which has no highlights. A matte painted wall, for example. But I don't know what you can do about that.


There are many improvements to make to this simple algorithm. You can average multiple bright pixels, grid the image and grab bright and dark pixels from each cell, etc. You'll find some obvious tweaks after implementing the algorithm.

疏忽 2024-08-05 09:05:20

@Charles Ma 建议使用 Gimp 白平衡算法。 在 python 和 numpy 中,这可能看起来像这样:

# white balance for every channel independently
def wb(channel, perc = 0.05):
    mi, ma = (np.percentile(channel, perc), np.percentile(channel,100.0-perc))
    channel = np.uint8(np.clip((channel-mi)*255.0/(ma-mi), 0, 255))
    return channel

image = cv2.imread("foo.jpg", 1) # load color
imWB  = np.dstack([wb(channel, 0.05) for channel in cv2.split(img)] )

它快速、简单并且提供相当不错的结果

@Charles Ma has suggested to use the Gimp white balance algorithm. In python and numpy this could look like this:

# white balance for every channel independently
def wb(channel, perc = 0.05):
    mi, ma = (np.percentile(channel, perc), np.percentile(channel,100.0-perc))
    channel = np.uint8(np.clip((channel-mi)*255.0/(ma-mi), 0, 255))
    return channel

image = cv2.imread("foo.jpg", 1) # load color
imWB  = np.dstack([wb(channel, 0.05) for channel in cv2.split(img)] )

it's fast, simple and provides pretty decent results

海风掠过北极光 2024-08-05 09:05:20

白平衡算法很难。 即使数码相机也偶尔会出错,尽管它们知道有关图片的许多额外信息 - 例如是否使用闪光灯以及光线水平。

对于初学者来说,我只会平均红色、绿色和蓝色,并将其用作白平衡点。 对其设置限制 - 保持在钨丝灯、荧光灯和日光的范围内。 它不会是完美的,但当它出错时,解释原因会相对容易。

White balancing algorithms are hard. Even digital cameras get the wrong once in a while, even though they know a lot of extra info about the picture - such as whether the flash was used, and the light level.

For starters, I would just average red, green, and blue, and use that as the white balance point. Set limits on it - stay within the ranges for tungsten, flourescent, and daylight. It won't be perfect, but when its wrong, it will be relatively easy to explain why.

风追烟花雨 2024-08-05 09:05:20

最近发布的一种算法是颜色分布算法:

D. Cheng、DK Prasad 和 MS Brown,
“颜色恒定性的光源估计:空间域方法为何有效以及颜色分布的作用”
美国光学学会杂志 A 31(5):1049-1058 (2014)
DOIPDF

论文中还引用了 Matlab 源代码(互联网档案馆)。 这是一个简单的算法,可以轻松编程,结果表明它非常快。

如果您需要额外的快速且同时准确的白平衡(颜色恒常性)算法,您应该检查 此网站

有几种算法及其各自的源代码,可能正是您所寻找的。

One recently published algorithm is the Color Distribution algorithm:

D. Cheng, D.K. Prasad, and M.S. Brown,
"Illuminant Estimation for Color Constancy: Why spatial domain methods work and the role of the color distribution"
Journal of the Optical Society of America A 31(5):1049-1058 (2014)
DOI, PDF

In the paper there is also the reference to the Matlab source code (Internet Archive). It's a simple algorithm that can be easily programmed and the results show it to be very fast.

If you need additional fast and at the same time accurate white balancing (color constancy) algorithms, you should check this site.

There are several algorithms with their respective source coded that might be just the ones you look for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文