Clear Filters
Clear Filters

Get singular sagittal slice from nifti file saved as axial slices

7 views (last 30 days)
Hello, with the following code i'm able to retrieve a single axial slice from this nifti file of a lung CT scan. I would like to know how to be able to save a slice from another view, such as sagittal or coronal in a separate image format such at png or jpg for image processing.
L1 = load_nii('lung_029.nii');
NumSlices = size(L1.img,3)
S = L1.img(:,:,125);
figure
imshow(S)
I'm using the image processing toolbox and tools for nifti and analyze image add ons.

Answers (1)

Walter Roberson
Walter Roberson about 17 hours ago
Example
L1 = load_nii('lung_029.nii');
S = size(L1.img);
r1 = randi(S(1));
r2 = randi(S(2));
r3 = randi(S(3));
view1 = reshape(L1.img(r1,:,:), S(2), S(3));
view2 = reshape(L1.img(:,r2,:), S(1), S(3));
view3 = reshape(L1.img(:,:,r3), S(1), S(2));
imwrite(view1, 'view1.png');
imwrite(view2, 'view2.png');
imwrite(view3, 'view3.png');
I should note, though, that most of the time you do not need to save the extracted data into an image file in order to process it; most of the time you can just use the arrays (view1, view2, view3) directly.

Products


Release

R2024a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!