自动色阶算法用于图像增强,去雾等,其思想去掉像素值最高的比例,去掉像素值最低的一些比例,然后在将图像的其余像素值进行线性映射或者伽马校正至[0, 255]区间。如下图,在图像处理中是不是很熟悉?!
本文主要参考人在旅途的博客,采用matlab语言实现,算法不在详细叙述,在此代码留做备注。
function imDst = autolevel(varargin)
[I,lowCut,highCut] =parse_inputs(varargin{:});
[hei,wid,~] = size(I);
PixelAmount = wid * hei;
if size(I,3)==3
[HistRed,~] = imhist(I(:,:,1));
[HistGreen,~] = imhist(I(:,:,2));
[HistBlue,~] = imhist(I(:,:,3));
CumRed = cumsum(HistRed);
CumGreen = cumsum(HistGreen);
CumBlue = cumsum(HistBlue);
minR =find(CumRed>=PixelAmount*lowCut,1,‘first‘);
minG = find(CumGreen>=PixelAmount*lowCut,1,‘first‘);
minB =find(CumBlue>=PixelAmount*lowCut,1,‘first‘);
maxR =find(CumRed>=PixelAmount*(1-highCut),1,‘first‘);
maxG =find(CumGreen>=PixelAmount*(1-highCut),1,‘first‘);
maxB = find(CumBlue>=PixelAmount*(1-highCut),1,‘first‘);
RedMap = linearmap(minR,maxR);
GreenMap = linearmap(minG,maxG);
BlueMap = linearmap(minB,maxB);
imDst = zeros(hei,wid,3,‘uint8‘);
imDst(:,:,1) = RedMap (I(:,:,1)+1);
imDst(:,:,2) = GreenMap(I(:,:,2)+1);
imDst(:,:,3) = BlueMap(I(:,:,3)+1);
else
HistGray = imhist(I(:,:));
CumGray = cumsum(HistRed);
minGray =find(CumGray>=PixelAmount*lowCut,1,‘first‘);
maxGray =find(CumGray>=PixelAmount*(1-highCut),1,‘first‘);
GrayMap = linearmap(minGray,maxGray);
imDst = zeros(hei,wid,‘uint8‘);
imDst(:,:) = GrayMap (I(:,:)+1);
end
%--------------------------------------------------------------------
function map = linearmap(low,high)
map = [0:1:255];
for i=0:255
if(i<low)
map(i+1) = 0;
elseif (i>high)
map(i+1) = 255;
else
map(i+1) =uint8((i-low)/(high-low)*255);
end
end
%-------------------------------------------------------------------
function [I,lowCut,highCut] = parse_inputs(varargin)
narginchk(1,3)
I = varargin{1};
validateattributes(I,{‘double‘,‘logical‘,‘uint8‘,‘uint16‘,‘int16‘,‘single‘},{},...
mfilename,‘Image‘,1);
if nargin == 1
lowCut = 0.005;
highCut = 0.005;
elseif nargin == 3
lowCut = varargin{2};
highCut = varargin{3};
else
error(message(‘images:im2double:invalidIndexedImage‘,‘single, or logical.‘));
end
I = imread(‘bikes1.jpg‘);
Out = autolevel(I,0.005,0.005);
imshow([I Out])
自动色阶用来做去雾,还是比较稳定的,特别是对于雾分布均匀,效果很好;缺点也比较明显,不能自适应雾的分布,且通道分离计算,有些时候会带来一些颜色失真现象。现在也有很多种其他增强方法,例如暗通道去雾,优化对比度去雾,基于Color-Lines的去雾,但是这些算法还不是很稳定,当先验知识失效时,处理失真比较严重。顺便一提的是,美图秀秀的一键去雾即是采用此方法。
另外,自动对比度算法与自动色阶及其类似,可以参考更多内容,在此不再详述。
http://www.cnblogs.com/Imageshop/archive/2011/11/13/2247614.html
http://blog.csdn.net/bluecol/article/details/45422653
作者 | 更新日期 | 联系方式 |
---|---|---|
风吹夏天 | 2015年5月8日 | 272793757@qq.com |
原文地址:http://blog.csdn.net/bluecol/article/details/45576827