Skip to content

Commit 7a23058

Browse files
authored
Added Rotation Support
1 parent 2593571 commit 7a23058

File tree

1 file changed

+50
-11
lines changed

1 file changed

+50
-11
lines changed

nii2png.m

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,51 @@
2626
image = niftiread(fullfile(path,file));
2727
image_info = niftiinfo(fullfile(path,file));
2828
nifti_array = size(image);
29+
double = im2double(image);
30+
31+
# set rotations
32+
data_4d = mat2gray(double(:,:,i,j))
33+
data_rot1_4d = rot90(data_4d)
34+
data_rot2_4d = rot90(data_rot1_4d)
35+
data_rot3_4d = rot90(data_rot2_4d)
36+
37+
data_3d = mat2gray(double(:,:,i))
38+
data_rot1_3d = rot90(data_3d)
39+
data_rot2_3d = rot90(data_rot1_3d)
40+
data_rot3_3d = rot90(data_rot2_3d)
41+
42+
ask_rotate = input(' Would you like to rotate the orientation? (y/n) ', 's')
43+
44+
if lower(ask_rotate) == 'y'
45+
ask_rotate_num = input('OK. By 90° 180° or 270°? ', 's')
46+
47+
if ask_rotate_num == 90
48+
if length(nifti_array) == 4
49+
data = data_rot1_4d;
50+
elseif length(nifti_array) == 3
51+
data = data_rot1_3d;
52+
elseif ask_rotate_num == 180
53+
if length(nifti_array) == 4
54+
data = data_rot2_4d;
55+
elseif length(nifti_array) == 3
56+
data = data_rot2_3d;
57+
elseif ask_rotate_num == 280
58+
if length(nifti_array) == 4
59+
data = data_rot3_4d;
60+
elseif length(nifti_array) == 3
61+
data = data_rot3_3d;
62+
else:
63+
disp('Sorry, I did not understand that. Quitting...')
64+
exit
65+
elseif lower(ask_rotate) == 'n'
66+
disp('OK, I will convert it as it is.');
67+
if length(nifti_array) == 4
68+
data = data_4d;
69+
elseif length(nifti_array) == 3
70+
data = data_3d;
71+
else:
72+
disp('Sorry, I did not understand that. Quitting...')
73+
exit
2974

3075
% If this is a 4D NIfTI
3176
if length(nifti_array) == 4
@@ -50,11 +95,8 @@
5095
% Set Filename as per slice and vol info
5196
filename = file(1:end-4) + "_t" + sprintf('%03d', j) + "_z" + sprintf('%03d', i) + ".png";
5297

53-
% Convert Image to Double
54-
data = im2double(image);
55-
5698
% Write Image
57-
imwrite(rot90(mat2gray(data(:,:,i,j))), char(filename));
99+
imwrite(data, char(filename));
58100

59101
% If we reached the end of the slices
60102
if i == slices
@@ -63,11 +105,11 @@
63105
% Move to the next volume
64106
j = j + 1;
65107
% Write the image
66-
imwrite(rot90(mat2gray(data(:,:,i,j))), char(filename));
108+
imwrite(data, char(filename));
67109
% Else if we reached the end of slice and volume
68110
else
69111
% Write Image
70-
imwrite(rot90(mat2gray(data(:,:,i,j))), char(filename));
112+
imwrite(data, char(filename));
71113
disp('Finished!')
72114
return
73115
end
@@ -110,11 +152,8 @@
110152
% Set Filename as per slice and vol info
111153
filename = file(1:end-4) + "_z" + sprintf('%03d', i) + ".png";
112154

113-
% Convert Image to Double
114-
data = im2double(image);
115-
116155
% Write Image
117-
imwrite(rot90(mat2gray(data(:,:,i))), char(filename));
156+
imwrite(data, char(filename));
118157

119158
% Move Images To Folder
120159
movefile(char(filename),'png');
@@ -135,4 +174,4 @@
135174
elseif length(nifti_array) ~= 3 || 4
136175
disp('NIfTI must be 3D or 4D. Please try again.');
137176
end
138-
end
177+
end

0 commit comments

Comments
 (0)