Matlab数字图像处理学习1 -亮度变换

亮度变换函数:

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};
Matlab数字图像处理学习(1)-亮度变换
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

  

爱华网本文地址 » http://www.413yy.cn/a/25101011/79688.html

更多阅读

DSP在图像处理中的应用与发展 dsp在图像处理

前言花了一天时间看了15篇文章终于搞出这么篇综述来,完全是为了3个学分,除了摘要和结论其他的基本上不是我写的.我大概了解了一下,其他人都只找了一篇文章就开始写了,真是佩服他们的勇气和胆量.我还是对得起这3个学分的.DSP在图像

《荷花淀》疑难解析 数字图像处理疑难解析

四川省绵竹中学 龚志华一、字音疑难1、晌午,读“shǎng”。2、垛起垛,读“duò”。3、打点,念“dian”。4、撅着嘴,读juē5、横样子,hèng,这是个多音字,易读错。二、句段疑难1、女人抬头笑着问:“今天怎么回来得那么晚?”—— —句平淡的问

声明:《Matlab数字图像处理学习1 -亮度变换》为网友谈场末日恋爱分享!如侵犯到您的合法权益请联系我们删除