|
a |
|
b/Image features calculation code/Working/shape features/FacetMidpointPerim.m |
|
|
1 |
|
|
|
2 |
function finalPerimeter = FacetMidpointPerim( I ) |
|
|
3 |
|
|
|
4 |
% Create an n x 2 matrix representing an ordered set of boundary pixels. |
|
|
5 |
% - The row of the matrix represents the index of each boundary pixel. |
|
|
6 |
% - The first column contains the row index of the boundary pixel in the |
|
|
7 |
% original image. |
|
|
8 |
% - The second column contains the column index of the boundary pixel in the |
|
|
9 |
% original image. |
|
|
10 |
% - Single pixel wide strands are listed twice in the ordered list, once |
|
|
11 |
% going up the strand and once coming down the other side. |
|
|
12 |
% - the last member of the list is the first member, so for n pixles, |
|
|
13 |
% there are at least n+1 members of the list |
|
|
14 |
|
|
|
15 |
|
|
|
16 |
% Traverse the contour in order, counting perimeter according to the |
|
|
17 |
%relative location of the pixel with respect to the previous one and |
|
|
18 |
% the next one. |
|
|
19 |
|
|
|
20 |
Icontour = bwboundaries(I, 'noholes'); |
|
|
21 |
pixel = Icontour{1}; |
|
|
22 |
perimeter = 0; |
|
|
23 |
for i= 1: size(pixel,1)-1; |
|
|
24 |
|
|
|
25 |
if i == 1 |
|
|
26 |
lastPixelIndex = size(pixel,1)-1; |
|
|
27 |
end |
|
|
28 |
if i ~=1 |
|
|
29 |
lastPixelIndex = i-1; |
|
|
30 |
end |
|
|
31 |
|
|
|
32 |
%%%%%%% Determine relative location of last Pixel %%%%%%%%%% |
|
|
33 |
lastPixel = 'Void'; |
|
|
34 |
if pixel(lastPixelIndex,1) == pixel(i,1) -1 |
|
|
35 |
if pixel(lastPixelIndex,2) == pixel(i,2)-1 |
|
|
36 |
lastPixel = 'TL'; |
|
|
37 |
end |
|
|
38 |
end |
|
|
39 |
|
|
|
40 |
if pixel(lastPixelIndex,1) == pixel(i,1)-1 |
|
|
41 |
if pixel(lastPixelIndex,2) == pixel(i,2) |
|
|
42 |
lastPixel = 'T'; |
|
|
43 |
end |
|
|
44 |
end |
|
|
45 |
|
|
|
46 |
if pixel(lastPixelIndex,1) == pixel(i,1)-1 |
|
|
47 |
if pixel(lastPixelIndex,2) == pixel(i,2)+1 |
|
|
48 |
lastPixel = 'TR'; |
|
|
49 |
end |
|
|
50 |
end |
|
|
51 |
|
|
|
52 |
if pixel(lastPixelIndex,1) == pixel(i,1) |
|
|
53 |
if pixel(lastPixelIndex,2) == pixel(i,2)-1 |
|
|
54 |
lastPixel = 'L'; |
|
|
55 |
end |
|
|
56 |
end |
|
|
57 |
|
|
|
58 |
if pixel(lastPixelIndex,1) == pixel(i,1) |
|
|
59 |
if pixel(lastPixelIndex,2) == pixel(i,2)+1 |
|
|
60 |
lastPixel = 'R'; |
|
|
61 |
end |
|
|
62 |
end |
|
|
63 |
|
|
|
64 |
if pixel(lastPixelIndex,1) == pixel(i,1)+ 1 |
|
|
65 |
if pixel(lastPixelIndex,2) == pixel(i,2)-1 |
|
|
66 |
lastPixel = 'BL'; |
|
|
67 |
end |
|
|
68 |
end |
|
|
69 |
|
|
|
70 |
if pixel(lastPixelIndex,1) == pixel(i,1)+ 1 |
|
|
71 |
if pixel(lastPixelIndex,2) == pixel(i,2) |
|
|
72 |
lastPixel = 'B'; |
|
|
73 |
end |
|
|
74 |
end |
|
|
75 |
|
|
|
76 |
if pixel(lastPixelIndex,1) == pixel(i,1)+ 1 |
|
|
77 |
if pixel(lastPixelIndex,2) == pixel(i,2)+1 |
|
|
78 |
lastPixel = 'BR'; |
|
|
79 |
end |
|
|
80 |
end |
|
|
81 |
|
|
|
82 |
%%%%%%% Determine relative location of next Pixel %%%%%%%%%% |
|
|
83 |
nextPixel = 'void'; |
|
|
84 |
if pixel(i+1,1) == pixel(i,1) -1 |
|
|
85 |
if pixel(i+1,2) == pixel(i,2)-1 |
|
|
86 |
nextPixel = 'TL'; |
|
|
87 |
end |
|
|
88 |
end |
|
|
89 |
|
|
|
90 |
if pixel(i+1,1) == pixel(i,1)-1 |
|
|
91 |
if pixel(i+1,2) == pixel(i,2) |
|
|
92 |
nextPixel = 'T'; |
|
|
93 |
end |
|
|
94 |
end |
|
|
95 |
|
|
|
96 |
if pixel(i+1,1) == pixel(i,1)-1 |
|
|
97 |
if pixel(i+1,2) == pixel(i,2)+1 |
|
|
98 |
nextPixel = 'TR'; |
|
|
99 |
end |
|
|
100 |
end |
|
|
101 |
|
|
|
102 |
if pixel(i+1,1) == pixel(i,1) |
|
|
103 |
if pixel(i+1,2) == pixel(i,2)-1 |
|
|
104 |
nextPixel = 'L'; |
|
|
105 |
end |
|
|
106 |
end |
|
|
107 |
|
|
|
108 |
if pixel(i+1,1) == pixel(i,1) |
|
|
109 |
if pixel(i+1,2) == pixel(i,2)+1 |
|
|
110 |
nextPixel = 'R'; |
|
|
111 |
end |
|
|
112 |
end |
|
|
113 |
|
|
|
114 |
if pixel(i+1,1) == pixel(i,1)+ 1 |
|
|
115 |
if pixel(i+1,2) == pixel(i,2)-1 |
|
|
116 |
nextPixel = 'BL'; |
|
|
117 |
end |
|
|
118 |
end |
|
|
119 |
|
|
|
120 |
if pixel(i+1,1) == pixel(i,1)+ 1 |
|
|
121 |
if pixel(i+1,2) == pixel(i,2) |
|
|
122 |
nextPixel = 'B'; |
|
|
123 |
end |
|
|
124 |
end |
|
|
125 |
|
|
|
126 |
if pixel(i+1,1) == pixel(i,1)+ 1 |
|
|
127 |
if pixel(i+1,2) == pixel(i,2)+1 |
|
|
128 |
nextPixel = 'BR'; |
|
|
129 |
end |
|
|
130 |
end |
|
|
131 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
|
132 |
if strcmp(lastPixel,'TL') |
|
|
133 |
if strcmp(nextPixel,'TL') |
|
|
134 |
perimeter = perimeter + 4*sqrt(0.5); |
|
|
135 |
end |
|
|
136 |
if strcmp(nextPixel, 'T') |
|
|
137 |
perimeter = perimeter + (0.5 + (5/2) * sqrt(0.5)); |
|
|
138 |
end |
|
|
139 |
if strcmp(nextPixel, 'TR') |
|
|
140 |
perimeter = perimeter + sqrt(0.5); |
|
|
141 |
end |
|
|
142 |
if strcmp(nextPixel, 'R') |
|
|
143 |
perimeter = perimeter + (0.5 + 0.5*sqrt(0.5) ); |
|
|
144 |
end |
|
|
145 |
if strcmp(nextPixel, 'BR') |
|
|
146 |
perimeter = perimeter + 2*sqrt(0.5); |
|
|
147 |
end |
|
|
148 |
if strcmp(nextPixel, 'B') |
|
|
149 |
perimeter = perimeter + 0.5 + (3/2)*sqrt(0.5); |
|
|
150 |
end |
|
|
151 |
if strcmp(nextPixel, 'BL') |
|
|
152 |
perimeter = perimeter + 3*sqrt(0.5); |
|
|
153 |
end |
|
|
154 |
if strcmp(nextPixel, 'L') |
|
|
155 |
perimeter = perimeter + (0.5 + (5/2) * sqrt(0.5)); |
|
|
156 |
end |
|
|
157 |
|
|
|
158 |
end |
|
|
159 |
|
|
|
160 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
|
161 |
if strcmp(lastPixel, 'T') |
|
|
162 |
if strcmp(nextPixel,'TL') |
|
|
163 |
perimeter = perimeter + (0.5 + (5/2) * sqrt(0.5)); |
|
|
164 |
end |
|
|
165 |
if strcmp(nextPixel, 'T') |
|
|
166 |
perimeter = perimeter + (1 + 2*sqrt(0.5) ); |
|
|
167 |
end |
|
|
168 |
if strcmp(nextPixel, 'TR') |
|
|
169 |
perimeter = perimeter + (0.5 + (5/2) * sqrt(0.5)); |
|
|
170 |
end |
|
|
171 |
if strcmp(nextPixel, 'R') |
|
|
172 |
%not possible |
|
|
173 |
end |
|
|
174 |
if strcmp(nextPixel, 'BR') |
|
|
175 |
perimeter = perimeter + 0.5+0.5*sqrt(0.5); |
|
|
176 |
end |
|
|
177 |
if strcmp(nextPixel, 'B') |
|
|
178 |
perimeter = perimeter + 1; |
|
|
179 |
end |
|
|
180 |
if strcmp(nextPixel, 'BL') |
|
|
181 |
perimeter = perimeter + 0.5 + (3/2)*sqrt(0.5); |
|
|
182 |
end |
|
|
183 |
if strcmp(nextPixel, 'L') |
|
|
184 |
perimeter = perimeter + 1+sqrt(0.5); |
|
|
185 |
end |
|
|
186 |
|
|
|
187 |
end |
|
|
188 |
|
|
|
189 |
|
|
|
190 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
|
191 |
if strcmp(lastPixel, 'TR') |
|
|
192 |
if strcmp(nextPixel,'TL') |
|
|
193 |
perimeter = perimeter + 3*sqrt(0.5); |
|
|
194 |
end |
|
|
195 |
if strcmp(nextPixel, 'T') |
|
|
196 |
perimeter = perimeter + (0.5 + (5/2) * sqrt(0.5)); |
|
|
197 |
end |
|
|
198 |
if strcmp(nextPixel, 'TR') |
|
|
199 |
perimeter = perimeter + 4*sqrt(0.5); |
|
|
200 |
end |
|
|
201 |
if strcmp(nextPixel, 'R') |
|
|
202 |
perimeter = perimeter + (0.5 + (5/2) * sqrt(0.5)); |
|
|
203 |
end |
|
|
204 |
if strcmp(nextPixel, 'BR') |
|
|
205 |
perimeter = perimeter + sqrt(0.5); |
|
|
206 |
end |
|
|
207 |
if strcmp(nextPixel, 'B') |
|
|
208 |
perimeter = perimeter + 0.5 + 0.5*sqrt(0.5); |
|
|
209 |
end |
|
|
210 |
if strcmp(nextPixel, 'BL') |
|
|
211 |
perimeter = perimeter + 2*sqrt(0.5); |
|
|
212 |
end |
|
|
213 |
if strcmp(nextPixel, 'L') |
|
|
214 |
perimeter = perimeter + 0.5 + (3/2)*sqrt(0.5); |
|
|
215 |
end |
|
|
216 |
|
|
|
217 |
|
|
|
218 |
end |
|
|
219 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
|
220 |
if strcmp(lastPixel, 'R') |
|
|
221 |
if strcmp(nextPixel,'TL') |
|
|
222 |
perimeter = perimeter + 0.5 + (3/2)*sqrt(0.5); |
|
|
223 |
end |
|
|
224 |
if strcmp(nextPixel, 'T') |
|
|
225 |
perimeter = perimeter + 1 + sqrt(0.5); |
|
|
226 |
end |
|
|
227 |
if strcmp(nextPixel, 'TR') |
|
|
228 |
perimeter = perimeter + 0.5 + (5/2)* sqrt(0.5); |
|
|
229 |
end |
|
|
230 |
if strcmp(nextPixel, 'R') |
|
|
231 |
perimeter = perimeter + 1+ 2* sqrt(0.5); |
|
|
232 |
end |
|
|
233 |
if strcmp(nextPixel, 'BR') |
|
|
234 |
% not possible |
|
|
235 |
end |
|
|
236 |
if strcmp(nextPixel, 'B') |
|
|
237 |
% no contribution |
|
|
238 |
end |
|
|
239 |
if strcmp(nextPixel, 'BL') |
|
|
240 |
perimeter = perimeter + 0.5 + 0.5*sqrt(0.5); |
|
|
241 |
end |
|
|
242 |
if strcmp(nextPixel, 'L') |
|
|
243 |
perimeter = perimeter + 1; |
|
|
244 |
end |
|
|
245 |
|
|
|
246 |
end |
|
|
247 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
|
248 |
if strcmp(lastPixel, 'BR') |
|
|
249 |
if strcmp(nextPixel,'TL') |
|
|
250 |
perimeter = perimeter + 2*sqrt(0.5); |
|
|
251 |
end |
|
|
252 |
if strcmp(nextPixel, 'T') |
|
|
253 |
perimeter = perimeter + 0.5 + (3/2)*sqrt(0.5); |
|
|
254 |
end |
|
|
255 |
if strcmp(nextPixel, 'TR') |
|
|
256 |
perimeter = perimeter + 3*sqrt(0.5); |
|
|
257 |
end |
|
|
258 |
if strcmp(nextPixel, 'R') |
|
|
259 |
perimeter = perimeter + 0.5 + (5/2) * sqrt(0.5); |
|
|
260 |
end |
|
|
261 |
if strcmp(nextPixel, 'BR') |
|
|
262 |
perimeter = perimeter + 4*sqrt(0.5); |
|
|
263 |
end |
|
|
264 |
if strcmp(nextPixel, 'B') |
|
|
265 |
%no contribution |
|
|
266 |
end |
|
|
267 |
if strcmp(nextPixel, 'BL') |
|
|
268 |
perimeter = perimeter + sqrt(0.5); |
|
|
269 |
end |
|
|
270 |
if strcmp(nextPixel, 'L') |
|
|
271 |
perimeter = perimeter + 0.5 + 0.5*sqrt(0.5); |
|
|
272 |
end |
|
|
273 |
|
|
|
274 |
end |
|
|
275 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
|
276 |
if strcmp(lastPixel, 'B') |
|
|
277 |
if strcmp(nextPixel,'TL') |
|
|
278 |
perimeter = perimeter + 0.5 + 0.5*sqrt(0.5); |
|
|
279 |
end |
|
|
280 |
if strcmp(nextPixel, 'T') |
|
|
281 |
perimeter = perimeter + 1; |
|
|
282 |
end |
|
|
283 |
if strcmp(nextPixel, 'TR') |
|
|
284 |
perimeter = perimeter + 0.5 + (3/2)*sqrt(0.5); |
|
|
285 |
end |
|
|
286 |
if strcmp(nextPixel, 'R') |
|
|
287 |
perimeter = perimeter + 1+ sqrt(0.5); |
|
|
288 |
end |
|
|
289 |
if strcmp(nextPixel, 'BR') |
|
|
290 |
perimeter = perimeter + 0.5 + (5/2)*sqrt(0.5); |
|
|
291 |
end |
|
|
292 |
if strcmp(nextPixel, 'B') |
|
|
293 |
perimeter = perimeter + 1 + 2*sqrt(0.5); |
|
|
294 |
end |
|
|
295 |
if strcmp(nextPixel, 'BL') |
|
|
296 |
%No contribution |
|
|
297 |
end |
|
|
298 |
if strcmp(nextPixel, 'L') |
|
|
299 |
%No contribution |
|
|
300 |
end |
|
|
301 |
|
|
|
302 |
end |
|
|
303 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
|
304 |
if strcmp(lastPixel, 'BL') |
|
|
305 |
if strcmp(nextPixel,'TL') |
|
|
306 |
perimeter = perimeter + sqrt(0.5); |
|
|
307 |
end |
|
|
308 |
if strcmp(nextPixel, 'T') |
|
|
309 |
perimeter = perimeter + 0.5 + 0.5*sqrt(0.5); |
|
|
310 |
end |
|
|
311 |
if strcmp(nextPixel, 'TR') |
|
|
312 |
perimeter = perimeter + 2*sqrt(0.5); |
|
|
313 |
end |
|
|
314 |
if strcmp(nextPixel, 'R') |
|
|
315 |
perimeter = perimeter + 0.5 + (3/2)*sqrt(0.5); |
|
|
316 |
end |
|
|
317 |
if strcmp(nextPixel, 'BR') |
|
|
318 |
perimeter = perimeter + 3*sqrt(0.5); |
|
|
319 |
end |
|
|
320 |
if strcmp(nextPixel, 'B') |
|
|
321 |
perimeter = perimeter + 0.5 + (5/2)*sqrt(0.5); |
|
|
322 |
end |
|
|
323 |
if strcmp(nextPixel, 'BL') |
|
|
324 |
perimeter = perimeter + 4*sqrt(0.5); |
|
|
325 |
end |
|
|
326 |
if strcmp(nextPixel, 'L') |
|
|
327 |
%no contribution |
|
|
328 |
end |
|
|
329 |
|
|
|
330 |
end |
|
|
331 |
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
|
|
332 |
if strcmp(lastPixel, 'L') |
|
|
333 |
if strcmp(nextPixel,'TL') |
|
|
334 |
%No contribution |
|
|
335 |
end |
|
|
336 |
if strcmp(nextPixel, 'T') |
|
|
337 |
%No Contribution |
|
|
338 |
end |
|
|
339 |
if strcmp(nextPixel, 'TR') |
|
|
340 |
perimeter = perimeter + 0.5 + 0.5*sqrt(0.5); |
|
|
341 |
end |
|
|
342 |
if strcmp(nextPixel, 'R') |
|
|
343 |
perimeter = perimeter + 1; |
|
|
344 |
end |
|
|
345 |
if strcmp(nextPixel, 'BR') |
|
|
346 |
perimeter = perimeter + 0.5 + (3/2)*sqrt(0.5); |
|
|
347 |
end |
|
|
348 |
if strcmp(nextPixel, 'B') |
|
|
349 |
perimeter = perimeter + 1 + sqrt(0.5); |
|
|
350 |
|
|
|
351 |
|
|
|
352 |
end |
|
|
353 |
if strcmp(nextPixel, 'BL') |
|
|
354 |
perimeter = perimeter + 0.5 + (5/2)*sqrt(0.5); |
|
|
355 |
end |
|
|
356 |
if strcmp(nextPixel, 'L') |
|
|
357 |
perimeter = perimeter + 1 + 2*sqrt(0.5); |
|
|
358 |
end |
|
|
359 |
|
|
|
360 |
end |
|
|
361 |
|
|
|
362 |
|
|
|
363 |
|
|
|
364 |
end |
|
|
365 |
finalPerimeter = perimeter; |
|
|
366 |
|
|
|
367 |
% In three cases, P^2 < 4*pi*A, which is not possible in Euclidean geometry |
|
|
368 |
% so we adjust the perimeter to equal a value which satisfies this relationship |
|
|
369 |
|
|
|
370 |
% if it is just one pixel, make the perimeter equal to the perimeter of |
|
|
371 |
% a circle with area equal to 1 |
|
|
372 |
if size(pixel,1)== 2 |
|
|
373 |
finalPerimeter = 2*sqrt(pi); %3.544908; |
|
|
374 |
end |
|
|
375 |
%If it is two pixels aligned, make the perimeter equal to the perimeter of |
|
|
376 |
%an ellipse with ara equal to 2 and minor axis = 0.5 |
|
|
377 |
if size(pixel,1) == 3 && ( pixel(1,1) == pixel(2,1) || pixel(1,2) == pixel(2,2) ) |
|
|
378 |
finalPerimeter = 6.0774; |
|
|
379 |
end |
|
|
380 |
%If it is four pixels arranged as a square |
|
|
381 |
if(size(pixel, 1) == 5) |
|
|
382 |
if (perimeter < 6.8285) |
|
|
383 |
finalPerimeter = 7.089815; |
|
|
384 |
end |
|
|
385 |
end |
|
|
386 |
|
|
|
387 |
end |
|
|
388 |
|
|
|
389 |
|
|
|
390 |
|
|
|
391 |
|
|
|
392 |
|
|
|
393 |
|
|
|
394 |
|
|
|
395 |
|