summaryrefslogtreecommitdiff
path: root/Digital_Image_Processing_by_S_Jayaraman/4-Image_Transforms.ipynb
blob: 31133d0ca7ecd1e5122303e9bfa3b90cb910d85c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
{
"cells": [
 {
		   "cell_type": "markdown",
	   "metadata": {},
	   "source": [
       "# Chapter 4: Image Transforms"
	   ]
	},
{
		   "cell_type": "markdown",
		   "metadata": {},
		   "source": [
			"## Example 4.10: Program_to_compute_discrete_cosine_transform.sce"
		   ]
		  },
  {
"cell_type": "code",
	   "execution_count": null,
	   "metadata": {
	    "collapsed": true
	   },
	   "outputs": [],
"source": [
"//Caption: Program to compute discrete cosine tranform\n",
"//Example4.10\n",
"//page 198\n",
"clc;\n",
"N =4; //DCT matrix of order four\n",
"X = dct_mtx(N);\n",
"disp(X,'DCT matrix of order four')\n",
"//Result\n",
"//DCT matrix of order four   \n",
"// \n",
"//    0.5          0.5          0.5          0.5        \n",
"//    0.6532815    0.2705981  - 0.2705981  - 0.6532815  \n",
"//    0.5        - 0.5        - 0.5          0.5        \n",
"//    0.2705981  - 0.6532815    0.6532815  - 0.2705981  "
   ]
   }
,
{
		   "cell_type": "markdown",
		   "metadata": {},
		   "source": [
			"## Example 4.12: Program_to_perform_KL_tranform_for_the_given_2D_matrix.sce"
		   ]
		  },
  {
"cell_type": "code",
	   "execution_count": null,
	   "metadata": {
	    "collapsed": true
	   },
	   "outputs": [],
"source": [
"//Caption: Program to perform KL transform for the given 2D matrix\n",
"//Example4.12\n",
"//page 208\n",
"clear;\n",
"clc;\n",
"X = [4,3,5,6;4,2,7,7;5,5,6,7];\n",
"[m,n]= size(X);\n",
"A = [];\n",
"E = [];\n",
"for i =1:n\n",
"    A = A+X(:,i);\n",
"    E = E+X(:,i)*X(:,i)';\n",
"end\n",
"mx = A/n;   //mean matrix\n",
"E = E/n;        \n",
"C = E - mx*mx';//covariance matrix C = E[xx']-mx*mx'\n",
"[V,D] = spec(C); //eigen values and eigen vectors\n",
"d = diag(D); //diagonal elements od eigen values\n",
"[d,i] = gsort(d); //sorting the elements of D in descending order\n",
"for j = 1:length(d)\n",
"    T(:,j)= V(:,i(j));\n",
"end\n",
"T =T'\n",
"disp(d,'Eigen Values are U = ')\n",
"disp(T,'The eigen vector matrix T =')\n",
"disp(T,'The KL tranform basis is =')\n",
"//KL transform\n",
"for i = 1:n\n",
"    Y(:,i)= T*X(:,i);\n",
"end\n",
"disp(Y,'KL transformation of the input matrix Y =')\n",
"//Reconstruction\n",
"for i = 1:n\n",
"    x(:,i)= T'*Y(:,i);\n",
"end\n",
"disp(x,'Reconstruct matrix of the given sample matrix X =')\n",
"//Result\n",
"// Eigen Values are U =    \n",
"//    6.1963372  \n",
"//    0.2147417  \n",
"//    0.0264211  \n",
"// The eigen vector matrix T =   \n",
"//    0.4384533    0.8471005    0.3002988  \n",
"//    0.4460381  - 0.4951684    0.7455591  \n",
"//  - 0.7802620    0.1929481    0.5949473  \n",
"// The KL tranform basis is =   \n",
"//    0.4384533    0.8471005    0.3002988  \n",
"//    0.4460381  - 0.4951684    0.7455591  \n",
"//  - 0.7802620    0.1929481    0.5949473  \n",
"// KL transformation of the input matrix Y =   \n",
"//    6.6437095    4.5110551    9.9237632    10.662515  \n",
"//    3.5312743    4.0755729    3.2373664    4.4289635  \n",
"//    0.6254808    1.0198466    1.0190104    0.8336957  \n",
"// Reconstruct matrix of the given sample matrix x =   \n",
"//    4.    3.    5.    6.  \n",
"//    4.    2.    7.    7.  \n",
"//    5.    5.    6.    7.  "
   ]
   }
,
{
		   "cell_type": "markdown",
		   "metadata": {},
		   "source": [
			"## Example 4.13: Program_to_find_the_singular_value_decomposition_of_given_matrix.sce"
		   ]
		  },
  {
"cell_type": "code",
	   "execution_count": null,
	   "metadata": {
	    "collapsed": true
	   },
	   "outputs": [],
"source": [
"//Caption: Program to find the singular value decomposition of given matrix\n",
"//Example4.13\n",
"//page 210\n",
"clear;\n",
"clc;\n",
"A = [1,-2,3;3,2,-1];\n",
"[U,S,V]= svd(A);\n",
"A_recon = U*S*V';\n",
"disp(U,'U =')\n",
"disp(S,'S =')\n",
"disp(V,'V =')\n",
"disp(A_recon,'A matrix from svd =')\n",
"//Result\n",
"// U =   \n",
"// \n",
"//  - 0.7071068    0.7071068  \n",
"//    0.7071068    0.7071068  \n",
"// \n",
"// S =   \n",
"// \n",
"//    4.2426407    0.           0.  \n",
"//    0.           3.1622777    0.  \n",
"// \n",
"// V =   \n",
"// \n",
"//    0.3333333    0.8944272  - 0.2981424  \n",
"//    0.6666667  - 2.776D-16    0.7453560  \n",
"//  - 0.6666667    0.4472136    0.5962848  \n",
"// \n",
"// A matrix from svd =   \n",
"// \n",
"//    1.  - 2.    3.  \n",
"//    3.    2.  - 1. "
   ]
   }
,
{
		   "cell_type": "markdown",
		   "metadata": {},
		   "source": [
			"## Example 4.4: DFT_of_4x4_grayscale_image.sce"
		   ]
		  },
  {
"cell_type": "code",
	   "execution_count": null,
	   "metadata": {
	    "collapsed": true
	   },
	   "outputs": [],
"source": [
"//Caption: 2D DFT of 4x4 grayscale image\n",
"//Example4.4\n",
"//page 170\n",
"clc;\n",
"f = [1,1,1,1;1,1,1,1;1,1,1,1;1,1,1,1];\n",
"N =4; //4-point DFT\n",
"kernel = dft_mtx(N);\n",
"F = kernel*(f*kernel');\n",
"disp(F,'2D DFT of given 2D image =')\n",
"//Result\n",
"//2D DFT of given 2D image =   \n",
"// \n",
"//    16.    0    0    0  \n",
"//    0      0    0    0  \n",
"//    0      0    0    0  \n",
"//    0      0    0    0  "
   ]
   }
,
{
		   "cell_type": "markdown",
		   "metadata": {},
		   "source": [
			"## Example 4.5: 2D_DFT_of_4X4_grayscale_image.sce"
		   ]
		  },
  {
"cell_type": "code",
	   "execution_count": null,
	   "metadata": {
	    "collapsed": true
	   },
	   "outputs": [],
"source": [
"//Caption: 2D DFT of 4x4 grayscale image\n",
"//Example4.5\n",
"//page 171\n",
"clc;\n",
"F = [16,0,0,0;0,0,0,0;0,0,0,0;0,0,0,0];\n",
"N =4; //4-point DFT\n",
"kernel = dft_mtx(N);\n",
"f = (kernel*(F*kernel'))/(N^2);\n",
"f = real(f);\n",
"disp(f,'Inverse 2D DFT of the transformed image f =')\n",
"//Result\n",
"//Inverse 2D DFT of the transformed image f =   \n",
"// \n",
"//    1.    1.    1.    1.  \n",
"//    1.    1.    1.    1.  \n",
"//    1.    1.    1.    1.  \n",
"//    1.    1.    1.    1.  "
   ]
   }
,
{
		   "cell_type": "markdown",
		   "metadata": {},
		   "source": [
			"## Example 4.6: Scilab_code_to_intergchange_phase_information_between_two_images.sce"
		   ]
		  },
  {
"cell_type": "code",
	   "execution_count": null,
	   "metadata": {
	    "collapsed": true
	   },
	   "outputs": [],
"source": [
"//Caption:Scilab code  to intergchange phase information between two images\n",
"//Example4.6\n",
"//page 174-175\n",
"clc;\n",
"close;\n",
"a = imread('E:\DIP_JAYARAMAN\Chapter4\lena.png');  //SIVP toolbox\n",
"b = imread('E:\DIP_JAYARAMAN\Chapter4\baboon.png');\n",
"a = rgb2gray(a);\n",
"b = rgb2gray(b);\n",
"a = imresize(a,0.5);\n",
"b = imresize(b,0.5);\n",
"figure(1)\n",
"ShowImage(a,'Original lena Image');    //IPD toolbox\n",
"title('Original lena Image'); \n",
"figure(2)\n",
"ShowImage(b,'Original baboon Image');\n",
"title('Original baboon Image')\n",
"ffta = fft2d(double(a));\n",
"fftb = fft2d(double(b));\n",
"mag_a = abs(ffta);\n",
"mag_b = abs(fftb);\n",
"ph_a = atan(imag(ffta),real(ffta));\n",
"ph_b = atan(imag(fftb),real(fftb));\n",
"newfft_a = mag_a.*(exp(%i*ph_b));\n",
"newfft_b = mag_b.*(exp(%i*ph_a));\n",
"rec_a = ifft2d(newfft_a);\n",
"rec_b = ifft2d(newfft_b);\n",
"figure(3)\n",
"ShowImage(uint8(rec_a),'lena Image after phase reversal');\n",
"title('lena Image after phase reversal')\n",
"figure(4)\n",
"ShowImage(uint8(rec_b),'baboon Image after phase reversal');\n",
"title('baboon Image after phase reversal')"
   ]
   }
],
"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
}