-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmat7gray.m
More file actions
158 lines (133 loc) · 4.74 KB
/
mat7gray.m
File metadata and controls
158 lines (133 loc) · 4.74 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
%% Load (rgb)image by user
[fname,path] = uigetfile('*.png', 'Select an image');
fname = strcat(path,fname);
data = imread(fname);
%% Show original image
figure, imshow(data);
title('Original image');
%% Convert the original image into grayscale image
data_gray = rgb2gray(data);
%% Show grayscale image
figure, imshow(data_gray);
title('Grayscale image');
%% Use a median filter to filter out noise
data_gray = medfilt2(data_gray, [3 3]);
%% 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.7 -> 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.
data_bw = imbinarize(data_gray, 'adaptive', 'ForegroundPolarity', 'bright', 'Sensitivity', 0.7);
figure, imshow(data_bw), title('Binary image after the segmentation');
% Remove objects containing fewer than 200px
data_bw = bwareaopen(data_bw, 200);
% Show binary image
figure, imshow(data_bw), title('Binary image');
%% Start image blob analysis.
% Find connected components in binary image
bw = bwconncomp(data_bw, 8);
% 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);
result = data_gray;
% This is a loop to:
% 1) Bound the objects in a rectangular box
% 2) Find line where calculate the intensity of each pixel
% 3) Find value of the head and the tail
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
%% 1) Bound the object in a rectangular box
% insertShape(image, shape, position, characteristics, charact_value)
result = insertShape(result, 'Rectangle', bb, 'Color', 'green', 'LineWidth', 2);
% insertText(image, position, pos_value, characteristics, charact_value)
% default -> BoxColor:yellow - FontColor: black
result = insertText(result, [bb(1,1)-15 bb(1,2)], object, 'BoxOpacity', 1, 'FontSize', 10, 'BoxColor', 'green');
%% 2) 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];
%% 3) Find value of the head and the tail
clear M2 M3
% improfile: get pixel-value along line segment
[M2] = improfile(data_gray,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;
head = m_max - n_min;
HH(object,1) = head;
tail = length(M3) - m_max;
TT(object,1) = tail;
% Print single comet and it's tail-value
figure, subplot(1,2,1);
crop = imcrop(data_gray,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(data_gray,x,y);
title(['Histogram ',num2str(object)]);
xlabel('Length of comet');
ylabel('Color value');
break;
end
nComet = bw.NumObjects;
figure, imshow(result), title(['Comets found: ',num2str(nComet)]);