I am converting a 3D image to a set of 2D images(slices), but the images I receive are not of good quality, so there is a loss of information. Is there a solution? please help
6 views (last 30 days)
Show older comments
clear all;
close all;
i=niftiinfo('picture.nii');
Va = niftiread(i);
Vmax = max(Va);
Vmin = min(Va);
Va_prime = ((Va-abs(Vmin))./(Vmax-Vmin))*255;%Normalisation -> valeurs entre 0 et 1
dim = size(Va_prime);%Dimensions du volume
volumeViewer(Va)
[sliceX sliceY sliceZ]=size(Va);
frontal = Va_prime(:,:,sliceZ);
sagittal = reshape(Va_prime(sliceX,:,:),[dim(2) dim(3)]);
horizontal = reshape(Va_prime(:,sliceY,:),[dim(1) dim(3)]);
folder='direction file';
for i=1:sliceY
frontal=uint8(Va(:,:,i));
frontal=imrotate(frontal,90);
basefilename=sprintf('image %d.png',i);
fullfilemane=fullfile(folder,basefilename);
imshow(frontal)
imwrite(frontal, fullfilemane);
end
0 Comments
Accepted Answer
DGM
on 4 Jun 2021
Well here goes. I'm pretty sure the main problems were in the normalization, but there were other issues that could be simplified anyway. I left the unused stuff in there with notes.
% i have no idea what your image is
ni=niftiinfo('brain.nii');
Va = niftiread(ni);
% if Va is 3D, min(Va) will return a 2D array
% since it only minimizes along dim1. is that desired?
%Vmax = max(Va);
%Vmin = min(Va);
% for the global minimum instead (see below why neither are used)
%Vmax = max(Va(:));
%Vmin = min(Va(:));
%Va_prime = ((Va-abs(Vmin))./(Vmax-Vmin))*255; % that's not normalization
Va_prime = mat2gray(Va); % this normalizes independent of image class
%dim = size(Va_prime); % this contains the same information as sliceX/Y/Z
%[sliceX sliceY sliceZ]=size(Va); % so just use them instead
% again, don't really need to use either though
% you're picking only one slice each (the last one)... why?
%frontal = Va_prime(:,:,sliceZ);
%sagittal = reshape(Va_prime(sliceX,:,:),[sliceY sliceZ]);
%horizontal = reshape(Va_prime(:,sliceY,:),[sliceX sliceZ]);
% i don't know the orientation of your image, so i'm guessing which side is which
% but using permute() avoids needing to know array geometry
frontal = Va_prime;
sagittal = permute(Va_prime,[2 3 1]);
horizontal = permute(Va_prime,[1 3 2]);
% just pick which image to output
outpict = frontal;
outpict = rot90(outpict,1); % just rotate the whole thing if necessary
folder='direction file';
for p=1:size(outpict,3) % automatically picks the right number of slices
outslice = outpict(:,:,p);
% picking filenames and stuff
basefilename=sprintf('image %d.png',i);
fullfilemane=fullfile(folder,basefilename);
imwrite(outslice,fullfilemane);
end
% you could put this in the loop, but it slows things way down
% and you'll only see the last slice anyway
imshow(outslice)
You'll have to check the orientation of the saggital/horizontal slicing with respect to your image. You can just swap them if they're flipped.
More Answers (0)
See Also
Categories
Find more on Image Processing Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!