summaryrefslogtreecommitdiff
path: root/Digital_Image_Processing_by_S_Jayaraman/6-Image_Restoration_and_Denoising.ipynb
diff options
context:
space:
mode:
Diffstat (limited to 'Digital_Image_Processing_by_S_Jayaraman/6-Image_Restoration_and_Denoising.ipynb')
-rw-r--r--Digital_Image_Processing_by_S_Jayaraman/6-Image_Restoration_and_Denoising.ipynb512
1 files changed, 512 insertions, 0 deletions
diff --git a/Digital_Image_Processing_by_S_Jayaraman/6-Image_Restoration_and_Denoising.ipynb b/Digital_Image_Processing_by_S_Jayaraman/6-Image_Restoration_and_Denoising.ipynb
new file mode 100644
index 0000000..35834d9
--- /dev/null
+++ b/Digital_Image_Processing_by_S_Jayaraman/6-Image_Restoration_and_Denoising.ipynb
@@ -0,0 +1,512 @@
+{
+"cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Chapter 6: Image Restoration and Denoising"
+ ]
+ },
+{
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 6.13: Scilab_code_to_perform_wiener_filtering_of_the_corrupted_image.sce"
+ ]
+ },
+ {
+"cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+"source": [
+"//Caption: Scilab code to perform wiener filtering of the corrupted image\n",
+"//Fig6.13\n",
+"//Page 339\n",
+"close;\n",
+"clc;\n",
+"x = imread('E:\DIP_JAYARAMAN\Chapter6\flower2.jpg'); //SIVP toolbox\n",
+"x=double(rgb2gray(x));\n",
+"sigma = 50;\n",
+"Gamma = 1;\n",
+"alpha = 1; // It indicates Wiener filter\n",
+"[M N]=size(x);\n",
+"h = zeros(M,N);\n",
+"for i = 1:5\n",
+" for j = 1:5\n",
+" h(i,j) = 1/25;\n",
+" end\n",
+"end\n",
+"Freqa = fft2d(x);\n",
+"Freqh = fft2d(h);\n",
+"y = real(ifft2d(Freqh.*Freqa)) //image degradation \n",
+"y = y+25*rand(M,N,'normal'); //Adding random noise with normal distribution\n",
+"Freqy = fft2d(y);\n",
+"Powy = abs(Freqy).^2/(M*N);\n",
+"sFreqh = Freqh.*(abs(Freqh)>0)+1/Gamma*(abs(Freqh)==0);\n",
+"iFreqh = 1/sFreqh;\n",
+"iFreqh = iFreqh'.*(abs(Freqh)*Gamma>1)+Gamma*abs(sFreqh)*iFreqh*(abs(sFreqh)*Gamma<=1);\n",
+"iFreqh = iFreqh/(max(max(abs(iFreqh))));\n",
+"Powy = Powy.*(Powy>sigma^2)+sigma^2*(Powy<=sigma^2);\n",
+"Freqg = iFreqh.*(Powy-sigma^2)./(Powy-(1-alpha)*sigma^2);\n",
+"ResFreqa = Freqg.*Freqy;\n",
+"Resa = real(ifft2d(ResFreqa));\n",
+"x = uint8(x);\n",
+"y = uint8(y);\n",
+"Resa = uint8(Resa);\n",
+"ShowImage(x,'Original Image')\n",
+"title('Original Image')\n",
+"figure\n",
+"ShowImage(y,'Degraded Image')\n",
+"title('Degraded Image')\n",
+"figure\n",
+"ShowImage(Resa,'Restored Image')\n",
+"title('Restored Image')"
+ ]
+ }
+,
+{
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 6.18: Scilab_code_to_Perform_Average_Filtering_operation.sce"
+ ]
+ },
+ {
+"cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+"source": [
+"//Caption:Scilab code to Perform Average Filtering operation \n",
+"//Fig6.18\n",
+"//page 349\n",
+"clc;\n",
+"close;\n",
+"a= imread('E:\DIP_JAYARAMAN\Chapter6\lenna.jpg');//SIVP toolbox\n",
+"a=imnoise(a,'salt & pepper', 0.2); //Add salt&pepper noise tothe image \n",
+"a=double(a);\n",
+"[m n]=size(a);\n",
+"N=input('enter the window size='); //The window size can be 3x3,5x5etc\n",
+"Start=(N+1)/2;\n",
+"Out_Imag=a;\n",
+"for i=Start:(m-Start+1)\n",
+"for j=Start:(n-Start+1)\n",
+" limit=(N-1)/2;\n",
+" Sum=0;\n",
+" for k=-limit:limit,\n",
+" for l=-limit:limit,\n",
+" Sum=Sum+a(i+k,j+l);\n",
+" end\n",
+" end\n",
+" Out_Imag(i,j)=Sum/(N*N);\n",
+"end\n",
+"end\n",
+"a = uint8(a);\n",
+"Out_Imag = uint8(Out_Imag);\n",
+"ShowImage(a,'original Image')\n",
+"title('Noisy Image')\n",
+"figure\n",
+"ShowImage(Out_Imag,'average filtered Image')\n",
+"title('5x5 average filtered Image');"
+ ]
+ }
+,
+{
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 6.1: Scilab_code_to_create_motion_blur.sce"
+ ]
+ },
+ {
+"cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+"source": [
+"//Caption:Scilab code to create motion blur\n",
+"//Fig6.1\n",
+"//page 326\n",
+"clc;\n",
+"close;\n",
+"a = imread('E:\DIP_JAYARAMAN\Chapter6\humm.jpg');//SIVP toolbox\n",
+"//filter coefficients of fspecial('motion',10,25)\n",
+"H =[0,0,0,0,0,0,0,0.0032,0.0449,0.0865,0.0072;...\n",
+"0,0,0,0,0,0.0092,0.0509,0.0925,0.0629,0.0213,0;...\n",
+"0,0,0,0.0152,0.0569,0.0985,0.0569,0.0152,0,0,0;...\n",
+"0,0.0213,0.0629,0.0925,0.0509,0.0092,0,0,0,0,0;...\n",
+"0.0072,0.0865,0.0449,0.0032,0,0,0,0,0,0,0];\n",
+"Motion_Blur = imfilter(a,H);\n",
+"Motion_Blur =uint8(Motion_Blur);\n",
+"ShowImage(a,'original Image')\n",
+"title('original Image')\n",
+"figure\n",
+"ShowImage(Motion_Blur,'Motion Blurred Image')\n",
+"title('10x25 Motion Blurred Image')"
+ ]
+ }
+,
+{
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 6.21: Scilab_code_to_Perform_median_filtering.sce"
+ ]
+ },
+ {
+"cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+"source": [
+"//Caption:Scilab code to Perform median filtering\n",
+"//Fig6.21\n",
+"//page 352\n",
+"clc;\n",
+"close;\n",
+"c = imread('E:\DIP_JAYARAMAN\Chapter6\cameraman.jpg');//SIVP toolbox\n",
+"N = input('Enter the window size'); \n",
+"a = double(imnoise(c,'salt & pepper',0.2));\n",
+"[m,n] = size(a);\n",
+"b = a;\n",
+"if(modulo(N,2)==1)\n",
+" Start = (N+1)/2;\n",
+" End = Start;\n",
+" limit1 = (N-1)/2;\n",
+" limit2 = limit1;\n",
+"else\n",
+" Start = N/2;\n",
+" End = Start+1;\n",
+" limit1 = (N/2)-1;\n",
+" limit2 = limit1+1;\n",
+"end\n",
+"for i = Start:(m-End+1)\n",
+" for j = Start:(n-End+1)\n",
+" I =1;\n",
+" for k = -limit1:limit2\n",
+" for l = -limit1:limit2\n",
+" mat(I)= a(i+k,j+1)\n",
+" I = I+1;\n",
+" end\n",
+" end\n",
+" mat = gsort(mat);\n",
+" if(modulo(N,2)==1)\n",
+" b(i,j) = (mat(((N^2)+1)/2));\n",
+" else\n",
+" b(i,j) = (mat((N^2)/2)+mat(((N^2)/2)+1))/2;\n",
+" end\n",
+" end\n",
+"end\n",
+"a = uint8(a);\n",
+"b = uint8(b);\n",
+"figure\n",
+"ShowImage(c,'Original Image') \n",
+"title('Original Image')\n",
+"figure\n",
+"ShowImage(a,'noisy image') \n",
+"title('noisy image')\n",
+"figure\n",
+"ShowImage(b,'Median Filtered Image')\n",
+"title('5x5 Median Filtered Image')"
+ ]
+ }
+,
+{
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 6.23: Scilab_code_to_Perform_median_filtering_of_colour_image.sce"
+ ]
+ },
+ {
+"cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+"source": [
+"//Caption:Scilab code to Perform median filtering of colour image\n",
+"//Fig6.23(a)\n",
+"//page 353\n",
+"clc;\n",
+"close;\n",
+"a=imread('E:\DIP_JAYARAMAN\Chapter6\peppers.png'); //SIVP toolbox\n",
+"N=input('enter the window size');\n",
+"b=imresize(a,[256,256]);\n",
+"b=imnoise(b,'salt & pepper',.1);\n",
+"[m n]=size(b);\n",
+"R=b(:,:,1); \n",
+"G=b(:,:,2);\n",
+"B=b(:,:,3);\n",
+"Out_R=Func_medianall(R,N);//Applying Median filter to ‘R’ plane\n",
+"Out_G=Func_medianall(G,N);//Applying Median filter to ‘G’ plane\n",
+"Out_B=Func_medianall(B,N);//Applying Median filter to ‘B’ plane\n",
+"Out_Image(:,:,1)=Out_R;\n",
+"Out_Image(:,:,2)=Out_G;\n",
+"Out_Image(:,:,3)=Out_B;\n",
+"b = uint8(b);\n",
+"Out_Image = uint8(Out_Image);\n",
+"//ShowColorImage(b,'noise added')\n",
+"//title('noise added')\n",
+"figure\n",
+"ShowColorImage(Out_Image,'3x3 median filtered')\n",
+"title('3x3 median filtered') "
+ ]
+ }
+,
+{
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 6.24: Scilab_code_to_Perform_Trimmed_Average_Filter.sce"
+ ]
+ },
+ {
+"cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+"source": [
+"//Caption:Scilab code to Perform Trimmed Average Filter \n",
+"//Alpha trimmed average filter\n",
+"//Fig6.24\n",
+"//page 355\n",
+"clc;\n",
+"close;\n",
+"c = imread('E:\DIP_JAYARAMAN\Chapter6\lenna.jpg');//SIVP toolbox\n",
+"s = 1; //s denotes the number of values to be left in the end\n",
+"r = 1;\n",
+"N = 9; //3x3 window\n",
+"a = double(imnoise(c,'gaussian'));\n",
+"[m,n] = size(a);\n",
+"b = zeros(m,n);\n",
+"for i= 2:m-1\n",
+" for j = 2:n-1\n",
+" mat = [a(i,j),a(i,j-1),a(i,j+1),a(i-1,j),a(i+1,j),a(i-1,j-1),...\n",
+" a(i-1,j+1),a(i-1,j+1),a(i+1,j+1)];\n",
+" sorted_mat = gsort(mat);\n",
+" Sum=0;\n",
+" for k=r+s:(N-s)\n",
+" Sum = Sum+mat(k);\n",
+" end\n",
+" b(i,j)= Sum/(N-r-s);\n",
+" end\n",
+"end\n",
+"a = uint8(a);\n",
+"b = uint8(b);\n",
+"//figure\n",
+"//imshow(c) \n",
+"//title('Original Image')\n",
+"figure\n",
+"ShowImage(a,'noisy image') \n",
+"title('noisy image')\n",
+"figure\n",
+"ShowImage(b,'Trimmed Average Filtered Image')\n",
+"title('Trimmed Average Filtered Image')"
+ ]
+ }
+,
+{
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 6.5: Scilab_code_performs_inverse_filtering.sce"
+ ]
+ },
+ {
+"cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+"source": [
+"//Caption:Scilab code performs inverse filtering \n",
+"//Degrade the image by means of a known blur\n",
+"//Apply inverse filter to the blurred image and see the restored image\n",
+"//Fig6.5\n",
+"//page 330\n",
+"clc;\n",
+"close;\n",
+"x =imread('E:\DIP_JAYARAMAN\Chapter6\flower2.jpg');\n",
+"x=double(rgb2gray(x));\n",
+"[M N]=size(x);\n",
+"h = zeros(M,N);\n",
+"for i = 1:11\n",
+" for j = 1:11\n",
+" h(i,j) = 1/121;\n",
+" end\n",
+"end\n",
+"sigma = sqrt(4*10^(-7));\n",
+"freqx = fft2d(x); //Fourier transform of input image\n",
+"freqh = fft2d(h);//Fourier transform of degradation\n",
+"y = real(ifft2d(freqh.*freqx));\n",
+"freqy = fft2d(y);\n",
+"powfreqx = freqx.^2/(M*N);\n",
+"alpha = 0.5; //Indicates inverse filter \n",
+"freqg = ((freqh.')').*abs(powfreqx)./(abs(freqh.^2).*abs(powfreqx)+alpha*sigma^2);\n",
+"Resfreqx = freqg.*freqy;\n",
+"Resa = real(ifft2d(Resfreqx));\n",
+"x = uint8(x);\n",
+"y = uint8(y);\n",
+"Resa = uint8(Resa)\n",
+"ShowImage(x,'Original Image')\n",
+"title('Original Image')\n",
+"figure\n",
+"ShowImage(y,'Degraded Image')\n",
+"title('Degraded Image')\n",
+"figure\n",
+"ShowImage(Resa,'Restored Image')\n",
+"title('Restored Image')"
+ ]
+ }
+,
+{
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 6.7: Scilab_code_performs_inverse_filtering.sce"
+ ]
+ },
+ {
+"cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+"source": [
+"//Caption:Scilab code performs inverse filtering \n",
+"//Degrade the image by means of a known blur and white noise\n",
+"//The image is degraded as well as corrupted by noise\n",
+"//Apply inverse filter to restore the image\n",
+"//Fig6.7\n",
+"//page 332\n",
+"clc;\n",
+"close;\n",
+"x =imread('E:\DIP_JAYARAMAN\Chapter6\flower2.jpg');\n",
+"x=double(rgb2gray(x));\n",
+"[M N]=size(x);\n",
+"h = zeros(M,N);\n",
+"for i = 1:11\n",
+" for j = 1:11\n",
+" h(i,j) = 1/121;\n",
+" end\n",
+"end\n",
+"sigma = sqrt(4*10^(-7));\n",
+"freqx = fft2d(x); //Fourier transform of input image\n",
+"freqh = fft2d(h);//Fourier transform of degradation\n",
+"y = real(ifft2d(freqh.*freqx))+10*rand(M,N,'normal');\n",
+"freqy = fft2d(y);\n",
+"powfreqx = freqx.^2/(M*N);\n",
+"alpha = 0.5; //Indicates inverse filter \n",
+"freqg = ((freqh.')').*abs(powfreqx)./(abs(freqh.^2).*abs(powfreqx)+alpha*sigma^2);\n",
+"Resfreqx = freqg.*freqy;\n",
+"Resa = real(ifft2d(Resfreqx));\n",
+"x = uint8(x);\n",
+"y = uint8(y);\n",
+"Resa = uint8(Resa)\n",
+"ShowImage(x,'Original Image')\n",
+"title('Original Image')\n",
+"figure\n",
+"ShowImage(y,'Degraded+noise Image')\n",
+"title('Degraded+noise Image')\n",
+"figure\n",
+"ShowImage(Resa,'Restored Image')\n",
+"title('Restored Image')"
+ ]
+ }
+,
+{
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Example 6.9: Scilab_code_performs_Pseudo_inverse_filtering.sce"
+ ]
+ },
+ {
+"cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+"source": [
+"//Caption:Scilab code performs Pseudo inverse filtering \n",
+"//Degrade the image by means of a known blur and white noise\n",
+"//The image is degraded as well as corrupted by noise\n",
+"//Apply Pseudo inverse filter to restore the image\n",
+"//Fig6.9\n",
+"//page 333\n",
+"clc;\n",
+"close;\n",
+"x =imread('E:\DIP_JAYARAMAN\Chapter6\flower2.jpg');\n",
+"x=double(rgb2gray(x));\n",
+"[M N]=size(x);\n",
+"h = zeros(M,N);\n",
+"for i = 1:11\n",
+" for j = 1:11\n",
+" h(i,j) = 1/121;\n",
+" end\n",
+"end\n",
+"mask_b = ones(11,11)/121;\n",
+"[m1,n1] = size(mask_b);\n",
+"Thr_Freq = 0.2;\n",
+"freqx = fft2d(x); //Fourier transform of input image\n",
+"freqh = fft2d(h);//Fourier transform of degradation\n",
+"y = real(ifft2d(freqh.*freqx))+25*rand(M,N,'normal');\n",
+"freqy = fft2d(y);\n",
+"psf=zeros(M,N); \n",
+"psf(M/2+1-(m1-1)/2:M/2+1+(m1-1)/2,N/2+1-(n1-1)/2:N/2+1+(n1-1)/2) = mask_b;\n",
+"psf = fftshift(psf);\n",
+"freq_res = fft2d(psf);\n",
+"Inv_filt = freq_res./((abs(freq_res)).^2+Thr_Freq);\n",
+"z = real(ifft2d(freqy.*Inv_filt));\n",
+"x = uint8(x);\n",
+"y = uint8(y);\n",
+"z = uint8(z)\n",
+"ShowImage(x,'Original Image')\n",
+"title('Original Image')\n",
+"figure\n",
+"ShowImage(y,'Degraded+noise Image')\n",
+"title('Degraded+noise Image')\n",
+"figure\n",
+"ShowImage(z,'Restored Image')\n",
+"title('Restored Image')"
+ ]
+ }
+],
+"metadata": {
+ "kernelspec": {
+ "display_name": "Scilab",
+ "language": "scilab",
+ "name": "scilab"
+ },
+ "language_info": {
+ "file_extension": ".sce",
+ "help_links": [
+ {
+ "text": "MetaKernel Magics",
+ "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md"
+ }
+ ],
+ "mimetype": "text/x-octave",
+ "name": "scilab",
+ "version": "0.7.1"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 0
+}