-
Notifications
You must be signed in to change notification settings - Fork 2
/
Assignment2_JatinJain_5250.m
511 lines (356 loc) · 9.54 KB
/
Assignment2_JatinJain_5250.m
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
InputImage = 'IDPicture.bmp';
A = imread(InputImage);
% Q. 1
I = 0.299 * A(:,:,1) + 0.587 * A(:,:,2) + 0.114 * A(:,:,3);
%Generate histogram
hist = zeros([255,1]);
for i=1:size(I,1)
for j=1:size(I,2)
hist(I(i,j)) = hist(I(i,j)) + 1;
end
end
figure, histogram = bar(hist,0.2)
title('Histogram')
xlim([0 255])
%Perform histogram operations
% 1.Contrast enhancement
L = double(I);
Output = ((L-20)*255)/210;
for i=1:size(Output,1)
for j=1:size(Output,2)
if Output(i,j) < 0
Output(i,j) = 0;
elseif Output(i,j) > 255
Output(i,j) = 255;
end
end
end
Output = uint8(Output);
figure,imshow(Output)
title('Linear Stretch');
%Generate histogram
hist = zeros([256,1]);
for i=1:250
for j=1:250
hist(Output(i,j)+1) = hist(Output(i,j)+1) + 1;
end
end
figure,histogram = bar(hist,0.2)
xlim([0 255])
title('Linear Stretch Histogram');
%Perform histogram operations
L = double(I) % convert to double;
Output = ((L-20)*255)/210;
for i=1:size(Output,1)
for j=1:size(Output,2)
if Output(i,j) < 0
Output(i,j) = 0;
elseif Output(i,j) > 255
Output(i,j) = 255;
end
end
end
Output = uint8(Output); % Convert to uint8
figure,imshow(Output)
title('Linear Stretch');
%Generate histogram
hist = zeros([256,1]);
for i=1:250
for j=1:250
hist(Output(i,j)+1) = hist(Output(i,j)+1) + 1;
end
end
figure,histogram = bar(hist,0.2)
xlim([0 255])
title('Linear Stretch Histogram');
% 2. Thresholding
binary_image = I
for i=1:size(binary_image,1)
for j=1:size(binary_image,2)
if binary_image(i,j) > 100
binary_image(i,j) = 255;
else
binary_image(i,j) = 0;
end
end
end
%Generate histogram
hist = zeros([256,1]);
for i=1:size(binary_image,1)
for j=1:size(binary_image,2)
hist(binary_image(i,j)+1) = hist(binary_image(i,j)+1) + 1;
end
end
figure,imshow(binary_image)
title('Threshold Image');
figure,histogram = bar(hist,0.2)
xlim([0 255])
title('Threshold Histogram');
%%
% 3. Histogram Equalization
Img=imhist(I)
Output=Img/numel(I);
CSum=cumsum(Output);
equalized_image=CSum(I+1);
for i=1:size(equalized_image,1)
for j=1:size(equalized_image,2)
if equalized_image(i,j) < 0
equalized_image(i,j) = 0;
elseif equalized_image(i,j) > 255
equalized_image(i,j) = 255;
end
end
end
figure,imshow(equalized_image);
title('Histogram Equalization');
%Generate histogram
hist = zeros([256,1]);
for i=1:size(equalized_image,1)
for j=1:size(equalized_image,2)
hist(equalized_image(i,j)+1) = hist(equalized_image(i,j)+1) + 1;
end
end
figure,histogram = bar(hist,0.2)
xlim([0 255])
title('Equalized Histogram');
%%
% QUESTION 2
C=double(I); % Convert to double
for i=1:size(C,1)-2
for j=1:size(C,2)-2
X=((2*C(i+2,j+1)+C(i+2,j)+C(i+2,j+2))-(2*C(i,j+1)+C(i,j)+C(i,j+2)));
Y=((2*C(i+1,j+2)+C(i,j+2)+C(i+2,j+2))-(2*C(i+1,j)+C(i,j)+C(i+2,j)));
sobel_image(i,j)=sqrt(X.^2+Y.^2);
end
end
sobel_image = uint8(sobel_image);
figure,imshow(sobel_image);
title('Sobel gradient');
%Generate histogram
hist = zeros([256,1]);
for i=1:size(sobel_image,1)
for j=1:size(sobel_image,2)
hist(sobel_image(i,j)+1) = hist(sobel_image(i,j)+1) + 1;
end
end
figure,histogram = bar(hist,0.2)
xlim([0 255])
title('Sobel Histogram');
%%
% 2. Horizontal Sobel Operator
C=double(I);
for i=1:size(C,1)-2
for j=1:size(C,2)-2
X=((2*C(i+2,j+1)+C(i+2,j)+C(i+2,j+2))-(2*C(i,j+1)+C(i,j)+C(i,j+2)));
hsobel_image(i,j)=sqrt(X.^2);
end
end
hsobel_image = uint8(hsobel_image);
figure,imshow(hsobel_image);
title('Horizontal Sobel gradient');
%%
% 3. Vertical Sobel Gradient
C=double(I); % Convert image to double
for i=1:size(C,1)-2
for j=1:size(C,2)-2
%Find gradient for y direction
Y=((2*C(i+1,j+2)+C(i,j+2)+C(i+2,j+2))-(2*C(i+1,j)+C(i,j)+C(i+2,j)));
vsobel_image(i,j)=sqrt(Y.^2);
end
end
vsobel_image = uint8(vsobel_image);
figure,imshow(vsobel_image);
title('Vertical Sobel gradient');
%%
% 4. 1x2 Combined edge
% Kernel: x: [-1,1] y: [-1;1]
C = double(I); % Convert image to a double
for i=1:size(C,1)-2
for j=1:size(C,2)-2
X = (C(i,j+1) - C(i,j));
Y = (C(i+1,j) - C(i,j));
onetwo_image(i,j) = sqrt(X.^2+Y.^2);
end
end
onetwo_image = uint8(onetwo_image);
figure,imshow(onetwo_image);
title('1x2');
%Generate histogram
hist = zeros([256,1]);
for i=1:size(onetwo_image,1)
for j=1:size(onetwo_image,2)
hist(onetwo_image(i,j)+1) = hist(onetwo_image(i,j)+1) + 1;
end
end
figure,histogram = bar(hist,0.2)
xlim([0 255])
title('1x2 Histogram');
%%
% 5. 1x2 Horizontal edge
C = double(I); % Convert to a double
for i=1:size(C,1)-2
for j=1:size(C,2)-2
% Find gradient for x direction
X = (C(i+1,j) - C(i,j));
h_onetwo(i,j) = sqrt(X.^2);
end
end
h_onetwo = uint8(h_onetwo);
figure,imshow(h_onetwo);
title('1x2 Horizontal gradient');
%%
% 6. 1x2 Vertical edge
C = double(I); % Convert to a double
for i=1:size(C,1)-2
for j=1:size(C,2)-2
%Find gradient for y direction
Y = (C(i,j+1) - C(i,j));
v_onetwo(i,j) = sqrt(Y.^2);
end
end
v_onetwo = uint8(v_onetwo);
figure,imshow(v_onetwo);
title('1x2 Vertical gradient');
%%
% QUESTION 4
% Sobel Edge Map
% Use histogram to choose threshold value
thresh=120;
sobel_thresh = sobel_image;
for i=1:size(sobel_image,1)
for j=1:size(sobel_image,2)
if sobel_image(i,j) > thresh
sobel_thresh(i,j) = 255;
else
sobel_thresh(i,j) = 0;
end
end
end
figure,imshow(sobel_thresh)
title('Sobel Threshold');
%Generate histogram
hist = zeros([256,1]);
for i=1:size(sobel_thresh,1)
for j=1:size(sobel_thresh,2)
hist(sobel_thresh(i,j)+1) = hist(sobel_thresh(i,j)+1) + 1;
end
end
figure,histogram = bar(hist,0.2)
xlim([0 255])
title('Sobel Threshold Histogram');
%%
% 1x2 Edge Map
% Use histogram to choose threshold value
thresh = 30;
onetwo_thresh = onetwo_image;
for i=1:size(onetwo_image,1)
for j=1:size(onetwo_image,2)
if onetwo_image(i,j) > thresh
onetwo_thresh(i,j) = 255;
else
onetwo_thresh(i,j) = 0;
end
end
end
figure,imshow(onetwo_thresh)
title('1x2 Threshold');
%Generate histogram
hist = zeros([256,1]);
for i=1:size(onetwo_thresh,1)
for j=1:size(onetwo_thresh,2)
hist(onetwo_thresh(i,j)+1) = hist(onetwo_thresh(i,j)+1) + 1;
end
end
figure,histogram = bar(hist,0.2)
xlim([0 255])
title('1x2 Threshold Histogram');
%%
% Subtract gradient maps
subtract_gradient = sobel_image - onetwo_image;
figure,imshow(subtract_gradient)
title('Sobel - 1x2');
%%
% Color edge detector
C = double(A); % Convert to double
% Apply sobel operator to each color band
R = C(:,:,1); % Red color band
for i=1:size(C,1)-2
for j=1:size(C,2)-2
%Find gradient in x direction
X=((2*R(i+2,j+1)+R(i+2,j)+R(i+2,j+2))-(2*R(i,j+1)+R(i,j)+R(i,j+2)));
%Find gradient in y direction
Y=((2*R(i+1,j+2)+R(i,j+2)+R(i+2,j+2))-(2*R(i+1,j)+R(i,j)+R(i+2,j)));
sobel_image_red(i,j)=sqrt(X.^2+Y.^2);
end
end
sobel_image_red = uint8(sobel_image_red);
G = C(:,:,2); % Green color band
for i=1:size(C,1)-2
for j=1:size(C,2)-2
%Find gradient in x direction
X=((2*G(i+2,j+1)+G(i+2,j)+G(i+2,j+2))-(2*G(i,j+1)+G(i,j)+G(i,j+2)));
%Find gradient in y direction
Y=((2*G(i+1,j+2)+G(i,j+2)+G(i+2,j+2))-(2*G(i+1,j)+G(i,j)+G(i+2,j)));
sobel_image_green(i,j)=sqrt(X.^2+Y.^2);
end
end
sobel_image_green = uint8(sobel_image_green);
B = C(:,:,3); % Blue color band
for i=1:size(C,1)-2
for j=1:size(C,2)-2
%Find gradient in x direction
X=((2*B(i+2,j+1)+B(i+2,j)+B(i+2,j+2))-(2*B(i,j+1)+B(i,j)+B(i,j+2)));
%Find gradient in y direction
Y=((2*B(i+1,j+2)+B(i,j+2)+B(i+2,j+2))-(2*B(i+1,j)+B(i,j)+B(i+2,j)));
sobel_image_blue(i,j)=sqrt(X.^2+Y.^2);
end
end
sobel_image_blue = uint8(sobel_image_blue);
%concatenate 3 color band images
rgb_sobel = cat(3,sobel_image_red,sobel_image_green,sobel_image_blue);
figure,imshow(rgb_sobel);
title('Color Sobel');
%Intensity Image of rgb sobel
rgb_sobel_I = 0.299 * rgb_sobel(:,:,1) + 0.587 * rgb_sobel(:,:,2) + 0.114 * rgb_sobel(:,:,3);
figure,imshow(rgb_sobel_I);
title('RGB Sobel Intensity');
%Generate histogram
hist = zeros([256,1]);
for i=1:size(rgb_sobel_I,1)
for j=1:size(rgb_sobel_I,2)
hist(rgb_sobel_I(i,j)+1) = hist(rgb_sobel_I(i,j)+1) + 1;
end
end
figure,histogram = bar(hist,0.2)
xlim([0 255])
title('RGB Sobel Intensity Histogram');
%Threshold of rgb sobel intensity image
thresh = 120;
rgbsobel_I_thresh = rgb_sobel_I;
for i=1:size(rgb_sobel_I,1)
for j=1:size(rgb_sobel_I,2)
if rgb_sobel_I(i,j) > thresh
rgbsobel_I_thresh(i,j) = 255;
else
rgbsobel_I_thresh(i,j) = 0;
end
end
end
figure,imshow(rgbsobel_I_thresh);
title('RGB Sobel Intensity Threshold');
%Threshold of rgb sobel
thresh = 120;
rgbsobel_thresh = rgb_sobel;
for i=1:size(rgb_sobel,1)
for j=1:size(rgb_sobel,2)
for k=1:size(rgb_sobel,3)
if rgb_sobel(i,j,k) > thresh
rgb_sobel(i,j,k) = 255;
else
rgb_sobel(i,j,k) = 0;
end
end
end
end
figure,imshow(rgb_sobel);
title('RGB Sobel Threshold');