-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathalignShapes.m
45 lines (39 loc) · 1.8 KB
/
alignShapes.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
function alignedShapes = alignShapes(allShapes, scaling)
% ALIGNSHAPES uses Procrustes analysis to align a set of shapes (with or without
% scaling).
%
% INPUT
% allShapes: [2*n_landmarks x n_shapes], n_shapes is basically n_subjects
% Collected from the HD scan semi-automated segmentation
% for each row: 20 points (40 elements): x1, y1, x2, y2, ..., x20, y20% %
% scaling: (bool) Do you want to scale your images or not? (default = 0)
%
% OUTPUT
% alignedShapes: The realigned shapes. Same shape as totalShapes
%
% Shape analysis techniques based on this paper:
% Cootes, T. F., Taylor, C. J., Cooper, D. H., & Graham, J. (1995).
% "Active Shape Models-Their Training and Application. Computer Vision and
% Image Understanding."
%
% See also PROCRUSTES, PLACELANDMARKS, PLOTLANDMARKS
%
% John W. Miller
% 15-Mar-2017
% Pre-allocate
n_shapes = size(allShapes,2);
alignedShapes = zeros(size(allShapes));
% Mean shape across all subjects (assuming each shape in totalShapes is a different subj)
meanShape = mean(allShapes,2); % x1, y1, x2, y2, ..., x20, y20
meanShape = [meanShape(1:2:end) meanShape(2:2:end)]; % Reshape for Procrustes
%% Loop thru each shape in totalShapes and transform via Procrustes analysis
for n_shape = 1:n_shapes
% Landmarks shape for the current subject (if 1 shape per subject)
iShape = [allShapes(1:2:end,n_shape) allShapes(2:2:end,n_shape)];
% Do the Procrustes alignment
[~, iShapeAligned] = procrustes(meanShape,iShape,'scaling',scaling,'reflection','best');
% Store the aligned shape in a similar manner as how totalShapes is passed in
alignedShapes(1:2:end,n_shape) = iShapeAligned(:,1)';
alignedShapes(2:2:end,n_shape) = iShapeAligned(:,2)';
end
end % End of main