-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTask1to4.m
175 lines (143 loc) · 4.4 KB
/
Task1to4.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
clear; close all;
% Task 1: Pre-processing -----------------------
% Step-1: Load input image
I = imread('IMG_01.png');
figure, imshow(I)
title('original')
saveas(gcf,"Original.png")
%--------------------------
% Step-2: Covert image to grayscale
I_gray = rgb2gray(I);
figure, imshow(I_gray)
title('Gray Conversion')
saveas(gcf,"Gray Conversion")
%--------------------------
% Step-3: Rescale image
I_resize = imresize(I_gray,[512, NaN]);
figure, imshow(I_resize)
title('Rescaled(imresize)')
% Step-4: Produce histogram of the resized image
% histogram(I_resize,64);
% title('Histogram before enhancemnt')
% saveas(gcf,"Histogram before enhancemnt")
%--------------------------
% Step-5: Enhance image before binarisation
I_imadjust = imadjust(I_resize);
figure, imshow(I_imadjust)
title('Image enhanced for binarization')
saveas(gcf,"Image enhanced for binarization")
%--------------------------
% Step-6: Histogram after enhancement
% histogram(I_imadjust);
% title('Histogram after enhanced')
% saveas(gcf,"Histogram after enhanced")
%--------------------------
% Step-7: Image Binarisation
BW = imbinarize(I_imadjust);
figure, imshow(BW)
title('Binarized Image')
saveas(gcf,"Binarized Image")
%--------------------------
% Task 2: Edge detection ------------------------
canny = edge(I_imadjust,'Canny');
figure,imshow(canny)
title('canny edge detection')
saveas(gcf,"canny edge detection")
%--------------------------
% Task 3: Simple segmentation --------------------
level = graythresh(I_resize);
BW = imbinarize(I,level);
figure, imshowpair(I_gray,BW,'montage')
% figure, imshow(BW)
title('Graythresh segmentation')
saveas(gcf,"Graythresh segmentation")
%active contour
mask = zeros(size(I_resize));
mask(25:end-25,25:end-25) = 1;%boundaries
%Create a mask
bw = activecontour(I_resize,mask,900);
figure, imshow(bw)
title('Active Contour Segmentation')
saveas(gcf,"Active Contour Segmentation")
%de-noise with bwareaopen
dnoise = bwareaopen(bw,170);
figure, imshow(dnoise)
title('Denoised image (bweareaopen)')
saveas(gcf,"Denoised image (bweareaopen)")
%--------------------------
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Task 4: Object Recognition --------------------
%read image
i = imread('IMG_11.png');
%greyscale
g = rgb2gray(i);
%enhance image for binarization
ima = imadjust(g);
%simple segmentation on grayscale
level = graythresh(ima);
BW = imbinarize(ima,level);
% figure, imshowpair(ima,BW,'montage')
% title('simple segmentation')
%
mask = zeros(size(BW));
mask(25:end-25,25:end-25) = 1;
bw = activecontour(BW,mask,900);
% figure, imshow(bw)
% title('active contour')
%----------------------------------%
level = graythresh(ima);
BW = imbinarize(ima,level);
% figure, imshowpair(ima,BW,'montage')
% title('simple segmentation')
%active contour
mask = zeros(size(ima));
mask(25:end-25,25:end-25) = 1; %boundaries
bw = activecontour(ima,mask,1600);
% figure, imshow(bw)
% title('active contour')
%de-noise
bw = bwareaopen(bw,170);
% imshow(bw)
% title('denoise')
figure, imshow(bw)
title('Object Recognition')
saveas(gcf,"Object Recognition")
%fill holes
imfill(bw, 'holes')
%get boundaries of all solid objects
[B,L] = bwboundaries(bw,'noholes');
hold on
%
%Loop over every object in B and get boundary.
for k = 1:length(B)
boundary = B{k};
end
% Get region statistics for later
stats = regionprops(L,'Area','Centroid','PixelList');
%Circularity threshold value
threshold = 0.50;
% loop over boundaries
for k = 1:length(B)
% obtain (X,Y) boundary coordinates corresponding to label 'k']
% Obtain the coordinates of every boundary in k
boundary = B{k};
% Calculate an estimate of the object perimeter, store
delta_sq = diff(boundary).^2;
perimeter = sum(sqrt(sum(delta_sq,2)));
% get the area for item 'k'
area = stats(k).Area;
% compute circularity
metric = 4*pi*area/perimeter^2;
% Display circularity value (for testing)
metric_string = sprintf('%2.2f',metric);
% mark objects above the threshold with a black circle
if metric > threshold
fill(boundary(:,2), boundary(:,1), 'r','LineWidth',2 ,'LineJoin','round', 'LineStyle','none');
end
if metric < threshold
fill(boundary(:,2), boundary(:,1), 'b','LineWidth',2 ,'LineJoin','round', 'LineStyle','none');
end
%text(boundary(1,2)-35,boundary(1,1)+13,metric_string,'Color','y',...
%'FontSize',14,'FontWeight','bold')
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%