-
Notifications
You must be signed in to change notification settings - Fork 0
/
calcCentroid.m
66 lines (59 loc) · 1.83 KB
/
calcCentroid.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
function centroid = calcCentroid()
centroid = {};
img = [1, 1, 0, 0; 1, 1, 0, 0; 1, 0, 1, 1; 0, 1, 0, 0];
sz = size(img);
if (mod(sz(2),2) ~= 0)
colSz = (sz(2) - 1) / 2;
else
colSz = sz(2) / 2;
end
if (mod(sz(1),2) ~= 0)
rowSz = (sz(1) - 1) / 2;
else
rowSz = sz(1) / 2;
end
i = 1;
j = 1;
sumOfX = 0;
sumOfY = 0;
numOf1s = 0;
while true
if img(i, j) == 1
sumOfX = sumOfX + i;
sumOfY = sumOfY + j;
numOf1s = numOf1s + 1;
end
if j == colSz && i == sz(2)
j = colSz + 1;
i = 1;
centroid = [centroid, (sumOfX / numOf1s), (sumOfY / numOf1s)];
sumOfX = 0;
sumOfY = 0;
numOf1s = 0;
elseif j == colSz
if i == rowSz
centroid = [centroid, (sumOfX / numOf1s), (sumOfY / numOf1s)];
sumOfX = 0;
sumOfY = 0;
numOf1s = 0;
end
j = 1;
i = i + 1;
elseif j == sz(2) && i == sz(1)
centroid = [centroid, (sumOfX / numOf1s), (sumOfY / numOf1s)];
break;
elseif j == sz(2)
if i == rowSz
centroid = [centroid, (sumOfX / numOf1s), (sumOfY / numOf1s)];
sumOfX = 0;
sumOfY = 0;
numOf1s = 0;
end
j = colSz + 1;
i = i + 1;
else
j = j + 1;
end
end
centroid = cell2mat(centroid);
end