|
a |
|
b/combinedDeepLearningActiveContour/functions/imtranslate.m |
|
|
1 |
function Iout = imtranslate(I, translation, F, method, same_size) |
|
|
2 |
%SCd 12/21/2009 |
|
|
3 |
%Affine translates a 2D or 3D image |
|
|
4 |
% |
|
|
5 |
%Input Arguments: |
|
|
6 |
% -I = 2D or 3D image |
|
|
7 |
% -translation = |
|
|
8 |
% -[row_shift col_shift] for 2D images |
|
|
9 |
% -[row_shift col_shift pag_shift] for 3D images |
|
|
10 |
% -F = values to pad the image with (optional, defaults to 0) |
|
|
11 |
% -method = interpolation method (optional, defaults to 'linear') |
|
|
12 |
% -same_size = 1 or 0, 1 if the output image is to be the same size as |
|
|
13 |
% the input image (optional, defaults to 1) |
|
|
14 |
% |
|
|
15 |
%Output Arguments: |
|
|
16 |
% -Iout = translated image |
|
|
17 |
% |
|
|
18 |
|
|
|
19 |
if nargin < 2 |
|
|
20 |
error('Missing input arguments: imtranslate(I, translation)'); |
|
|
21 |
elseif nargin == 2 |
|
|
22 |
F = 0; |
|
|
23 |
method = 'linear'; |
|
|
24 |
same_size = 1; |
|
|
25 |
elseif nargin == 3 |
|
|
26 |
method = 'linear'; |
|
|
27 |
same_size = 1; |
|
|
28 |
elseif nargin == 4 |
|
|
29 |
same_size = 1; |
|
|
30 |
end |
|
|
31 |
|
|
|
32 |
dims = max(size(translation)); |
|
|
33 |
if dims ~= max(size(size(I))) |
|
|
34 |
error('a:b', 'The number of translations is not equal to the dimensions of the image. \n Use 0 for dimensions which are not to be shifted.'); |
|
|
35 |
elseif dims == 2 |
|
|
36 |
T_dims = [1 2]; %Dimensions in order |
|
|
37 |
A = zeros(3); |
|
|
38 |
A([1 5 9]) = 1; |
|
|
39 |
A(3,1:2) = translation; |
|
|
40 |
elseif dims == 3 |
|
|
41 |
T_dims = [1 2 3]; |
|
|
42 |
A = zeros(4); |
|
|
43 |
A([1 6 11 16]) = 1; |
|
|
44 |
A(4,1:3) = translation; |
|
|
45 |
else |
|
|
46 |
error('I must be a 2D or 3D image'); |
|
|
47 |
end |
|
|
48 |
|
|
|
49 |
if same_size |
|
|
50 |
T_size_b = size(I); %In order to recieve an image of the same size with data outside of dimensions cropped. |
|
|
51 |
else |
|
|
52 |
T_size_b = size(I) + ceil(abs(translation)); %In order to create a new image big enough for the translation |
|
|
53 |
end |
|
|
54 |
|
|
|
55 |
R = makeresampler(method, 'fill'); %Interpolation method and filling the blank spots as opposed to shifting back. |
|
|
56 |
Tmap = []; %Unused with the Tform. |
|
|
57 |
Tform = maketform('affine', A); %Generate the affine transformation |
|
|
58 |
|
|
|
59 |
Iout = tformarray(I, Tform, R, T_dims, T_dims, T_size_b, Tmap, F); |
|
|
60 |
|
|
|
61 |
end |