数字图像处理(matlab编程)
matlab编程
matlab的一些语法和对图像进行简单处理的matlab编程实现
imshow函数的使用
imshow主要有三种,以下以存储图像为uint8为例,即灰度值为0-255的整型,
(1)imshow(f)默认输出在0-255的值,其中f为存储的图像的变量
(2)imshow(f,[min,max]),输出min-max的值,此函数为对比拉伸显示,指定一个min值,指定一个max值,则图像的所有灰度值进行对比度映射显示,具体规则为:小于等于min的灰度值置为0,大于等于max的值置为255,在min和max中间的灰度值f’(x,y)=(f(x,y)-min)/(max-min)*256,即原灰度值在min-max中间的相对位置,例如:min=100 max=200,原灰度值为150,映射之后的像素值为128,类似于S型函数。
(3)imshow(f,[]),自适应对比度拉伸显示,原理和(2)类似,唯一的区别就是此时图像中最小的灰度值设置为0,图像的最大的灰度值设置为255。
以上(2)(3)imshow函数第二个参数主要就是为了使过黑或者过白的图像看起来有对比度。
如:使用imshow(f)
使用imshow(f,[])
imnoise2函数的使用
在《数字图像处理》matlab版本这本书,作者使用到了imnoise2函数对图像进行一些噪声的处理,其实在Matlab中IPT里是不存在该函数的,该函数是作者自己定义的
使用语法 R=imnoise2(type,M,N,a,b)该函数是生成一个符合type类型某种分布的随机数M*N的矩阵
type有“gaussian”、“uniform”、“salt&pepper”等取值,具体查看下面m文件的注释部分,其中a和b两个参数在不同的概率密度函数中表达的含义不同,如在均值(“uniform”)均值分布中a、b表示的是产生从[a,b]的随机数,高斯函数中表示的是均值为a,标准差为b的服从高斯分布的随机数分布,具体如下图所示:
function R = imnoise2(type, M, N, a, b)
% IMNOISE2 Generates an array of random number with specified PDF.
% R = IMNOISE2(TYPE, M, N, A, B)generates an array, R, of size M
% -by-N, whose elements are random numbers of the specified TYPE with
% parameters A and B. If only TYPE is included in the input argument
% list, a single random number of the specified TYPE and defalut
% parameters shown below is generated. If only TYPE, M, and N are
% provided, the default parameters shown below are used. If M = N = 1,
% IMNOISE2 generates a single random number of the specified TYPE and
% parameters A and B.
%
% Valid values for TYPE and parameters A and B are:
% 'uniform' Uniform random numbers in the interval (A, B). The defalut
% value are (0, 1).
% 'gaussian' Gaussian random numbers with mean A and standard deviation
% B. The default values are A = 0, B = 1.
% 'salt & pepper' Salt and pepper numbers of amplitude 0 with probability
% Pa = A, and amplitude 1 with probability Pb = B. The
% default value are Pa = Pb = A = B = 0.05. Note that the
% noise has value 0 (Pa = A) and 1 (Pb = B), so scaling is
% necessary if value other than 0 and 1 are required. The
% noise matrix R is assigned three values. If R(x, y) = 0,
% the noise at (x, y) is pepper(black). If R(x, y) = 1,
% the noise at (x, y) is salt(white). If R(x, y) = 0.5,
% there is no noise assigned to coordinates (x, y).
% 'lognormal' Lognormal numbers with offset A and shape parameter B. The
% defaults are A = 1 and B = 0.25.
% 'rayleigh' Rayleigh noise with parameters A and B. The defalut values
% are A = 0 and B = =1.
% 'exponential'Exponential random numbers wirh parameter A. The default
% is A = 1.
% 'erlang' Erlang (gamma) random numbers with parameters A and B. B
% must be a positive integer. The defaults are A = 2 and B =
% 5. Erlang random numbers are approximated as the sum of B
% exponential random numbers.
% set default values.
if nargin == 1
a = 0; b = 1;
M = 1; N = 1;
elseif nargin == 3
a = 0; b = 1;
end
% Begin processing. Use lower(type) to protect against input being
% capitalized.
switch lower(type)
case 'uniform'
R = a + (b - a) * rand(M, N);
case 'gaussian'
R = a + b * randn(M, N);
case 'salt & pepper'
if nargin <= 3
a = 0.05; b = 0.05;
end
% Check to make sure that Pa + Pb is not > 1.
if (a + b) > 1
error('The sum Pa + Pb must not exceed 1.')
end
R(1:M, 1:N) = 0.5;
% Generate an M-by-N array of uniformly-distributed random nunmbers
% in the range(0, 1). Then, Pa*(M*N) of them will have values <= a.
% The coordianates of these points we call 0(peper noise).
% Similarly, Pb*(M*N) will have values in the range > a & <= (a +
% b). There we call 1(salt noise).
X = rand(M, N);
c = find(X <= a);
R(c) = 0;
u = a + b;
c = find(X > a & X <= u);
R(c) = 1;
case 'lognormal'
if nargin <= 3
a = 1; b = 0.25;
end
R = a * exp(b * randn(M, N));
case 'rayleigh'
R = a + (-b * log(1 - rand(M, N)).^0.5);
case 'exponential'
if nargin <= 3
a = 1;
end
if a <= 0
error('Parameter a must be positive for exponential type.')
end
k = -1/a;
R = k * log(1 - rand(M, N));
case 'erlang'
if nargin <= 3
a = 2; b = 5;
end
if (b ~= round(b) | b <= 0)
error('Param b must be a positive integer fo Erlang.')
end
k = -1/a;
R = zeros(M, N);
for j = 1 : b
R = R + k*log(1 - rand(M, N));
end
otherwise
error('Unknown distribution type.')
end
imnoise 函数说明
在MATLAB中,可以使用imnoise函数给一幅图像添加噪声。
g = imnoise(f, type, parameters)
参数说明:
输出:g是添加噪声之后的图像。
输入:f是原图像,type是加入的噪声类型,parameters是噪声的一些参数。
概括地说,imnoise的函数原型为:g = imnoise(f, type, parameters),注意这个只是一个概括性的函数原型,因为parameters根据噪声种类的不同可以指定多个值,也可以指定一个值。所以真正可以直接调用的函数原型是下面的这些函数接口。
具体形式的imnoise接口
下面引用自IPT里面该函数的注释,type一共可以有如下参数
% ‘gaussian’ Gaussian white noise with constant
% mean and variance
%
% ‘localvar’ Zero-mean Gaussian white noise
% with an intensity-dependent variance
%
% ‘poisson’ Poisson noise
%
% ‘salt & pepper’ “On and Off” pixels
%
% ‘speckle’ Multiplicative noise
下面解释其中常用的三种:
g=imnoise(f,‘gaussian’,m,var)是将均值为m,方差为var的高斯噪声加到图像f上。m的默认值是0,var默认值是0.01。
g=imnoise(f,‘salt & pepper’,d)给图像f添加椒盐噪声,其中d是噪声密度(即包含噪声值的图像区域的百分比)。因此,大约有d*numel(f)个像素受到污染,默认的噪声密度为0.05,表示的是0.025为椒(像素值为1)0.025为盐(像素值为0)。
g=imnoise(f,‘speckle’,var)用方程g=f + n*f将乘性噪声添加到图像f上,其中n是均值为0、方差为var的均匀分布的随机噪声。var的默认值为0.04。