自动色阶算法用于图像增强,去雾等,其思想去掉像素值最高的比例,去掉像素值最低的一些比例,然后在将图像的其余像素值进行线性映射或者伽马校正至[0, 255]区间。如下图,在图像处理中是不是很熟悉?!
本文主要参考人在旅途的博客,采用matlab语言实现,算法不在详细叙述,在此代码留做备注。http://www.cnblogs.com/Imageshop/archive/2011/11/13/2247614.html。
以下是程序代码:
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)); imDst(:,:,2) = GreenMap(I(:,:,2)); imDst(:,:,3) = BlueMap(I(:,:,3)); 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(:,:,1) = GrayMap (I(:,:)); 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的去雾,但是这些算法还不是很稳定,当先验知识失效时,处理失真比较严重。顺便一提的是,美图秀秀的一键去雾即是采用此方法。