-
Notifications
You must be signed in to change notification settings - Fork 3
/
CentroidCalculation.m
120 lines (96 loc) · 3.41 KB
/
CentroidCalculation.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
function [cx, cy] = CentroidCalculation(image,method, display)
%{
Created by Sergio Bonaque-Gonzalez. Optical Engineer.
This scripts calculates the centroid of an image by several ways.
INPUTS:
Imagen= problem image. Any size is valid.
Manera= flag that indicates how the centroid will be calculated.
display = 1, flag which indicates wheter paint or not the image and the centroid, pausing for a while.
OUTPUTS:
[cx,cy]= coordinates of the centroid.
%}
cx=0;
cy=0;
%Only positive matrix are accepted as an input image
if min(min(image)) < 0
disp('This function does not accept negative values in the input image');
return
end
[size_y,size_x]=size(image);
%The special case where all values are zero is resolved here:
if sum(sum(image))==0
cx=length(sum(image))/2;
cy=length(sum(image,2)')/2;
% Method 1: classical way.
elseif method == 1
cx=(sum(image)*(1:length(sum(image)))')/sum(sum(image));
cy=(sum(image,2)'*(1:length(sum(image,2)'))')/sum(sum(image));
%Method 2.
elseif method ==2
% First,the maximum is calculated
[~,r,c] = max2D(image);
maximo_x=c;
maximo_y=r;
vector_x= -maximo_x+1:(size_x-maximo_x);
vector_y= -maximo_y+1:(size_y-maximo_y);
delta_x=(sum(image)*(vector_x'))/sum(sum(image));
delta_y=(sum(image,2)'*(vector_y)')/sum(sum(image));
%Finally, displacements from the maximum are calculated.
cx= maximo_x+delta_x;
cy= maximo_y+delta_y;
%Method 3: normalization of the peak
elseif method ==3
vall = max(image(:));
imagen2=image./vall;
cx=(sum(imagen2)*(1:length(sum(imagen2)))')/sum(sum(imagen2));
cy=(sum(imagen2,2)'*(1:length(sum(imagen2,2)'))')/sum(sum(imagen2));
%Method 4: setting a threshold in the average of the border, plus 3 times the standard deviation. After that, the centroid is calculated.
elseif method ==4
marco=[image(1,:) image(:,size_x)' image(size_y,:) image(:,1)'];
media_marco=mean(marco);
std_marco=std(marco);
umbral = media_marco+3*std_marco;
imagen2=image-umbral;
imagen2(imagen2<0)=0;
cx=(sum(imagen2)*(1:length(sum(imagen2)))')/sum(sum(imagen2));
cy=(sum(imagen2')*(1:length(sum(imagen2')))')/sum(sum(imagen2'));
%Method 5: setting a threshold in the average of the whole image. Useful when borders have no information
elseif method ==5
roi=image(image > 0);
umbral = mean(roi(:));
imagen2=image-umbral;
imagen2(imagen2<0)=0;
cx=(sum(imagen2)*(1:length(sum(imagen2)))')/sum(sum(imagen2));
cy=(sum(imagen2,2)*(1:length(sum(imagen2,2)'))')/sum(sum(imagen2));
else
disp('Not defined method for calculate the centroid');
return
end
%Painting
if method ==4
if display == 1
subplot(1,2,1);imshow(image, []);
line([cx-1,cx+1],[cy ,cy],'color','b');
line([cx,cx],[cy-1 ,cy+1],'color','b');
if method ==4
subplot(1,2,2);
plot(image);
hold;plot(umbral*ones(size(image)),'o');
end
shg;
zoom(4);
pause(0.3);
close;
end
else
if display == 1
imshow(image, []);
line([cx-1,cx+1],[cy ,cy],'color','b');
line([cx,cx],[cy-1 ,cy+1],'color','b');
shg;
zoom(4);
pause(0.1);
end
end
end