图像处理怎么学matlab,Matlab数字图像处理学习(1)-亮度变换
亮度變換函數(shù):
1、imadjust(f,[low_in high_in],[low_out
high_out],gamma)
gamma = 1時是線性映射,gamma < 1時高亮度區(qū)域被壓縮,gamma
> 1時低亮度區(qū)域被壓縮。
2、imcomplement(f)
求取圖像的反片
3、對數(shù)變換
用于傅里葉頻譜取值范圍較大,其中高值部分占優(yōu),從而導致頻譜中低亮度值可視細節(jié)丟失,通過對數(shù)變換可以將動態(tài)范圍降低,從而便于我們處理,公式為:log(1
+ double(f));
4、對比度拉伸函數(shù)
公式為:g = 1 / (1 + (m ./ (double(f) +
eps)).^E),其中m是均值,E控制斜率,一般取4;
書中將以上變換整理到一個函數(shù)中,代碼如下:
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, C
defaults to 1. Because the log is used
%?frequently to display Fourier
spectra, parameter CLASS offers the
%?option to specify the class
of the output as 'uint8' or
%?'uint16'. If parameter CLASS
is omitted, the output is of the
%?same class as the
input.
%
%?G = INTRANS(F, 'gamma', GAM)
performs a gamma transformation on
%?the input image using
parameter GAM (a required input).?%
%?G = INTRANS(F, 'stretch', M,
E) computes a contrast-stretching
%?transformation using the
expression 1./(1 + (M./(F +
%?eps)).^E).?Parameter M must be in the range [0, 1].?The
default
%?value for M is
mean2(im2double(F)), and the default value for E
%?is 4.
%
%?For the 'neg', 'gamma', and
'stretch' transformations, double
%?input images whose maximum
value is greater than 1 are scaled
%?first using
MAT2GRAY.?Other images are converted to double
first
%?using
IM2DOUBLE.?For the 'log' transformation, double
images are
%?transformed without being
scaled; other images are converted to
%?double first using
IM2DOUBLE.
%
%?The output is of the same
class as the input, except if a
%?different class is specified
for the 'log' option.
%?Copyright 2002-2004 R. C.
Gonzalez, R. E. Woods, & S. L. Eddins
%?Digital Image Processing
Using 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 the
range
% [0, 1], and the specified transformation is not 'log', convert
the
% 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 transformation
specified.?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 number
of inputs for the stretch option.')
end
g = 1./(1 + (m./(f +
eps)).^E);
otherwise
error('Unknown enhancement
method.')
end
% Convert to the class of the input image.
g = changeclass(classin, g);
5、直方圖均衡化
histeq(f,nval),其中nval為輸出圖像的灰度級數(shù),默認為64;
6、直方圖規(guī)定化
直方圖均衡化可以提高圖像的對比度,但對于圖像灰度值過于集中于0值區(qū)域時,灰度變換函數(shù)是灰度直方圖的累加和的曲線,此時曲線在低值區(qū)域會出現(xiàn)陡峰,因此把灰度級低端過于集中的像素映射到灰度級的高端,并不能提高圖像的對比度。
直方圖規(guī)定化將解決該問題,期望的直方圖應在灰度級有較小的幾種范圍,在保留圖像直方圖大體形狀,其函數(shù)為:
histeq(f,p)
p為期望直方圖,其原理和直方圖均衡化一樣,只不過直方圖均衡化的期望直方圖為等于1的直線,而直方圖規(guī)定化是一個任意的曲線而已,在實際中期望直方圖可以通過雙峰高斯函數(shù)來估計,書中提供雙峰函數(shù)的生成代碼:
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-like
function in the interval [0,1].?P is a
%?256-element vector normalized
so that SUM(P) equals 1.?The mean
%?and standard deviation of the
modes are (M1, SIG1) and (M2,
%?SIG2), respectively. A1 and
A2 are the amplitude values of the
%?two modes.?Since the output is normalized, only the relative
%?values of A1 and A2 are
important.?K is an offset value that
%?raises the "floor" of the
function.?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 Processing
Using 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 a
two-mode histogram using
%?TWOMODEGAUSS(m1, sig1, m2,
sig2, A1, A2, k).?m1 and m2 are the
%?means of the two modes and
must be in the range [0,1].?sig1 and
%?sig2 are the standard
deviations of the two modes.?A1 and A2 are
%?amplitude values, and k is an
offset value that raised the
%?"floor" of
histogram.?The number of elements in the
histogram
%?vector P is 256 and sum(P) is
normalized to 1.?MANUALHIST
%?repeatedly prompts for the
parameters and plots the resulting
%?histogram until the user
types an 'x' to quit, and then it returns
%?the last histogram
computed.
%
%?A good set of starting values
is: (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 Processing
Using 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 quits
before
% 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 to
a vector of numerical values and
% verify the number of
inputs.
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 scale
the axes. Specifying only xlim
% leaves ylim on auto.
figure, plot(p)
xlim([0 255])
end
總結
以上是生活随笔為你收集整理的图像处理怎么学matlab,Matlab数字图像处理学习(1)-亮度变换的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 龙格-库塔法(runge-kutta)m
- 下一篇: 什么才是真正的外科正牙术: