5.2 imnoise函数
生活随笔
收集整理的這篇文章主要介紹了
5.2 imnoise函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
imnoise2函數
function R = imnoise2(type, M, N, a, b) %type 噪聲類型 M,N噪聲矩陣大小M x N a表示均值,b表示標準差 %IMNOISE2 Generates an array of random numbers 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 default 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 default values 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 values are Pa = % Pb = A = B = 0.05. Note that the noise has % values 0 (with probability Pa = A) and 1 (with % probability Pb = B), so scaling is necessary if % values 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 % default values are A = 0 and B = 1. % 'exponential' Exponential random numbers with 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.% Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins % Digital Image Processing Using MATLAB, Prentice-Hall, 2004 % $Revision: 1.5 $ $Date: 2003/10/12 23:37:29 $% Set default values. %若參數個數為1,則給a,b,M,N賦值 if nargin == 1a = 0; b = 1;M = 1; N = 1; elseif nargin == 3a = 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 <= 3a = 0.05; b = 0.05;end% Check to make sure that Pa + Pb is not > 1.if (a + b) > 1error('The sum Pa + Pb must not exceed 1.')endR(1:M, 1:N) = 0.5;% Generate an M-by-N array of uniformly-distributed random numbers% in the range (0, 1). Then, Pa*(M*N) of them will have values <=% a. The coordinates of these points we call 0 (pepper% noise). Similarly, Pb*(M*N) points will have values in the range% > a & <= (a + b). These 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 <= 3a = 1; b = 0.25;endR = a*exp(b*randn(M, N)); case 'rayleigh'R = a + (-b*log(1 - rand(M, N))).^0.5; case 'exponential'if nargin <= 3a = 1;endif a <= 0error('Parameter a must be positive for exponential type.')endk = -1/a;R = k*log(1 - rand(M, N)); case 'erlang'if nargin <= 3a = 2; b = 5;endif (b ~= round(b) | b <= 0)error('Param b must be a positive integer for Erlang.')endk = -1/a;R = zeros(M, N);for j = 1:b R = R + k*log(1 - rand(M, N));end otherwiseerror('Unknown distribution type.') end書中畫出各噪聲的直方圖
>> %以下顯示所有類型的直方圖,沒有賦值的取默認值 R=imnoise2('gaussian',100000,1,0,1); subplot(3,2,1),hist(R,50),title('高斯')%用hist顯示直方圖 R=imnoise2('salt & pepper',100000,10); subplot(3,2,2),hist(R,50),title('焦鹽') R=imnoise2('lognormal',100000,10); subplot(3,2,3),hist(R,50),title('對數正態') R=imnoise2('rayleigh',100000,1); subplot(3,2,4),hist(R,50),title('瑞利') R=imnoise2('exponential',100000,1); subplot(3,2,5),hist(R,50),title('指數') R=imnoise2('erlang',100000,1); subplot(3,2,6),hist(R,50),title('厄蘭')
imnoise2用來產生噪聲模型,上式gaussian代表噪聲服從高斯分布,100000,1代表產生的噪聲矩陣為100000*1大小,0代表高斯分布隨機數的均值,1代表高斯分布隨機數的標準偏差。
imnoise3
function [r, R, S] = imnoise3(M, N, C, A, B) % IMNOISE3 Generates periodic noise. % [r, R, S] = IMNOISE3(M, N, C, A, B), generates a spatial sinusoidal % noise pattern, r, of size M-by-N, its Fourier transform, R, and % spectrum, S. The remaining parameters are as follows: % C is a K-by-2 matrix containing K pairs of frequency domain coordinates % (u, v) indicating the locations of impulses in the frequency domain. % These locations are with respect to the frequency rectangele center at % (M/2 + 1, N/2 + 1). Only one paor of coordinates is required for each % impulse. The program automatically generates the locations of the % conjugate symmetric impulses. These impulse paird determine the % frequency content of r. % A is a 1-by-k vector that contains the ampitude of each of the K % impulse pairs. If A is not included in the argument, the default used % is A = ONES(1, K). B is then automatically set to its default values % (see next paragraph). The value specified for A(j) is associated with % the coordinates in C(j, 1:2). % B is a K-by-2 matrix containing the Bx and By phase components for each % impulse pair. The default values for B are B(1:K, 1:2) = 0.% Chinese : 根據5.2.3周期噪聲的DFT公式得到,A是振幅,u0,v0是關于x軸和y軸確定正弦頻率,這里是C表示 % Bx和By是關于原點的相移。 % [r,R,S] = IMNOISE3(M,N,C,A,B),產生大小為M-by-N的空間正弦噪聲模式r, % 其傅里葉變換R和頻譜S。 其余參數如下: % C是K-by-2矩陣,包含K對頻域坐標(u,v), 表示頻域中脈沖的位置。 % 這些位置相對于頻率矩形中心(M / 2 + 1,N / 2 + 1)。 每個脈沖只需要一個坐標。 % 程序自動生成共軛對稱脈沖的位置。 這些脈沖信號決定了r的頻率成分。 % A是1×k矢量,包含每個K脈沖對的幅度。 如果參數中不包含A,則使用的默認值為A = ONES(1,K)。 % 然后B自動設置為其默認值(參見下一段)。 為A(j)指定的值與C(j,1:2)中的坐標相關聯。 % B是K-by-2矩陣,包含每個脈沖對的Bx和By相位分量。 B的默認值為B(1:K,1:2)= 0。% Process input parameters. [K, n] = size(C); if nargin ==3A(1:K) = 1.0;B(1:K, 1:2) = 0; elseif nargin == 4B(1:K, 1:2) = 0; end% Generate R R = zeros(M, N); for j = 1:Ku1 = M/2 + 1 + C(j, 1);v1 = N/2 + 1 + C(j, 2);R(u1, v1) = i * (A(j)/2) * exp(i * 2 * pi * C(j, 1)* B(j,1)/M); % 位于(u + u0, v + v0) 的沖擊處的R% Complex conjugate.u2 = M/2 + 1 - C(j, 1);v2 = N/2 + 1 - C(j, 2);R(u2, v2) = -i * (A(j)/2) * exp(i * 2 * pi * C(j, 2)* B(j,2)/M);% 位于(u - u0, v - v0) 的沖擊處的R end % Compute spectrum and spatial sinusoidal pattern. S = abs(R); r = real(ifft2(ifftshift(R)));總結
以上是生活随笔為你收集整理的5.2 imnoise函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 创建AutoCAD线型
- 下一篇: Hping3 拒绝服务攻击手册