自动白平衡算法

前言 计算机图像处理技术已经广泛的应用于人们的日常生活当中,自动白平衡是计算机图像处理当中的一个重要技术,不同的白平衡算法根据不同的预设校正基准从原始偏色图像中得到图像的偏色信息,然后根据计算所得的偏色信息对图像进行校正恢复。自动白平衡算法主要应用在数码设备白平衡系统、扫描仪偏色图像校正以及计算机图像处理软件当中。在a0幅面平台彩色扫描仪中,由于扫描光照环境不标准、ccd头感光器件的光感参数偏差等原因,都会使扫描所得的数字图像色彩出现偏差。
1.预备知识-----自动对比度调整原理 自动对比度调整的方法中,我们假设alow和ahight为单前图像中的最小值和最大值,需要映射的范围为[amin,amax],为了使得图像映射到整个映射范围,我们首先把最小值alow映射到0,之后用比例因子(amax-amin)/(ahigh-alow)增加其对比度,随后加上amin使得计算出来的值映射到需要的映射范围。其具体公式为:
对于8bit图像而言,amin = 0,amax = 255。
故上述公式可以改写为:
实现原理图如下:
实际的图像中,上述的映射函数容易受到个别极暗或及亮像素的影响,导致映射可能出现错误。为了避免这种错误,我们选取较低像素和较亮像素的一定比例qlow,qhigh,并根据此比例重新计算alow和ahigh,以重新计算的alow和ahigh为原始的图像的亮度范围进行映射。我们可以通过累计直方图h(i)很方便的计算出最新的alow和ahigh。
其中,0《=qlow ,qhigh《=1,qlow+qhigh《=1,m*n为图像的像素数量。
其原理图如下所示:
2. 基本原理 这种简单的白平衡算法是基于这么一种假设:图像中r、g、b最高灰度值对应于图像中的白点,最低灰度值的对应于图像中最暗的点。这种算法类似于自动对比度增强的方法,使用映射函数ax+b,尽可能的把彩色图像中r、g、b三个通道内的像素灰度值映射到[0.255]的范围内。由于一般图像中已经有很多图像的灰度值已经在[0,255]范围内(对应24bit bmp图像而言,每个通道内的像素都在此范围内。但是对于每个像素用16bit或更高的32bit表示的话,需要进过上述的映射方法,把像素灰度值映射到[0,255]范围内),因此,这种方法选取较高亮度的像素一定比例赋予255,选取较暗亮度的像素的一定比例赋予0。
具体实现方法: 1.排序。为了获取高亮度一定比例像素和较低亮度的一定比例像素,不要对整个图像的灰度值进行排序,方便选择对应的像素。
2.从排序的像素数组中选取一定比例的高亮和较暗像素。假设s = s1+s2 =[0,100],我们需要选取n*s/100个像素。其中vmin和vmax应该选择位于排序后数组的位置为n*s1 /100和n(1-*s2/100)-1处的像素灰度值。
3.填充像素。由上一步定义的vmin和vmax,把原始图像中低于或等于vmin的像素灰度值赋予0;把原始像素中高于或等于vmax的像素灰度值赋予255。
4.映射像素灰度值。使用函数f(x) =(x-vmin)*(max-min)/(vmax-vmin)把原始图像中位于(vmin,vmax)的像素映射到[0,255]范围内。其中min,max分别为0和255。
当图像较大的时候,图像的像素可达到百万级,对这么大数量的像素进行排序,往往效率比较低。另外一种方法可以像上述自动对比度调整那样,建立一个256大小的数组,再以数组中n*s1/100和n(1-*s2/100)-1处的像素灰度值赋予vmin和vma。然后再进行像素值得映射。
算法实现的伪代码为:
3.关键代码 /*
*function: 一种简单的图像白平衡算法--对比度增强
*/
void quantiles_u8(const t_u8 *data, size_t img_size,
size_t nb_min, size_t nb_max,
t_u8*ptr_min,t_u8 *ptr_max)
{
/*
* the histogram must hold all possible “unsigned char” values,
* including 0
*/
size_t h_size = uchar_max + 1;
size_t histo[uchar_max + 1];
size_t i;
/* make a cumulative histogram */
memset(histo, 0x00, h_size * sizeof(size_t));
for (i = 0; i 《 img_size; i++)
histo[(size_t) data[i]] += 1;
for (i = 1; i 《 h_size; i++)
histo[i] += histo[i - 1];
/* get the new min/max */
if (null != ptr_min) {
/* simple forward traversal of the cumulative histogram */
/* search the first value 》 nb_min */
i = 0;
while (i 《 h_size && histo[i] 《= nb_min)
i++;
/* the corresponding histogram value is the current cell position */
*ptr_min = (t_u8) i;
}
if (null != ptr_max) {
/* simple backward traversal of the cumulative histogram */
/* search the first value 《= size - nb_max */
i = h_size - 1;
/* i is unsigned, we check i《h_size instead of i》=0 */
while (i 《 h_size && histo[i] 》 (img_size - nb_max))
i--;
/*
* if we are not at the end of the histogram,
* get to the next cell,
* the last (backward) value 》 size - nb_max
*/
if (i 《 h_size - 1)
i++;
*ptr_max = (t_u8) i;
}
return;
}
void minmax_u8(const t_u8 *data, size_t size,
t_u8 *ptr_min, t_u8 *ptr_max)
{
t_u8 min, max;
size_t i;
/* compute min and max */
min = data[0];
max = data[0];
for (i = 1; i 《 size; i++) {
if (data[i] 《 min)
min = data[i];
if (data[i] 》 max)
max = data[i];
}
/* save min and max to the returned pointers if available */
if (null != ptr_min)
*ptr_min = min;
if (null != ptr_max)
*ptr_max = max;
return;
}
t_u8 *rescale_u8(t_u8 *data, size_t size,t_u8 min,t_u8 max)
{
size_t i;
if (max 《= min)
for (i = 0; i 《 size; i++)
data[i] = uchar_max / 2;
else {
/* build a normalization table */
t_u8 norm[uchar_max + 1];
for (i = 0; i 《 min; i++)
norm[i] = 0;
for (i = min; i 《 max; i++)
/*
* we can‘t store and reuse uchar_max / (max - min) because
* 105 * 255 / 126. -》 212.5, rounded to 213
* 105 * (double) (255 / 126.) -》 212.4999, rounded to 212
*/
norm[i] = (t_u8) ((i - min) * uchar_max
/ (double) (max - min) + .5);
for (i = max; i 《 uchar_max + 1; i++)
norm[i] = uchar_max;
/* use the normalization table to transform the data */
for (i = 0; i 《 size; i++)
data[i] = norm[(size_t) data[i]];
}
return data;
}
t_u8 *balance_u8(t_u8* bmp8_data_img,size_t img_size,size_t nb_min, size_t nb_max)
{
unsigned char min, max;
/* sanity checks */
if (null == bmp8_data_img) {
fprintf(stderr, “a pointer is null and should not be so”);
abort();
}
if (nb_min + nb_max 》= img_size) {
nb_min = (img_size - 1) / 2;
nb_max = (img_size - 1) / 2;
fprintf(stderr, “the number of pixels to flatten is too large”);
fprintf(stderr, “using (size - 1) / 2”);
}
/* get the min/max */
if (0 != nb_min || 0 != nb_max)
quantiles_u8(bmp8_data_img,img_size, nb_min, nb_max, &min, &max);
else
minmax_u8(bmp8_data_img, img_size, &min, &max);
(void)rescale_u8(bmp8_data_img, img_size, min, max);
return bmp8_data_img;
}
int simplest_24bitcolor_balance(image_type *bmp24_img,dword width,dword height,float smin,float smax)
{
t_u32 linebyte,source_linebyte,source_index,dst_index;
t_u16 k = 0,i,j;
t_u8 *dst_bmp24_img,*dst_bmp24_data,*r_img,*g_img,*b_img;
int newbibitcount =8;
size_t img_size = width*height;
float nb_min = img_size*smin/100.0,nb_max = img_size*smax/100.0;
file *simplest_color_balance_bmp8bit_fp = fopen(“simplest_24bitcolor_balance.bmp”,“wb”);
if(null == simplest_color_balance_bmp8bit_fp)
{
printf(“can’t open simplest_color_balance.bmp”);
return exit_failure;
}
if (0. 》 smin || 100. 《= smin || 0. 》 smax || 100. 《= smax) {
fprintf(stderr, “the saturation percentages must be in [0-100[”);
return exit_failure;
}
linebyte = (width * 8 / 8 + 3) / 4 * 4;
source_linebyte = ( (width * 24 / 8 + 3) / 4 * 4);
dst_bmp24_img = (t_u8*)malloc(source_linebyte*height+bmpheadsize);
r_img = (t_u8*)malloc(linebyte*height);
g_img = (t_u8*)malloc(linebyte*height);
b_img = (t_u8*)malloc(linebyte*height);
check_malloc_tu8_valid(dst_bmp24_img);
check_malloc_tu8_valid(g_img);
check_malloc_tu8_valid(b_img);
check_malloc_tu8_valid(r_img);
memcpy(dst_bmp24_img,bmp24_img,source_linebyte*height+bmpheadsize);
dst_bmp24_data = dst_bmp24_img+bmpheadsize;
for (i = 0; i 《 height;i++)
{
source_index = i*source_linebyte;
dst_index = i*linebyte;
for (j = 0; j 《 width;j++)
{
r_img[dst_index] = dst_bmp24_data[source_index+2];
g_img[dst_index] = dst_bmp24_data[source_index+1];
b_img[dst_index] = dst_bmp24_data[source_index+0];
source_index += 3;
dst_index += 1;
}
}
(void)balance_u8(r_img,img_size,(size_t)nb_min,(size_t)nb_max);
(void)balance_u8(g_img,img_size,(size_t)nb_min,(size_t)nb_max);
(void)balance_u8(b_img,img_size,(size_t)nb_min,(size_t)nb_max);
for (i = 0; i 《height;i++)
{
source_index = i*source_linebyte;
dst_index = i*linebyte;
for (j = 0; j 《width;j++)
{
dst_bmp24_data[source_index+2] = r_img[dst_index];
dst_bmp24_data[source_index+1] = g_img[dst_index];
dst_bmp24_data[source_index+0] = b_img[dst_index];
source_index += 3;
dst_index += 1;
}
}
fwrite(bmp24_img,bmpheadsize, 1, simplest_color_balance_bmp8bit_fp);
fwrite(dst_bmp24_data, source_linebyte*height, 1, simplest_color_balance_bmp8bit_fp);
fclose(simplest_color_balance_bmp8bit_fp);
simplest_color_balance_bmp8bit_fp = null;
free(dst_bmp24_img);
free(r_img);
free(g_img);
free(b_img);
return 0;
}
#p##e#
4.图像效果
a色温白平衡校正前后对比图
tl84色温白平衡校正前后对比图
如算法原理所述,使用的是类似于自动对比度调整的方法对图像进行白平衡校正。这种方法也能够很好的对比较朦胧的图像进行对比度的调整,提高图像的通透性。
彩色图像的对比度提升
灰度图像的对比图提升

MOSFET厂将在2021年受惠涨价潮 有望持续缴出新高水准
Switch Lite怎么样 值不值得买
谷歌开源VR180格式 探索其更多可能性
专注工业产品开发的瑞米派强势来袭
南方电网发布招标公告 智能电表市场继续稳步增长
自动白平衡算法
量身定制的LVDS连接器的好处
基于CW32系列MCU的MPU6050姿态传感器设计
苹果新ipadpro配置曝光 超强性能,或成最强平板电脑
苹果公司与芯片初创公司Rivos和解谈判中
氢燃料汽车充电只需要5分钟 续航里程均超过500公里
2016年三星PK苹果营收仅为一半左右
在可调降压型DC-DC转换器设计中使用数字电位器
UMTS通信技术特性及技术分析
问界M9发布!余承东称1000万以内最好的SUV,正加速提升产能
静电对策和ESD对策
推荐一款集成电压控制振荡器HMC765LP6CE
海信新专利曝光 背面设计形如专业相机
新唐科技W584B031主板介绍
引入LIPO屏,苹果iPhone 15恐延至10月