imadjust
寄语
读研的时候主要用的语言就是Matlab,工作前2年都用的C/C++,以至于因为Matlab不能做成产品而鄙视它。现在的工作主要是算法仿真,发现还是Matlab好用!
Matlab中对函数imadjust的解释是:
调节图像的亮度或者颜色映射(colormap)
1、imadjust( I )
J = imadjust(I) 把灰度图像I中的亮度值,映射到J中,把图像I中亮度处于[ ilow, ihigh]范围内的像素,拉伸到[0, 255]。这样可以增加输出图像J的对比度。imadjust(I)等价于imadjust(I,stretchlim(I))。
因此,有必要仔细理解函数stretchlim(I)。
stretchlim
寻找图像对比度拉伸的上下限
function lowhigh = stretchlim(varargin)
[img,tol] = ParseInputs(varargin{:});
if isa(img,'uint8')
nbins = 256;
else
nbins = 65536;
end
tol_low = tol(1);
tol_high = tol(2);
p = size(img,3);
if tol_low < tol_high
ilowhigh = zeros(2,p);
for i = 1:p % 第i个图像通道
N = imhist(img(:,:,i),nbins);
cdf = cumsum(N)/sum(N); % 累积分布函数
ilow = find(cdf > tol_low, 1, 'first');
ihigh = find(cdf >= tol_high, 1, 'first');
if ilow == ihigh % 如果图像是平坦的(比如图像上灰度值都一样)
ilowhigh(:,i) = [1;nbins];
else
ilowhigh(:,i) = [ilow;ihigh];
end
end
lowhigh = (ilowhigh - 1)/(nbins-1); % convert to range [0 1]
else
% tol_low >= tol_high, this tolerance does not make sense. For example, if
% the tolerance is .5 then no pixels would be left after saturating
% low and high pixel values. In all of these cases, STRETCHLIM
% returns [0; 1]. See gecks 278249 and 235648.
lowhigh = repmat([0;1],1,p);
end
子函数理解:
(1)imhist显示图像数据的直方图
I = imread('pout.tif');
imhist(I);
figure(2);imshow(I);
也可以这样:
N = imhist(I);%N就是直方图数组
figure(3);
stem( N );
(2)cumsum累加和
cumsum(1:5)
ans =
[1 3 6 10 15]
a = [1 2 3;2 3 4;3 4 5]
a =
1 2 3
2 3 4
3 4 5
K>> cumsum(a,1)
ans =
1 2 3
3 5 7
6 9 12
K>> cumsum(a,2)
ans =
1 3 6
2 5 9
3 7 12
cdf = cumsum(N)/sum(N);
cdf(i)表示图像中像素值处于[1,i]范围的概率和。
sample:
img = imread('pout.tif');
imshow(img);
low_high = stretchlim( img );% 寻找图像对比度拉伸的上下限
g1 = imadjust( img );
figure(2);imshow(g1);
result:
2、J = imadjust(I,[low_in; high_in],[low_out; high_out])
把图像I的亮度映射到图像J,使得亮度范围[low_in ,high_in]映射到[low_out; high_out]。这4个数字必须在范围[0,1]之内。输入图像I中亮度低于low_in,或者大于high_in的,被截断了,也就是说,亮度低于low_in的,映射到low_out;亮度大于high_in的,映射到high_out。可以用空矩阵来指定这2个范围数组,表示缺省值[0,1]。
3、J = imadjust(I,[low_in; high_in],[low_out; high_out],gamma)
gamma指定了I和J之间关系曲线的形状(详见冈萨雷斯图像处理教材的图3.2)。如果gamma小于1,输出亮度大于输入亮度;如果gamma大于1,输出亮度小于输入亮度。如果忽略该参数,缺省值是1(线性映射)。
4、newmap = imadjust(map,[low_in; high_in],[low_out; high_out],gamma)
对一个与索引图像相关的颜色映射(colormap)进行变换。如果low_in, high_in, low_out, high_out, and gamma都是标量,那么对RGB通道进行相同的映射。如果想对某个单独的通道进行映射,可以这样:low_in, high_in都是1行3列的矢量,low_out, high_out都是1行3列的矢量,或者gamma是1行3列的矢量。
5、RGB2 = imadjust(RGB1,...)
对每个图像平面(通道RGB)进行亮度调节。与颜色映射(colormap)调节一样,也可以对某个颜色通道进行映射。
sample:
RGB1 = imread('football.jpg');
RGB2 = imadjust(RGB1,[.2 .3 0; .6 .7 1],[]);
imshow(RGB1), figure, imshow(RGB2)
result:
注意:如果high_out < low_out,那么输出图像被颠倒,就像相片中的负片一样。