-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmat9.m
More file actions
194 lines (167 loc) · 5.87 KB
/
mat9.m
File metadata and controls
194 lines (167 loc) · 5.87 KB
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
%% Load (rgb)image by user
[fname,path] = uigetfile('.bmp', 'Select an image');
fname = strcat(path,fname);
[data,map] = imread(fname);
figure, imshow(data), title('Original image');
%% Looking for some info about the image
infodata = imfinfo(fname);
switch infodata.ColorType
case 'indexed'
rgbImage = ind2rgb(data,map);
rgbImage = uint8(255 * rgbImage);
grayImage = rgb2gray(rgbImage);
case 'truecolor'
rgbImage = data;
grayImage = rgb2gray(rgbImage);
case 'grayscale'
grayImage = data;
end
%% Use a median filter to filter out noise
grayImage = medfilt2(grayImage, [3 3]);
figure, imshow(grayImage), title('Grayscale image');
%% Convert the resulting grayscale image into a binary image:
% - Method: adaptive -> method used to binarize image
% - ForegroundPolarity: bright -> indicate that the foreground is brighter
% than background
% - Sensivity: 0.1 -> Sensitivity factor for adaptive thresholding
% specified as a value in the range [0 1].
% A high sensitivity value leads to thresholding more pixels as foreground,
% at the risk of including some background pixels.
bwImage = imbinarize(grayImage, 'adaptive', 'ForegroundPolarity', 'bright', 'Sensitivity', 0.1);
% Remove objects smaller than 500px and greather 5000px
bwImage = xor(bwareaopen(bwImage,400), bwareaopen(bwImage,5000));
figure, imshow(bwImage), title('Binary image');
ans1 = 'Yes';
while (strcmpi(ans1, 'Yes'))
msg1 = sprintf('Do you want to remove wrong objects?');
ans1 = questdlg(msg1, 'Answer', 'Yes', 'No', 'Yes');
if strcmpi(ans1, 'Yes')
msg2 = sprintf('Remove objects smaller than ... pixel');
ans2 = inputdlg(msg2, 'Input', 1, {'500'});
bwImage = xor(bwareaopen(bwImage,str2double(ans2)), bwareaopen(bwImage,5000));
imshow(bwImage), title('Binary image');
end
end
% Find connected components in binary image
bw = bwconncomp(bwImage, 8);
numObjects = bw.NumObjects;
% Find heads of good comets
% imfindcircles to find
% viscircles to draw
[centers, radii] = imfindcircles(bwImage,[10 30]);
% h = viscircles(centers,radii,'Color','b');
numCircles = length(centers);
% Get a set of properties for each labeled region:
% - Area: number of pixel in the region
% - BoundingBox: smallest rectangle containing the region, 1*4 vector that
% contain x and y of upper left corner, width and heigth of rectangle
% - Centroid: center of mass of the region
stats = regionprops(bw, 'Area', 'BoundingBox', 'Centroid');
batot = zeros(1);
bbtot = zeros(1,4);
bctot = zeros(1,2);
HH = zeros(1);
TT = zeros(1);
TST = zeros(1);
result = grayImage;
for object = 1:length(stats)
ba = stats(object).Area;
bb = stats(object).BoundingBox;
bc = stats(object).Centroid;
% Save value of ba,bb,bc into different matrix
batot(object,1) = ba;
for b = 1:4
bbtot(object,b) = bb(1,b);
end
for c = 1:2
bctot(object,c) = bc(1,c);
end
% Find line where calculate the intensity of each pixel
larg = bb(1,3);
lung = bb(1,4);
x1 = bb(1,1);
x2 = x1 + larg;
y1 = bb(1,2) + (lung/2);
y2 = y1;
x = [x1,x2];
y = [y1,y2];
%% Find value of the head and the tail
clear M2 M3
% improfile: get pixel-value along line segment
[M2] = improfile(grayImage,x,y);
j = 1;
M3 = zeros(1);
% Get only the pixels with value > 0
for i = 1:length(M2)
if M2(i,1) > 0
M3(j,1) = M2(i,1);
j=j+1;
end;
end;
% Find max value in the matrix M3
max = 0;
max2 = 0;
m_max = 0;
for m = 1:length(M3)
max2 = M3(m,1);
if max2 >= max
max = max2;
m_max = m;
end;
end;
% Find min value in the matrix M3
min = 0;
min2 = 0;
min3 = 0;
n_min = 0;
for n = 1:m_max
min2 = M3(n,1);
n1 = n-1;
if n1 > 0
min3 = M3(n1,1);
end;
if and(min2 < max, min3 > min2)
min = min2;
n_min = n;
end;
end;
% Test validità cometa
test = 0;
for cent = 1:numCircles
diff1 = bc(1,1) - centers(cent,1);
diff2 = bc(1,2) - centers(cent,2);
if (diff1 > 2 || diff1 < -2 || diff2 > 2 || diff2 < -2) && test == 0
test = 0;
else
test = 1;
end
end
TST(object,1) = test;
if test == 0 %Comet error
head = m_max - n_min;
HH(object,1) = head;
tail = length(M3) - m_max;
TT(object,1) = tail;
result = insertText(result, [bb(1,1)-15 bb(1,2)], object, 'BoxOpacity', 1, 'FontSize', 10, 'BoxColor', 'red');
result = insertShape(result, 'Rectangle', bb, 'Color', 'red', 'LineWidth', 2);
else %Comet good
head = radii(object) * 2;
HH(object,1) = head;
tail = length(M3) - m_max;
TT(object,1) = tail;
result = insertText(result, [bb(1,1)-15 bb(1,2)], object, 'BoxOpacity', 1, 'FontSize', 10, 'BoxColor', 'green');
result = insertShape(result, 'Rectangle', bb, 'Color', 'green', 'LineWidth', 2);
end
% Print single comet and it's tail-value
figure, subplot(1,2,1);
crop = imcrop(grayImage,bb);
imshow(crop);
title({['Comet ',num2str(object)],['Image size: ',num2str(larg),'x',num2str(lung)],['Diameter of the head: ',num2str(head), ' pixel'],['Length of the tail: ',num2str(tail),' pixel']});
% Plot the histogram of the intensity along the line-segments
subplot(1,2,2);
improfile(grayImage,x,y);
title(['Histogram ',num2str(object)]);
xlabel('Length of comet');
ylabel('Color value');
end
figure, imshow(result), title(['Comets found: ',num2str(numObjects)]);