亮度变换函数:
1、imadjust(f,[low_in high_in],[low_outhigh_out],gamma)
gamma = 1时是线性映射,gamma < 1时高亮度区域被压缩,gamma> 1时低亮度区域被压缩。
2、imcomplement(f)
求取图像的反片
3、对数变换
用于傅里叶频谱取值范围较大,其中高值部分占优,从而导致频谱中低亮度值可视细节丢失,通过对数变换可以将动态范围降低,从而便于我们处理,公式为:log(1+ double(f));
4、对比度拉伸函数
公式为:g = 1 / (1 + (m ./ (double(f) +eps)).^E),其中m是均值,E控制斜率,一般取4;
书中将以上变换整理到一个函数中,代码如下:
function g = intrans(f, varargin)
%INTRANS Performs intensity (gray-level) transformations.
% G = INTRANS(F, 'neg')computes the negative of input image F.
%
% G = INTRANS(F, 'log', C,CLASS) computes C*log(1 + F) and
% multiplies the result by(positive) constant C. If the last two
% parameters are omitted, Cdefaults to 1. Because the log is used
% frequently to display Fourierspectra, parameter CLASS offers the
% option to specify the classof the output as 'uint8' or
% 'uint16'. If parameter CLASSis omitted, the output is of the
% same class as theinput.
%
% G = INTRANS(F, 'gamma', GAM)performs a gamma transformation on
% the input image usingparameter GAM (a required input).
%
% G = INTRANS(F, 'stretch', M,E) computes a contrast-stretching
% transformation using theexpression 1./(1 + (M./(F +
% eps)).^E).Parameter M must be in the range [0, 1]. Thedefault
% value for M ismean2(im2double(F)), and the default value for E
% is 4.
%
% For the 'neg', 'gamma', and'stretch' transformations, double
% input images whose maximumvalue is greater than 1 are scaled
% first usingMAT2GRAY. Other images are converted to doublefirst
% usingIM2DOUBLE. For the 'log' transformation, doubleimages are
% transformed without beingscaled; other images are converted to
% double first usingIM2DOUBLE.
%
% The output is of the sameclass as the input, except if a
% different class is specifiedfor the 'log' option.
% Copyright 2002-2004 R. C.Gonzalez, R. E. Woods, & S. L. Eddins
% Digital Image ProcessingUsing MATLAB, Prentice-Hall, 2004
% $Revision: 1.7$ $Date: 2003/10/13 00:45:53 $
% Verify the correct number of inputs.
error(nargchk(2, 4, nargin))
% Store the class of the input for use later.
classin = class(f);
% If the input is of class double, and it is outside therange
% [0, 1], and the specified transformation is not 'log', convertthe
% input to the range [0, 1].
if strcmp(class(f), 'double') & max(f(:))> 1 & ...
~strcmp(varargin{1}, 'log')
f = mat2gray(f);
else % Convert to double, regardless of class(f).
f = im2double(f);
end
% Determine the type of transformation specified.
method = varargin{1};
% Perform the intensity transformationspecified.
switch method
case 'neg'
g = imcomplement(f);
case 'log'
if length(varargin) ==1
c = 1;
elseif length(varargin) ==2
c = varargin{2};
elseif length(varargin) ==3
c = varargin{2};
classin = varargin{3};
else
error('Incorrect number of inputs for the log option.')
end
g = c*(log(1 +double(f)));
case 'gamma'
if length(varargin)< 2
error('Not enough inputs for the gamma option.')
end
gam = varargin{2};
g = imadjust(f, [ ], [ ],gam);
case 'stretch'
if length(varargin) == 1
% Use defaults.
m = mean2(f);
E =4.0;
elseif length(varargin) ==3
m = varargin{2};
E = varargin{3};
else error('Incorrect numberof inputs for the stretch option.')
end
g = 1./(1 + (m./(f +eps)).^E);
otherwise
error('Unknown enhancementmethod.')
end
% Convert to the class of the input image.
g = changeclass(classin, g);
5、直方图均衡化
histeq(f,nval),其中nval为输出图像的灰度级数,默认为64;
6、直方图规定化
直方图均衡化可以提高图像的对比度,但对于图像灰度值过于集中于0值区域时,灰度变换函数是灰度直方图的累加和的曲线,此时曲线在低值区域会出现陡峰,因此把灰度级低端过于集中的像素映射到灰度级的高端,并不能提高图像的对比度。
直方图规定化将解决该问题,期望的直方图应在灰度级有较小的几种范围,在保留图像直方图大体形状,其函数为:
histeq(f,p)
p为期望直方图,其原理和直方图均衡化一样,只不过直方图均衡化的期望直方图为等于1的直线,而直方图规定化是一个任意的曲线而已,在实际中期望直方图可以通过双峰高斯函数来估计,书中提供双峰函数的生成代码:
function p = twomodegauss(m1, sig1, m2, sig2, A1, A2, k)
%TWOMODEGAUSS Generates a two-mode Gaussian function.
% P = TWOMODEGAUSS(M1, SIG1,M2, SIG2, A1, A2, K) generates a
% two-mode, Gaussian-likefunction in the interval [0,1]. P is a
% 256-element vector normalizedso that SUM(P) equals 1. The mean
% and standard deviation of themodes are (M1, SIG1) and (M2,
% SIG2), respectively. A1 andA2 are the amplitude values of the
% two modes.Since the output is normalized, only the relative
% values of A1 and A2 areimportant. K is an offset value that
% raises the "floor" of thefunction. A good set of values to try
% is M1=0.15, S1=0.05, M2=0.75,S2=0.05, A1=1, A2=0.07, and
% K=0.002.
% Copyright 2002-2004 R. C.Gonzalez, R. E. Woods, & S. L. Eddins
% Digital Image ProcessingUsing MATLAB, Prentice-Hall, 2004
% $Revision: 1.6$ $Date: 2003/10/13 00:54:47 $
c1 = A1 * (1 / ((2 * pi) ^ 0.5) * sig1);
k1 = 2 * (sig1 ^ 2);
c2 = A2 * (1 / ((2 * pi) ^ 0.5) * sig2);
k2 = 2 * (sig2 ^ 2);
z = linspace(0, 1, 256);
p = k + c1 * exp(-((z - m1) .^ 2) ./ k1) + ...
c2 *exp(-((z - m2) .^ 2) ./ k2);
p = p ./ sum(p(:));
function p = manualhist
%MANUALHIST Generates a two-mode histogram interactively.
% P = MANUALHIST generates atwo-mode histogram using
% TWOMODEGAUSS(m1, sig1, m2,sig2, A1, A2, k). m1 and m2 are the
% means of the two modes andmust be in the range [0,1]. sig1 and
% sig2 are the standarddeviations of the two modes. A1 and A2 are
% amplitude values, and k is anoffset value that raised the
% "floor" ofhistogram. The number of elements in thehistogram
% vector P is 256 and sum(P) isnormalized to 1. MANUALHIST
% repeatedly prompts for theparameters and plots the resulting
% histogram until the usertypes an 'x' to quit, and then it returns
% the last histogramcomputed.
%
% A good set of starting valuesis: (0.15, 0.05, 0.75, 0.05, 1,
% 0.07,0.002).
% Copyright 2002-2004 R. C.Gonzalez, R. E. Woods, & S. L. Eddins
% Digital Image ProcessingUsing MATLAB, Prentice-Hall, 2004
% $Revision: 1.7$ $Date: 2003/10/13 00:49:57 $
% Initialize.
repeats = true;
quitnow = 'x';
% Compute a default histogram in case the user quitsbefore
% estimating at least one histogram.
p = twomodegauss(0.15, 0.05, 0.75, 0.05, 1, 0.07, 0.002);
% Cycle until an x is input.
while repeats
s = input('Enter m1, sig1, m2,sig2, A1, A2, k OR x to quit:','s');
if s == quitnow
break
end
% Convert the input string toa vector of numerical values and
% verify the number ofinputs.
v = str2num(s);
if numel(v) ~= 7
disp('Incorrect number of inputs')
continue
end
p = twomodegauss(v(1), v(2),v(3), v(4), v(5), v(6), v(7));
% Start a new figure and scalethe axes. Specifying only xlim
% leaves ylim on auto.
figure, plot(p)
xlim([0 255])
end