diff options
Diffstat (limited to '3176/CH5')
-rw-r--r-- | 3176/CH5/EX5.1/Ex5_1.sce | 346 | ||||
-rw-r--r-- | 3176/CH5/EX5.11/Ex5_11.sce | 217 | ||||
-rw-r--r-- | 3176/CH5/EX5.12/Ex5_12.sce | 196 | ||||
-rw-r--r-- | 3176/CH5/EX5.3/Ex5_3.sce | 431 | ||||
-rw-r--r-- | 3176/CH5/EX5.4/Ex5_4.sce | 178 |
5 files changed, 680 insertions, 688 deletions
diff --git a/3176/CH5/EX5.1/Ex5_1.sce b/3176/CH5/EX5.1/Ex5_1.sce index bb5e70081..d20f4e6b0 100644 --- a/3176/CH5/EX5.1/Ex5_1.sce +++ b/3176/CH5/EX5.1/Ex5_1.sce @@ -1,176 +1,170 @@ -//Ex5_1
-// Noisy Images and their Histogram
-//To plot the PDF of different Noise Distribution and add the same to the gray scale image.
-//(I)Gaussian (II)Uniform (III)Salt & Pepper (IV)Log Normal (V)Rayleigh (VI)Erlang (VII)Exponetial
-// Version : Scilab 5.4.1
-// Operating System : Window-xp, Window-7
-//Toolbox: Image Processing Design 8.3.1-1
-//Toolbox: SIVP 0.5.3.1-2
-//Reference book name : Digital Image Processing
-//book author: Rafael C. Gonzalez and Richard E. Woods
-
-clc;
-close;
-clear;
-xdel(winsid())//to close all currently open figure(s).
-
-function R=imnoise2(type,M,N,a,b)
- if argn(2)==3 then
- a=0; b=1;
- end
-
- select type
-
- case'gaussian'then
- rand("normal")
- R=a+b*rand(M,N);
-
- case'uniform'then
- R=a+(b-a)*rand(M,N,"uniform");
-
- case'salt & pepper'then
- if argn(2)==3
- a = 0.15; b = 0.15;
- end
- if (a+b) > 1
- error('The sum Pa + Pb must not exceed 1.');
- end
- R(1:M,1:N) = 0.5;
- X = rand(M,N);
- [r c] = find(X<=a);
- for i=1:length(r)
- R(r(i),c(i)) = 0;
- end
- u = a + b;
- [r c] = find(X>a & X<=u);
- for i=1:length(r)
- R(r(i),c(i)) = 255;
- end
-
- case'lognormal'then
- if argn(2)==3
- a = 1; b = 0.25;
- end
- R = a*exp(b*mtlb_randn(M,N));
-
- case'rayleigh'then
- if argn(2)==3
- a = 1; b = 0.25;
- end
- R = a + ((-b)*(log(1-rand(M,N,"uniform")))).^0.5;
-
- case'exponential'then
- if argn(2)==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,"uniform"));
-
- case'erlang'then
- if (b ~= round(b) | b <= 0)
- error('Param b must be positive for integer for Erlang.')
- end
- k = -1/a;
- R = zeros(M,N);
- for j=1:b
- R = R + k*log(1-rand(M,N,"uniform"));
- end
-
- else
- disp('Unknownfiltertype.')
- end
-
-endfunction
-
-
-
-/////////////////////////////////////// Main Programm /////////////////////////////
-gray=imread("Ex5_1.tif");
-//gray=rgb2gray(a);
-//gray=im2double(gray);
-figure,ShowImage(gray,'Gray Image');
-title('Original Image');
-[M,N]=size(gray);
-[count,cell]=imhist(gray);
-figure,bar(cell,count,0.2);
-mtlb_axis([0 255 0 35000]);
-title('Histogram of Original Image');
-
-/////////////////////////////////////// Gaussian Noise ////////////////////
-r1=imnoise2('gaussian',M,N,15,5); // Generate Gaussian Noise with Given Mean and Variance
-gray_noise_gaussian=gray+(r1);
-figure,ShowImage(gray_noise_gaussian,'Gray Image with Noise');
-title('Gray Image with Noise gaussian');
-[count,cell]=imhist(gray_noise_gaussian);
-figure;bar(cell,count,1.2);
-mtlb_axis([0 255 0 3000]);
-title('Gaussian');
-
-/////////////////////////////////////// Rayleigh Noise ////////////////////
-r2=imnoise2('rayleigh',M,N,0,55); // Generate rayleigh Noise
-gray_noise_rayleigh=gray+(r2);
-figure,ShowImage(gray_noise_rayleigh,'Gray Image with Noise');
-title('Gray Image with Noise rayleigh');
-[count,cell]=imhist(gray_noise_rayleigh);
-figure;bar(cell,count,1.2);
-mtlb_axis([0 255 0 4000]);
-title('Rayleigh');
-
-/////////////////////////////////////// Erlang (Gamma) Noise ////////////////////
-r3=imnoise2('erlang',M,N,2,15); // Generate erlang Noise
-gray_noise_erlang=gray+(r3);
-figure,ShowImage(gray_noise_erlang,'Gray Image with Noise');
-title('Gray Image with Noise erlang(Gamma)');
-[count,cell]=imhist(gray_noise_erlang);
-figure;bar(cell,count,1.2);
-mtlb_axis([0 255 0 9500]);
-title('Erlang (Gamma)');
-
-/////////////////////////////////////// Exponential Noise ////////////////////
-r4=imnoise2('exponential',M,N,0.15); //Generate exponential Noise
-gray_noise_exponential=gray+(r4);
-figure,ShowImage(gray_noise_exponential,'Gray Image with Noise');
-title('Gray Image with Noise exponential');
-[count,cell]=imhist(gray_noise_exponential);
-figure;bar(cell,count,1.2);
-mtlb_axis([0 255 0 4500]);
-title('Exponential');
-
-/////////////////////////////////////// Uniform Noise ////////////////////
-r5=imnoise2('uniform',M,N,0,20); // Generate uniform Noise
-gray_noise_uniform=gray+(r5);
-figure,ShowImage(gray_noise_uniform,'Gray Image with Noise');
-title('Gray Image with Noise uniform');
-[count,cell]=imhist(gray_noise_uniform);
-figure;bar(cell,count,1.2);
-mtlb_axis([0 255 0 2000]);
-title('Uniform');
-
-/////////////////////////////////////// Salt & pepper Noise ////////////////////
-r6=imnoise2('salt & pepper',M,N,0.15,0.15); // Generate salt & pepper Noise
-gray_noise_salt_pepper=gray+(r6);
-figure,ShowImage(gray_noise_salt_pepper,'Gray Image with Noise');
-title('Gray Image with Noise salt&pepper');
-[count,cell]=imhist(gray_noise_salt_pepper);
-figure;bar(cell,count,1.2);
-mtlb_axis([0 255 0 35000]);
-title('Salt & pepper');
-
-///////////////////////////////////////// lognormal Noise ////////////////////
-//r7=imnoise2('lognormal',M,N,5,0.65); // Generate lognormal Noise
-//gray_noise_lognormal=gray+(r7);
-//figure,ShowImage(gray_noise_lognormal,'Gray Image with Noise');
-//title('Gray Image with Noise lognormal');
-//[count,cell]=imhist(gray_noise_lognormal);
-//figure;bar(cell,count,1.2);
-//mtlb_axis([0 255 0 5500]);
-//title('lognormal');
-
-
-
-
-
-
+//Ex5_1 +// Noisy Images and their Histogram +//To plot the PDF of different Noise Distribution and add the same to the gray scale image. +//(I)Gaussian (II)Uniform (III)Salt & Pepper (IV)Log Normal (V)Rayleigh (VI)Erlang (VII)Exponetial +// Version : Scilab 5.4.1 +// Operating System : Window-xp, Window-7 +//Toolbox: Image Processing Design 8.3.1-1 +//Toolbox: SIVP 0.5.3.1-2 +//Reference book name : Digital Image Processing +//book author: Rafael C. Gonzalez and Richard E. Woods + +clc; +close; +clear; +xdel(winsid())//to close all currently open figure(s). + +function R=imnoise2(type,M,N,a,b) + if argn(2)==3 + a=0; b=1; + end + + select type + + case'gaussian' + rand("normal") + R=a+b*rand(M,N); + + case'uniform' + R=a+(b-a)*rand(M,N,"uniform"); + + case'salt & pepper' + if argn(2)==3 + a = 0.15; b = 0.15; + end + if (a+b) > 1 + error('The sum Pa + Pb must not exceed 1.'); + end + R(1:M,1:N) = 0.5; + X = rand(M,N); + [r c] = find(X<=a); + for i=1:length(r) + R(r(i),c(i)) = 0; + end + u = a + b; + [r c] = find(X>a & X<=u); + for i=1:length(r) + R(r(i),c(i)) = 255; + end + + case'lognormal' + if argn(2)==3 + a = 1; b = 0.25; + end + R = a*exp(b*mtlb_randn(M,N)); + + case'rayleigh' + if argn(2)==3 + a = 1; b = 0.25; + end + R = a + ((-b)*(log(1-rand(M,N,"uniform")))).^0.5; + + case'exponential' + if argn(2)==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,"uniform")); + + case'erlang' + if (b ~= round(b) | b <= 0) + error('Param b must be positive for integer for Erlang.') + end + k = -1/a; + R = zeros(M,N); + for j=1:b + R = R + k*log(1-rand(M,N,"uniform")); + end + + else + disp('Unknownfiltertype.') + end + +endfunction + + + +/////////////////////////////////////// Main Programm ///////////////////////////// +gray=imread("Ex5_1.tif"); +//gray=rgb2gray(a); +//gray=im2double(gray); +figure,ShowImage(gray,'Gray Image'); +title('Original Image'); +[M,N]=size(gray); +[count,cell]=imhist(gray); +figure,bar(cell,count,0.2); +mtlb_axis([0 255 0 35000]); +title('Histogram of Original Image'); + +/////////////////////////////////////// Gaussian Noise //////////////////// +r1=imnoise2('gaussian',M,N,15,5); // Generate Gaussian Noise with Given Mean and Variance +gray_noise_gaussian=gray+(r1); +figure,ShowImage(gray_noise_gaussian,'Gray Image with Noise'); +title('Gray Image with Noise gaussian'); +[count,cell]=imhist(gray_noise_gaussian); +figure;bar(cell,count,1.2); +mtlb_axis([0 255 0 3000]); +title('Gaussian'); + +/////////////////////////////////////// Rayleigh Noise //////////////////// +r2=imnoise2('rayleigh',M,N,0,55); // Generate rayleigh Noise +gray_noise_rayleigh=gray+(r2); +figure,ShowImage(gray_noise_rayleigh,'Gray Image with Noise'); +title('Gray Image with Noise rayleigh'); +[count,cell]=imhist(gray_noise_rayleigh); +figure;bar(cell,count,1.2); +mtlb_axis([0 255 0 4000]); +title('Rayleigh'); + +/////////////////////////////////////// Erlang (Gamma) Noise //////////////////// +r3=imnoise2('erlang',M,N,2,15); // Generate erlang Noise +gray_noise_erlang=gray+(r3); +figure,ShowImage(gray_noise_erlang,'Gray Image with Noise'); +title('Gray Image with Noise erlang(Gamma)'); +[count,cell]=imhist(gray_noise_erlang); +figure;bar(cell,count,1.2); +mtlb_axis([0 255 0 9500]); +title('Erlang (Gamma)'); + +/////////////////////////////////////// Exponential Noise //////////////////// +r4=imnoise2('exponential',M,N,0.15); //Generate exponential Noise +gray_noise_exponential=gray+(r4); +figure,ShowImage(gray_noise_exponential,'Gray Image with Noise'); +title('Gray Image with Noise exponential'); +[count,cell]=imhist(gray_noise_exponential); +figure;bar(cell,count,1.2); +mtlb_axis([0 255 0 4500]); +title('Exponential'); + +/////////////////////////////////////// Uniform Noise //////////////////// +r5=imnoise2('uniform',M,N,0,20); // Generate uniform Noise +gray_noise_uniform=gray+(r5); +figure,ShowImage(gray_noise_uniform,'Gray Image with Noise'); +title('Gray Image with Noise uniform'); +[count,cell]=imhist(gray_noise_uniform); +figure;bar(cell,count,1.2); +mtlb_axis([0 255 0 2000]); +title('Uniform'); + +/////////////////////////////////////// Salt & pepper Noise //////////////////// +r6=imnoise2('salt & pepper',M,N,0.15,0.15); // Generate salt & pepper Noise +gray_noise_salt_pepper=gray+(r6); +figure,ShowImage(gray_noise_salt_pepper,'Gray Image with Noise'); +title('Gray Image with Noise salt&pepper'); +[count,cell]=imhist(gray_noise_salt_pepper); +figure;bar(cell,count,1.2); +mtlb_axis([0 255 0 35000]); +title('Salt & pepper'); + +///////////////////////////////////////// lognormal Noise //////////////////// +//r7=imnoise2('lognormal',M,N,5,0.65); // Generate lognormal Noise +//gray_noise_lognormal=gray+(r7); +//figure,ShowImage(gray_noise_lognormal,'Gray Image with Noise'); +//title('Gray Image with Noise lognormal'); +//[count,cell]=imhist(gray_noise_lognormal); +//figure;bar(cell,count,1.2); +//mtlb_axis([0 255 0 5500]); +//title('lognormal');
\ No newline at end of file diff --git a/3176/CH5/EX5.11/Ex5_11.sce b/3176/CH5/EX5.11/Ex5_11.sce index a6f1901cd..555076108 100644 --- a/3176/CH5/EX5.11/Ex5_11.sce +++ b/3176/CH5/EX5.11/Ex5_11.sce @@ -1,109 +1,108 @@ -//Ex5_11
-//Inverse Filtering
-// Version : Scilab 5.4.1
-// Operating System : Window-xp, Window-7
-//Toolbox: Image Processing Design 8.3.1-1
-//Toolbox: SIVP 0.5.3.1-2
-//Reference book name : Digital Image Processing
-//book author: Rafael C. Gonzalez and Richard E. Woods
-
-clc;
-close;
-clear;
-xdel(winsid())//to close all currently open figure(s).
-
-function[H,H1]=lowpassfilter(type1,M,N,D0,n,k)//lowpassfilter is used to filter an image .
- u=0:(M-1);
- v=0:(N-1);
- idx=find(u>M/2);
- u(idx)=u(idx)-M;
- idy=find(v>N/2);
- v(idy)=v(idy)-N;
- [U,V]=meshgrid(v,u);
- D=sqrt(U.^2+V.^2); //Distance Calculation
- D=fftshift(D);
- for i=1:M
- for j=1:N
- H(i,j)=exp(-k.*((i-(M/2))^2+(j-(N/2))^2).^(5/6)); //Atmospheric Degradation Function
- end
- end
-
- select type1
-
- case'inverse'then
- if argn(2)==4 then
- n=1;k=0.0025;
- end
- H=H;
- H1=H;
-
- case'butterworth'then
- if argn(2)==4 then
- n=1;
- end
-// H1 = (ones(M,N)./(1+(D./D0).^(2*n)));
- H1=double(D<=D0);
- H=H.*H1;
-
- else
- disp('Unknownfiltertype.')
- end
-endfunction
-
-/////////////////////////////////// Main Programm ////////////////////////////////
-gray=imread('Ex5_11.png');
-gray=im2double(rgb2gray(gray));
-figure,ShowImage(gray,'Gray Image');
-title('Original Image','color','blue','fontsize',4);
-[M,N]=size(gray);
-
-h=fft2(gray);//fft2() is used to find 2-Dimensional Fast Fourier Transform of an matrix
-in=fftshift(h);//fftshift() is used to rearrange the fft output, moving the zero frequency to the center of the spectrum.
-i=log(1+abs(in));
-
-inm=mat2gray(i)
-
-////////////////////////// Filtering With Cut-off Frequency 480 ///////////////////////
-[filt,H1]=lowpassfilter('inverse',M,N,480,1,0.0025); // Function which generate Filter Mask Corresponding to Low Frequency
-//filt_shift=fftshift(filt);
-//figure,ShowImage(abs(filt),'Filter Mask');
-//title('Filter Mask to Specific Cut-Off Frequency');
-n=in./(filt+%eps);//Multiply the Original Spectrum with the Filter Mask.
-Image_filter=abs(ifft(fftshift(n)));
-Image_filter=mat2gray(Image_filter)
-figure,ShowImage(Image_filter,'Filtered Image');
-title('Filtered Image with Full Inverse Filter','color','blue','fontsize',4);
-
-////////////////////////// Filtering With Cut-off Frequency 40 ///////////////////////
-[filt,H1]=lowpassfilter('butterworth',M,N,40,10,0.0025); // Function which generate Filter Mask Corresponding to Low Frequency
-//filt_shift=fftshift(filt);
-//figure,ShowImage(abs(filt),'Filter Mask');
-//title('Filter Mask to Specific Cut-Off Frequency');
-n=(in.*H1)./(filt+%eps);//Multiply the Original Spectrum with the Filter Mask.
-Image_filter=abs(ifft(fftshift(n)));
-Image_filter=mat2gray(Image_filter)
-figure,ShowImage(Image_filter,'Filtered Image');
-title('Filtered Image with Cut-Off Frequency 40','color','blue','fontsize',4);
-
-////////////////////////// Filtering With Cut-off Frequency 70 ///////////////////////
-[filt,H1]=lowpassfilter('butterworth',M,N,70,10,0.0025); // Function which generate Filter Mask Corresponding to Low Frequency
-//filt_shift=fftshift(filt);
-//figure,ShowImage(abs(filt),'Filter Mask');
-//title('Filter Mask to Specific Cut-Off Frequency');
-n=(in.*H1)./(filt+%eps);//Multiply the Original Spectrum with the Filter Mask.
-Image_filter=abs(ifft(fftshift(n)));
-Image_filter=mat2gray(Image_filter)
-figure,ShowImage(Image_filter,'Filtered Image');
-title('Filtered Image with Cut-Off Frequency 70','color','blue','fontsize',4);
-
-////////////////////////// Filtering With Cut-off Frequency 100 ///////////////////////
-[filt,H1]=lowpassfilter('butterworth',M,N,100,10,0.0025); // Function which generate Filter Mask Corresponding to Low Frequency
-//filt_shift=fftshift(filt);
-//figure,ShowImage(abs(filt),'Filter Mask');
-//title('Filter Mask to Specific Cut-Off Frequency');
-n=(in.*H1)./(filt+%eps);//Multiply the Original Spectrum with the Filter Mask.
-Image_filter=abs(ifft(fftshift(n)));
-Image_filter=mat2gray(Image_filter)
-figure,ShowImage(Image_filter,'Filtered Image');
-title('Filtered Image with Cut-Off Frequency 100','color','blue','fontsize',4);
-
+//Ex5_11 +//Inverse Filtering +// Version : Scilab 5.4.1 +// Operating System : Window-xp, Window-7 +//Toolbox: Image Processing Design 8.3.1-1 +//Toolbox: SIVP 0.5.3.1-2 +//Reference book name : Digital Image Processing +//book author: Rafael C. Gonzalez and Richard E. Woods + +clc; +close; +clear; +xdel(winsid())//to close all currently open figure(s). + +function[H,H1]=lowpassfilter(type1,M,N,D0,n,k)//lowpassfilter is used to filter an image . + u=0:(M-1); + v=0:(N-1); + idx=find(u>M/2); + u(idx)=u(idx)-M; + idy=find(v>N/2); + v(idy)=v(idy)-N; + [U,V]=meshgrid(v,u); + D=sqrt(U.^2+V.^2); //Distance Calculation + D=fftshift(D); + for i=1:M + for j=1:N + H(i,j)=exp(-k.*((i-(M/2))^2+(j-(N/2))^2).^(5/6)); //Atmospheric Degradation Function + end + end + + select type1 + + case'inverse' + if argn(2)==4 + n=1;k=0.0025; + end + H=H; + H1=H; + + case'butterworth' + if argn(2)==4 + n=1; + end +// H1 = (ones(M,N)./(1+(D./D0).^(2*n))); + H1=double(D<=D0); + H=H.*H1; + + else + disp('Unknownfiltertype.') + end +endfunction + +/////////////////////////////////// Main Programm //////////////////////////////// +gray=imread('Ex5_11.png'); +gray=im2double(rgb2gray(gray)); +figure,ShowImage(gray,'Gray Image'); +title('Original Image','color','blue','fontsize',4); +[M,N]=size(gray); + +h=fft2(gray);//fft2() is used to find 2-Dimensional Fast Fourier Transform of an matrix +in=fftshift(h);//fftshift() is used to rearrange the fft output, moving the zero frequency to the center of the spectrum. +i=log(1+abs(in)); + +inm=mat2gray(i) + +////////////////////////// Filtering With Cut-off Frequency 480 /////////////////////// +[filt,H1]=lowpassfilter('inverse',M,N,480,1,0.0025); // Function which generate Filter Mask Corresponding to Low Frequency +//filt_shift=fftshift(filt); +//figure,ShowImage(abs(filt),'Filter Mask'); +//title('Filter Mask to Specific Cut-Off Frequency'); +n=in./(filt+%eps);//Multiply the Original Spectrum with the Filter Mask. +Image_filter=abs(ifft(fftshift(n))); +Image_filter=mat2gray(Image_filter) +figure,ShowImage(Image_filter,'Filtered Image'); +title('Filtered Image with Full Inverse Filter','color','blue','fontsize',4); + +////////////////////////// Filtering With Cut-off Frequency 40 /////////////////////// +[filt,H1]=lowpassfilter('butterworth',M,N,40,10,0.0025); // Function which generate Filter Mask Corresponding to Low Frequency +//filt_shift=fftshift(filt); +//figure,ShowImage(abs(filt),'Filter Mask'); +//title('Filter Mask to Specific Cut-Off Frequency'); +n=(in.*H1)./(filt+%eps);//Multiply the Original Spectrum with the Filter Mask. +Image_filter=abs(ifft(fftshift(n))); +Image_filter=mat2gray(Image_filter) +figure,ShowImage(Image_filter,'Filtered Image'); +title('Filtered Image with Cut-Off Frequency 40','color','blue','fontsize',4); + +////////////////////////// Filtering With Cut-off Frequency 70 /////////////////////// +[filt,H1]=lowpassfilter('butterworth',M,N,70,10,0.0025); // Function which generate Filter Mask Corresponding to Low Frequency +//filt_shift=fftshift(filt); +//figure,ShowImage(abs(filt),'Filter Mask'); +//title('Filter Mask to Specific Cut-Off Frequency'); +n=(in.*H1)./(filt+%eps);//Multiply the Original Spectrum with the Filter Mask. +Image_filter=abs(ifft(fftshift(n))); +Image_filter=mat2gray(Image_filter) +figure,ShowImage(Image_filter,'Filtered Image'); +title('Filtered Image with Cut-Off Frequency 70','color','blue','fontsize',4); + +////////////////////////// Filtering With Cut-off Frequency 100 /////////////////////// +[filt,H1]=lowpassfilter('butterworth',M,N,100,10,0.0025); // Function which generate Filter Mask Corresponding to Low Frequency +//filt_shift=fftshift(filt); +//figure,ShowImage(abs(filt),'Filter Mask'); +//title('Filter Mask to Specific Cut-Off Frequency'); +n=(in.*H1)./(filt+%eps);//Multiply the Original Spectrum with the Filter Mask. +Image_filter=abs(ifft(fftshift(n))); +Image_filter=mat2gray(Image_filter) +figure,ShowImage(Image_filter,'Filtered Image'); +title('Filtered Image with Cut-Off Frequency 100','color','blue','fontsize',4);
\ No newline at end of file diff --git a/3176/CH5/EX5.12/Ex5_12.sce b/3176/CH5/EX5.12/Ex5_12.sce index 0ef07a070..c702d5f66 100644 --- a/3176/CH5/EX5.12/Ex5_12.sce +++ b/3176/CH5/EX5.12/Ex5_12.sce @@ -1,98 +1,98 @@ -//Ex5_12
-//Comparision of Inverse Filtering and Wiener Filtering
-// Version : Scilab 5.4.1
-// Operating System : Window-xp, Window-7
-//Toolbox: Image Processing Design 8.3.1-1
-//Toolbox: SIVP 0.5.3.1-2
-//Reference book name : Digital Image Processing
-//book author: Rafael C. Gonzalez and Richard E. Woods
-
-clc;
-close;
-clear;
-xdel(winsid())//to close all currently open figure(s).
-
-function[H,H1]=lowpassfilter(type1,M,N,D0,n,k)//lowpassfilter is used to filter an image .
- u=0:(M-1);
- v=0:(N-1);
- idx=find(u>M/2);
- u(idx)=u(idx)-M;
- idy=find(v>N/2);
- v(idy)=v(idy)-N;
- [U,V]=meshgrid(v,u);
- D=sqrt(U.^2+V.^2); //Distance Calculation
- D=fftshift(D);
- for i=1:M
- for j=1:N
- H(i,j)=exp(-k.*((i-(M/2))^2+(j-(N/2))^2).^(5/6)); //Atmospheric Degradation Function
- end
- end
-
- select type1
-
- case'inverse'then
- if argn(2)==4 then
- n=1;k=0.0025;
- end
- H=H;
- H1=H;
-
- case'butterworth'then
- if argn(2)==4 then
- n=1;
- end
-// H1 = (ones(M,N)./(1+(D./D0).^(2*n)));
- H1=double(D<=D0);
- H=H.*H1;
-
- else
- disp('Unknownfiltertype.')
- end
-endfunction
-
-/////////////////////////////////// Main Programm ////////////////////////////////
-gray=imread('Ex5_12.png');
-gray=im2double(rgb2gray(gray));
-figure,ShowImage(gray,'Gray Image');
-title('Original Image','color','blue','fontsize',4);
-[M,N]=size(gray);
-
-h=fft2(gray);//fft2() is used to find 2-Dimensional Fast Fourier Transform of an matrix
-in=fftshift(h);//fftshift() is used to rearrange the fft output, moving the zero frequency to the center of the spectrum.
-i=log(1+abs(in));
-
-inm=mat2gray(i)
-
-////////////////////////// Filtering With Cut-off Frequency 480 ///////////////////////
-[filt,H1]=lowpassfilter('inverse',M,N,480,1,0.0025); // Function which generate Filter Mask Corresponding to Low Frequency
-//filt_shift=fftshift(filt);
-//figure,ShowImage(abs(filt),'Filter Mask');
-//title('Filter Mask to Specific Cut-Off Frequency');
-n=in./(filt+%eps);//Multiply the Original Spectrum with the Filter Mask.
-Image_filter=abs(ifft(fftshift(n)));
-Image_filter=mat2gray(Image_filter)
-figure,ShowImage(Image_filter,'Filtered Image');
-title('Filtered Image with Full Inverse Filter','color','blue','fontsize',4);
-
-////////////////////////// Filtering With Cut-off Frequency 40 ///////////////////////
-[filt,H1]=lowpassfilter('butterworth',M,N,40,10,0.0025); // Function which generate Filter Mask Corresponding to Low Frequency
-//filt_shift=fftshift(filt);
-//figure,ShowImage(abs(filt),'Filter Mask');
-//title('Filter Mask to Specific Cut-Off Frequency');
-n=(in.*H1)./(filt+%eps);//Multiply the Original Spectrum with the Filter Mask.
-Image_filter=abs(ifft(fftshift(n)));
-Image_filter=mat2gray(Image_filter)
-figure,ShowImage(Image_filter,'Filtered Image');
-title('Filtered Image with Cut-Off Frequency 40','color','blue','fontsize',4);
-
-
-////////////////////////// Filtering With Cut-off Frequency 40 ///////////////////////
-[filt,H1]=lowpassfilter('butterworth',M,N,40,10,0.0025); // Function which generate Filter Mask Corresponding to Low Frequency
-//filt_shift=fftshift(filt);
-//figure,ShowImage(abs(filt),'Filter Mask');
-//title('Filter Mask to Specific Cut-Off Frequency');
-n=(in.*H1)./(((1/filt+%eps).*(filt^2/(filt^2+6)))+%eps); // Wiener Filtering.
-Image_filter=abs(ifft(fftshift(n)));
-Image_filter=mat2gray(Image_filter)
-figure,ShowImage(Image_filter,'Filtered Image');
-title('Filtered Image with Cut-Off Frequency 40','color','blue','fontsize',4);
+//Ex5_12 +//Comparision of Inverse Filtering and Wiener Filtering +// Version : Scilab 5.4.1 +// Operating System : Window-xp, Window-7 +//Toolbox: Image Processing Design 8.3.1-1 +//Toolbox: SIVP 0.5.3.1-2 +//Reference book name : Digital Image Processing +//book author: Rafael C. Gonzalez and Richard E. Woods + +clc; +close; +clear; +xdel(winsid())//to close all currently open figure(s). + +function[H,H1]=lowpassfilter(type1,M,N,D0,n,k)//lowpassfilter is used to filter an image . + u=0:(M-1); + v=0:(N-1); + idx=find(u>M/2); + u(idx)=u(idx)-M; + idy=find(v>N/2); + v(idy)=v(idy)-N; + [U,V]=meshgrid(v,u); + D=sqrt(U.^2+V.^2); //Distance Calculation + D=fftshift(D); + for i=1:M + for j=1:N + H(i,j)=exp(-k.*((i-(M/2))^2+(j-(N/2))^2).^(5/6)); //Atmospheric Degradation Function + end + end + + select type1 + + case'inverse' + if argn(2)==4 + n=1;k=0.0025; + end + H=H; + H1=H; + + case'butterworth' + if argn(2)==4 + n=1; + end +// H1 = (ones(M,N)./(1+(D./D0).^(2*n))); + H1=double(D<=D0); + H=H.*H1; + + else + disp('Unknownfiltertype.') + end +endfunction + +/////////////////////////////////// Main Programm //////////////////////////////// +gray=imread('Ex5_12.png'); +gray=im2double(rgb2gray(gray)); +figure,ShowImage(gray,'Gray Image'); +title('Original Image','color','blue','fontsize',4); +[M,N]=size(gray); + +h=fft2(gray);//fft2() is used to find 2-Dimensional Fast Fourier Transform of an matrix +in=fftshift(h);//fftshift() is used to rearrange the fft output, moving the zero frequency to the center of the spectrum. +i=log(1+abs(in)); + +inm=mat2gray(i) + +////////////////////////// Filtering With Cut-off Frequency 480 /////////////////////// +[filt,H1]=lowpassfilter('inverse',M,N,480,1,0.0025); // Function which generate Filter Mask Corresponding to Low Frequency +//filt_shift=fftshift(filt); +//figure,ShowImage(abs(filt),'Filter Mask'); +//title('Filter Mask to Specific Cut-Off Frequency'); +n=in./(filt+%eps);//Multiply the Original Spectrum with the Filter Mask. +Image_filter=abs(ifft(fftshift(n))); +Image_filter=mat2gray(Image_filter) +figure,ShowImage(Image_filter,'Filtered Image'); +title('Filtered Image with Full Inverse Filter','color','blue','fontsize',4); + +////////////////////////// Filtering With Cut-off Frequency 40 /////////////////////// +[filt,H1]=lowpassfilter('butterworth',M,N,40,10,0.0025); // Function which generate Filter Mask Corresponding to Low Frequency +//filt_shift=fftshift(filt); +//figure,ShowImage(abs(filt),'Filter Mask'); +//title('Filter Mask to Specific Cut-Off Frequency'); +n=(in.*H1)./(filt+%eps);//Multiply the Original Spectrum with the Filter Mask. +Image_filter=abs(ifft(fftshift(n))); +Image_filter=mat2gray(Image_filter) +figure,ShowImage(Image_filter,'Filtered Image'); +title('Filtered Image with Cut-Off Frequency 40','color','blue','fontsize',4); + + +////////////////////////// Filtering With Cut-off Frequency 40 /////////////////////// +[filt,H1]=lowpassfilter('butterworth',M,N,40,10,0.0025); // Function which generate Filter Mask Corresponding to Low Frequency +//filt_shift=fftshift(filt); +//figure,ShowImage(abs(filt),'Filter Mask'); +//title('Filter Mask to Specific Cut-Off Frequency'); +n=(in.*H1)./(((1/filt+%eps).*(filt^2/(filt^2+6)))+%eps); // Wiener Filtering. +Image_filter=abs(ifft(fftshift(n))); +Image_filter=mat2gray(Image_filter) +figure,ShowImage(Image_filter,'Filtered Image'); +title('Filtered Image with Cut-Off Frequency 40','color','blue','fontsize',4);
\ No newline at end of file diff --git a/3176/CH5/EX5.3/Ex5_3.sce b/3176/CH5/EX5.3/Ex5_3.sce index 6d8c27f00..eb734546f 100644 --- a/3176/CH5/EX5.3/Ex5_3.sce +++ b/3176/CH5/EX5.3/Ex5_3.sce @@ -1,216 +1,215 @@ -//Ex5_3
-// Illustration of Order Statistic filter
-//To impliment the Following Order Statistic Restoration filter
-// (I)Median (II)MAX (III)MIN (IV)Mid Point (V)Alpha trimmed.
-
-// Version : Scilab 5.4.1
-// Operating System : Window-xp, Window-7
-//Toolbox: Image Processing Design 8.3.1-1
-//Toolbox: SIVP 0.5.3.1-2
-//Reference book name : Digital Image Processing
-//book author: Rafael C. Gonzalez and Richard E. Woods
-
-clc;
-close;
-clear;
-xdel(winsid())//to close all currently open figure(s).
-
-function [f]=arithmetic_mean(v,m,n)
- w=fspecial('average',m);
- f=imfilter(v,w);
-endfunction
-
-function [f]=geometric_mean1(g,m,n);//gmean1() is used to filter an image using Geometric mean filter
- size1=m;
- q=m*n;
- g=double(g);
- [nr,nc]=size(g);
- temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2));
- temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=g(1:$,1:$)
- temp=temp+1;
- for i=ceil(size1/2):nr+ceil(size1/2)-1
- for j=ceil(size1/2):nc+ceil(size1/2)-1
- t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2)) ;
- temp2(i,j)=prod(t);
- end
- end
- temp3=temp2.^(1/q);
- nn=temp3(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)
- f1=nn-1;
- f=mat2gray(f1)
-endfunction
-
-function [f]=restoration_filter(v,type,m,n,Q,d)
- if argn(2) ==2 then
- m=7;n=7;Q=1.5;d=10;
- elseif argn(2)==5 then
- Q=parameter;d=parameter;
- elseif argn(2)==4 then
- Q=1.5;d=2;
- else
- disp('wrong number of inputs');
- end
-
- select type
-
- case'median'then
- f=MedianFilter(v,[m n]);
-
- case'MIN'then
- size1=m;
- [nr,nc]=size(v);
- temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2));
- temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=v(1:$,1:$);
- for i=ceil(size1/2):nr+ceil(size1/2)-1
- for j=ceil(size1/2):nc+ceil(size1/2)-1
- t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2)) ;
- y=gsort(t);
- temp2(i-floor(size1/2),j-floor(size1/2))=min(y);
- end
- end
- f=mat2gray(temp2);
-
- case'MAX'then
- size1=m;
- [nr,nc]=size(v);
- temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2));
- temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=v(1:$,1:$);
- for i=ceil(size1/2):nr+ceil(size1/2)-1
- for j=ceil(size1/2):nc+ceil(size1/2)-1
- t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2)) ;
- y=gsort(t);
- temp2(i-floor(size1/2),j-floor(size1/2))=max(y);
- end
- end
- f=mat2gray(temp2);
-
- case'Mid_Point'then
- size1=m;
- [nr,nc]=size(v);
- temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2));
- temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=v(1:$,1:$);
- for i=ceil(size1/2):nr+ceil(size1/2)-1
- for j=ceil(size1/2):nc+ceil(size1/2)-1
- t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2)) ;
- y=gsort(t);
- temp2(i-floor(size1/2),j-floor(size1/2))=0.5*(min(y)+max(y));
- end
- end
- f=mat2gray(temp2);
-
- else
- disp('Unknownfiltertype.')
- end
-endfunction
-
-function [f]=alphatrim(g,m,n,d)//alphatrim()is used to filter an image using alpha-trimmed mean filter
- size1=m;
- [nr,nc]=size(g);
- temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2));
- temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=g(1:$,1:$)
-
- for i=ceil(size1/2):nr+ceil(size1/2)-1
- for j=ceil(size1/2):nc+ceil(size1/2)-1
- t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2))
- y=gsort(t);
- a=y(:)
- b=a';
- t1=b(1+d/2:$-d/2);
- temp2(i-floor(size1/2),j-floor(size1/2))=mean(t1);
- end
- end
- f=mat2gray(temp2)
-endfunction
-
-
-///////////////////////////////////// Main Programm ////////////////////
-
-gray=imread("Ex5_3.tif");
-//gray=rgb2gray(a);
-//gray=im2double(gray);
-figure,ShowImage(gray,'Gray Image');
-title('Original Image');
-[M,N]=size(gray);
-
-/////////////////////////////////// Median Filter ////////////////////
-v=imnoise(gray,'salt & pepper',0.1);
-figure,ShowImage(v,'Noisy Image');
-title('Original Image with Salt & Pepper Noise');
-//Filtering the corrupted image with median filter
-h=restoration_filter(v,'median',3,3);
-figure,ShowImage(h,'Recovered Image');
-title('Recovered Image with Median Filter');
-//Filtering the corrupted image with median filter
-h1=restoration_filter(h,'median',3,3);
-figure,ShowImage(h1,'Recovered Image');
-title('Recovered Image with Median Filter');
-//Filtering the corrupted image with median filter
-h2=restoration_filter(h1,'median',3,3);
-figure,ShowImage(h2,'Recovered Image');
-title('Recovered Image with Median Filter');
-
-
-/////////////////////////////////// MAX Filter ////////////////////
-temp(1:M,1:N)=0.5;
-r3=imnoise(temp,'salt & pepper',0.1); // Generate salt & pepper Noise
-gray_noise_pepper=gray; // Add Pepper Noise Only
-[r c]=find(r3==0);
- for i=1:length(r)
- gray_noise_pepper(r(i),c(i)) = 0;
- end
-figure,ShowImage(gray_noise_pepper,'Noisy Image');
-title('Noisy Image with Pepper Noise');
-
-//Filtering the Salt Noise corrupted image with MAX filter
-h=restoration_filter(gray_noise_pepper,'MAX',3,3);
-figure,ShowImage(h,'Recovered Image');
-title('Recovered Image with MAX Filter');
-
-
-//////////////////////////////////// MIN Filter ////////////////////
-temp(1:M,1:N)=0.5;
-r3=imnoise(temp,'salt & pepper',0.1); // Generate salt & pepper Noise
-gray_noise_salt=gray; // Add salt Noise Only
-[r c]=find(r3==1);
- for i=1:length(r)
- gray_noise_salt(r(i),c(i)) = 255;
- end
-figure,ShowImage(gray_noise_salt,'Noisy Image');
-title('Noisy Image');
-
-//Filtering the Salt Noise corrupted image with MIN filter
-h=restoration_filter(gray_noise_salt,'MIN',3,3);
-figure,ShowImage(h,'Recovered Image');
-title('Recovered Image with MIN Filter');
-
-
-///////////////////////////////////// Mid-Point Filter ////////////////////
-//v=imnoise(gray,'gaussian',0,0.02);
-//figure,ShowImage(v,'Noisy Image');
-//title('Image with Gaussian Noise');
-////Filtering the Salt Noise corrupted image with Mid-Point filter
-//h=restoration_filter(v,'Mid_Point',3,3);
-//figure,ShowImage(h,'Recovered Image');
-//title('Recovered Image with Mid_Point Filter');
-
-
-///////////////////////////////// Alpha Trimmed Filter ////////////////////
-v=imnoise(gray,'gaussian',0,0.02);
-v=imnoise(v,'salt & pepper',0.05);
-figure,ShowImage(v,'Noisy Image');
-title('Image with Gaussian and Salt&Pepper Noise');
-m=5;n=5;d=5;
-[f]=arithmetic_mean(v,m,n); // Filtering with Arithmetical mean
-figure,ShowImage(f,'Recovered Image');
-title('Recovered Image with Arithmetical Mean Filter');
-[f]=geometric_mean1(v,m,n); // Filtering with Geometric mean
-figure,ShowImage(f,'Recovered Image');
-title('Recovered Image with Geometric Mean Filter');
-//Filtering the corrupted image with median filter
-h=restoration_filter(v,'median',5,5); // Filtering with median Filtering
-figure,ShowImage(h,'Recovered Image');
-title('Recovered Image with Median Filter');
-f=alphatrim(v,m,n,d); // Filtering with alphatrim Filtering
-figure,ShowImage(f,'Recovered Image');
-title('Recovered Image with Alpha Trimmed Filter');
-
+//Ex5_3 +// Illustration of Order Statistic filter +//To impliment the Following Order Statistic Restoration filter +// (I)Median (II)MAX (III)MIN (IV)Mid Point (V)Alpha trimmed. + +// Version : Scilab 5.4.1 +// Operating System : Window-xp, Window-7 +//Toolbox: Image Processing Design 8.3.1-1 +//Toolbox: SIVP 0.5.3.1-2 +//Reference book name : Digital Image Processing +//book author: Rafael C. Gonzalez and Richard E. Woods + +clc; +close; +clear; +xdel(winsid())//to close all currently open figure(s). + +function [f]=arithmetic_mean(v,m,n) + w=fspecial('average',m); + f=imfilter(v,w); +endfunction + +function [f]=geometric_mean1(g,m,n);//gmean1() is used to filter an image using Geometric mean filter + size1=m; + q=m*n; + g=double(g); + [nr,nc]=size(g); + temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2)); + temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=g(1:$,1:$) + temp=temp+1; + for i=ceil(size1/2):nr+ceil(size1/2)-1 + for j=ceil(size1/2):nc+ceil(size1/2)-1 + t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2)) ; + temp2(i,j)=prod(t); + end + end + temp3=temp2.^(1/q); + nn=temp3(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1) + f1=nn-1; + f=mat2gray(f1) +endfunction + +function [f]=restoration_filter(v,type,m,n,Q,d) + if argn(2) ==2 + m=7;n=7;Q=1.5;d=10; + elseif argn(2)==5 + Q=parameter;d=parameter; + elseif argn(2)==4 + Q=1.5;d=2; + else + disp('wrong number of inputs'); + end + + select type + + case'median' + f=MedianFilter(v,[m n]); + + case'MIN' + size1=m; + [nr,nc]=size(v); + temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2)); + temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=v(1:$,1:$); + for i=ceil(size1/2):nr+ceil(size1/2)-1 + for j=ceil(size1/2):nc+ceil(size1/2)-1 + t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2)) ; + y=gsort(t); + temp2(i-floor(size1/2),j-floor(size1/2))=min(y); + end + end + f=mat2gray(temp2); + + case'MAX' + size1=m; + [nr,nc]=size(v); + temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2)); + temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=v(1:$,1:$); + for i=ceil(size1/2):nr+ceil(size1/2)-1 + for j=ceil(size1/2):nc+ceil(size1/2)-1 + t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2)) ; + y=gsort(t); + temp2(i-floor(size1/2),j-floor(size1/2))=max(y); + end + end + f=mat2gray(temp2); + + case'Mid_Point' + size1=m; + [nr,nc]=size(v); + temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2)); + temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=v(1:$,1:$); + for i=ceil(size1/2):nr+ceil(size1/2)-1 + for j=ceil(size1/2):nc+ceil(size1/2)-1 + t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2)) ; + y=gsort(t); + temp2(i-floor(size1/2),j-floor(size1/2))=0.5*(min(y)+max(y)); + end + end + f=mat2gray(temp2); + + else + disp('Unknownfiltertype.') + end +endfunction + +function [f]=alphatrim(g,m,n,d)//alphatrim()is used to filter an image using alpha-trimmed mean filter + size1=m; + [nr,nc]=size(g); + temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2)); + temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=g(1:$,1:$) + + for i=ceil(size1/2):nr+ceil(size1/2)-1 + for j=ceil(size1/2):nc+ceil(size1/2)-1 + t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2)) + y=gsort(t); + a=y(:) + b=a'; + t1=b(1+d/2:$-d/2); + temp2(i-floor(size1/2),j-floor(size1/2))=mean(t1); + end + end + f=mat2gray(temp2) +endfunction + + +///////////////////////////////////// Main Programm //////////////////// + +gray=imread("Ex5_3.tif"); +//gray=rgb2gray(a); +//gray=im2double(gray); +figure,ShowImage(gray,'Gray Image'); +title('Original Image'); +[M,N]=size(gray); + +/////////////////////////////////// Median Filter //////////////////// +v=imnoise(gray,'salt & pepper',0.1); +figure,ShowImage(v,'Noisy Image'); +title('Original Image with Salt & Pepper Noise'); +//Filtering the corrupted image with median filter +h=restoration_filter(v,'median',3,3); +figure,ShowImage(h,'Recovered Image'); +title('Recovered Image with Median Filter'); +//Filtering the corrupted image with median filter +h1=restoration_filter(h,'median',3,3); +figure,ShowImage(h1,'Recovered Image'); +title('Recovered Image with Median Filter'); +//Filtering the corrupted image with median filter +h2=restoration_filter(h1,'median',3,3); +figure,ShowImage(h2,'Recovered Image'); +title('Recovered Image with Median Filter'); + + +/////////////////////////////////// MAX Filter //////////////////// +temp(1:M,1:N)=0.5; +r3=imnoise(temp,'salt & pepper',0.1); // Generate salt & pepper Noise +gray_noise_pepper=gray; // Add Pepper Noise Only +[r c]=find(r3==0); + for i=1:length(r) + gray_noise_pepper(r(i),c(i)) = 0; + end +figure,ShowImage(gray_noise_pepper,'Noisy Image'); +title('Noisy Image with Pepper Noise'); + +//Filtering the Salt Noise corrupted image with MAX filter +h=restoration_filter(gray_noise_pepper,'MAX',3,3); +figure,ShowImage(h,'Recovered Image'); +title('Recovered Image with MAX Filter'); + + +//////////////////////////////////// MIN Filter //////////////////// +temp(1:M,1:N)=0.5; +r3=imnoise(temp,'salt & pepper',0.1); // Generate salt & pepper Noise +gray_noise_salt=gray; // Add salt Noise Only +[r c]=find(r3==1); + for i=1:length(r) + gray_noise_salt(r(i),c(i)) = 255; + end +figure,ShowImage(gray_noise_salt,'Noisy Image'); +title('Noisy Image'); + +//Filtering the Salt Noise corrupted image with MIN filter +h=restoration_filter(gray_noise_salt,'MIN',3,3); +figure,ShowImage(h,'Recovered Image'); +title('Recovered Image with MIN Filter'); + + +///////////////////////////////////// Mid-Point Filter //////////////////// +//v=imnoise(gray,'gaussian',0,0.02); +//figure,ShowImage(v,'Noisy Image'); +//title('Image with Gaussian Noise'); +////Filtering the Salt Noise corrupted image with Mid-Point filter +//h=restoration_filter(v,'Mid_Point',3,3); +//figure,ShowImage(h,'Recovered Image'); +//title('Recovered Image with Mid_Point Filter'); + + +///////////////////////////////// Alpha Trimmed Filter //////////////////// +v=imnoise(gray,'gaussian',0,0.02); +v=imnoise(v,'salt & pepper',0.05); +figure,ShowImage(v,'Noisy Image'); +title('Image with Gaussian and Salt&Pepper Noise'); +m=5;n=5;d=5; +[f]=arithmetic_mean(v,m,n); // Filtering with Arithmetical mean +figure,ShowImage(f,'Recovered Image'); +title('Recovered Image with Arithmetical Mean Filter'); +[f]=geometric_mean1(v,m,n); // Filtering with Geometric mean +figure,ShowImage(f,'Recovered Image'); +title('Recovered Image with Geometric Mean Filter'); +//Filtering the corrupted image with median filter +h=restoration_filter(v,'median',5,5); // Filtering with median Filtering +figure,ShowImage(h,'Recovered Image'); +title('Recovered Image with Median Filter'); +f=alphatrim(v,m,n,d); // Filtering with alphatrim Filtering +figure,ShowImage(f,'Recovered Image'); +title('Recovered Image with Alpha Trimmed Filter');
\ No newline at end of file diff --git a/3176/CH5/EX5.4/Ex5_4.sce b/3176/CH5/EX5.4/Ex5_4.sce index ca1fde8d4..c2aff7996 100644 --- a/3176/CH5/EX5.4/Ex5_4.sce +++ b/3176/CH5/EX5.4/Ex5_4.sce @@ -1,89 +1,89 @@ -//Ex5_4
-//Illustration of Adaptive Local Noise Reduction Filtering
-// Version : Scilab 5.4.1
-// Operating System : Window-xp, Window-7
-//Toolbox: Image Processing Design 8.3.1-1
-//Toolbox: SIVP 0.5.3.1-2
-//Reference book name : Digital Image Processing
-//book author: Rafael C. Gonzalez and Richard E. Woods
-
-clc;
-clear;
-close;
-xdel(winsid());
-
-///////////////// Function File /////////////////////
-function [f]=arithmetic_mean(v,m,n)
- w=fspecial('average',m);
- f=imfilter(v,w);
-endfunction
-
-function [f]=geometric_mean1(g,m,n);//gmean1() is used to filter an image using Geometric mean filter
- size1=m;
- q=m*n;
- g=double(g);
- [nr,nc]=size(g);
- temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2));
- temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=g(1:$,1:$)
- temp=temp+1;
- for i=ceil(size1/2):nr+ceil(size1/2)-1
- for j=ceil(size1/2):nc+ceil(size1/2)-1
- t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2)) ;
- temp2(i,j)=prod(t);
- end
- end
- temp3=temp2.^(1/q);
- nn=temp3(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)
- f1=nn-1;
- f=mat2gray(f1)
-endfunction
-
-
-///////////////// Main Programm /////////////////////
-A=imread("Ex5_4.tif");
-B = imnoise(A,'gaussian',0,0.01);
-[rw1 ,cl1]=size(B);
-figure;
-ShowImage(B,'Gaussian noise added');
-title('Image with gaussian noise','color','blue','fontsize',4);
-
-///////////////////////////////////// Arithmetical Mean Filter ////////////////////
-m=7;n=7;
-[f]=arithmetic_mean(B,m,n);
-figure,ShowImage(f,'Recovered Image');
-title('Restored Image with Arithmetical Mean Filter','color','blue','fontsize',4);
-
-///////////////////////////////////// Geometric Mean Filter ////////////////////
-m=7;n=7;
-[f]=geometric_mean1(B,m,n);
-figure,ShowImage(f,'Recovered Image');
-title('Restored Image with Geometric Mean Filter','color','blue','fontsize',4);
-
-
-
-////////////////////Adaptive Local Noise Reduction///////////////////////
-B= double(B);
-M=7;
-N=7;
-lvar=zeros([rw1-M+1,cl1-N+1]);
-lmean=zeros([rw1-M+1,cl1-N+1]);
-temp=zeros([rw1-M+1,cl1-N+1]);
-F=zeros([rw1-M+1,cl1-N+1]);
-sz=(rw1-M+1)*(cl1-N+1);
-for i=1:rw1-M+1
- for j=1:cl1-N+1
- temp=B(i:i+(M-1),j:j+(N-1));
- lmean(i,j)=mean(temp);
- lvar(i,j)=mean(temp.*temp)-mean(temp).^2;
- end
-end
-nvar=sum(lvar)/sz;
-lvar=max(lvar,nvar);
-C=B(M/2:rw1-M/2,N/2:cl1-N/2);
-F=nvar./lvar;
-F=F.*(C-lmean);
-F=C-F;
-F=uint8(F);
-figure;
-ShowImage(F,'Restored');
-title('Restored Image using Adaptive Local filter','color','blue','fontsize',4);
+//Ex5_4 +//Illustration of Adaptive Local Noise Reduction Filtering +// Version : Scilab 5.4.1 +// Operating System : Window-xp, Window-7 +//Toolbox: Image Processing Design 8.3.1-1 +//Toolbox: SIVP 0.5.3.1-2 +//Reference book name : Digital Image Processing +//book author: Rafael C. Gonzalez and Richard E. Woods + +clc; +clear; +close; +xdel(winsid()); + +///////////////// Function File ///////////////////// +function [f]=arithmetic_mean(v,m,n) + w=fspecial('average',m); + f=imfilter(v,w); +endfunction + +function [f]=geometric_mean1(g,m,n);//gmean1() is used to filter an image using Geometric mean filter + size1=m; + q=m*n; + g=double(g); + [nr,nc]=size(g); + temp=zeros(nr+2*floor(size1/2),nc+2*floor(size1/2)); + temp(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1)=g(1:$,1:$) + temp=temp+1; + for i=ceil(size1/2):nr+ceil(size1/2)-1 + for j=ceil(size1/2):nc+ceil(size1/2)-1 + t=temp(i-floor(size1/2):1:i+floor(size1/2),j-floor(size1/2):1:j+floor(size1/2)) ; + temp2(i,j)=prod(t); + end + end + temp3=temp2.^(1/q); + nn=temp3(ceil(size1/2):nr+ceil(size1/2)-1,ceil(size1/2):nc+ceil(size1/2)-1) + f1=nn-1; + f=mat2gray(f1) +endfunction + + +///////////////// Main Programm ///////////////////// +A=imread("Ex5_4.tif"); +B = imnoise(A,'gaussian',0,0.01); +[rw1 ,cl1]=size(B); +figure; +ShowImage(B,'Gaussian noise added'); +title('Image with gaussian noise','color','blue','fontsize',4); + +///////////////////////////////////// Arithmetical Mean Filter //////////////////// +m=7;n=7; +[f]=arithmetic_mean(B,m,n); +figure,ShowImage(f,'Recovered Image'); +title('Restored Image with Arithmetical Mean Filter','color','blue','fontsize',4); + +///////////////////////////////////// Geometric Mean Filter //////////////////// +m=7;n=7; +[f]=geometric_mean1(B,m,n); +figure,ShowImage(f,'Recovered Image'); +title('Restored Image with Geometric Mean Filter','color','blue','fontsize',4); + + + +////////////////////Adaptive Local Noise Reduction/////////////////////// +B= double(B); +M=7; +N=7; +lvar=zeros([rw1-M+1,cl1-N+1]); +lmean=zeros([rw1-M+1,cl1-N+1]); +temp=zeros([rw1-M+1,cl1-N+1]); +F=zeros([rw1-M+1,cl1-N+1]); +sz=(rw1-M+1)*(cl1-N+1); +for i=1:rw1-M+1 + for j=1:cl1-N+1 + temp=B(i:i+(M-1),j:j+(N-1)); + lmean(i,j)=mean(temp); + lvar(i,j)=mean(temp.*temp)-mean(temp).^2; + end +end +nvar=sum(lvar)/sz; +lvar=max(lvar,nvar); +C=B(M/2:rw1-M/2,N/2:cl1-N/2); +F=nvar./lvar; +F=F.*(C-lmean); +F=C-F; +F=uint8(F); +figure; +ShowImage(F,'Restored'); +title('Restored Image using Adaptive Local filter','color','blue','fontsize',4);
\ No newline at end of file |