How to matwrite file from 4D to into 3D

1 view (last 30 days)
Dear all,
I have mat file from LiverTs001.mat - LiverTs400.mat . Which is 4D. then have different dimension each. (can get the images thru this link https://drive.google.com/file/d/1JmAYlLekqRvlq4qU5piyQXfBfsZl8G8u/view?usp=sharing )
Then I want to convert it into 3D.
I tried used thic below coding but error
clc
clear all
close all
volReader = @(x) matRead(x);
% baca data banyak slice
% Get a list of all files in the folder with the desired file name pattern.
myFolder = ('F:\CNN unet code\BrainDataSet\preprocessedDataset\imagesTr');
filePattern = fullfile(myFolder, '*.mat'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for L = 1 : length(theFiles)
baseFileName = theFiles(L).name;
fullFileName = fullfile(theFiles(L).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
liver(:,:,:,:,L) = volReader(fullFileName);
end
frame_i=cropVol(:,:,:,4);

Answers (1)

Walter Roberson
Walter Roberson on 10 Sep 2022
Edited: Walter Roberson on 10 Sep 2022
Which is 4D. then have different dimension each.
You need to take the different dimensions into account.
volReader = @(x) matRead(x);
% baca data banyak slice
% Get a list of all files in the folder with the desired file name pattern.
myFolder = ('F:\CNN unet code\BrainDataSet\preprocessedDataset\imagesTr');
filePattern = fullfile(myFolder, '*.mat'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
numfiles = length(theFiles);
idx4 = repmat({':'}, 1, 4);
idx5 = repmat({':'}, 1, 5);
for L = 1 : numfiles
baseFileName = theFiles(L).name;
fullFileName = fullfile(theFiles(L).folder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
% Now do whatever you want with this file name,
% such as reading it in as an image array with imread()
thisdata = double(volReader(fullFileName));
if L == 1
liver = thisdata;
liver(:,:,:,:,2:numfiles) = nan;
else
for dim = 1 : 4
if size(thisdata, dim) < size(liver,dim)
idxpat = idx4; idxpat{dim} = size(thisdata,dim)+1: size(liver,dim);
thisdata(idxpat{:}) = nan;
elseif size(thisdata, dim) > size(liver,dim)
idxpat = idx5; idxpat{dim} = size(liver,dim)+1 : size(thisdata,dim);
liver(idxpat{:}) = nan;
end
end
liver(:,:,:,:,L) = thisdata;
end
end
What this code is doing is reading in one file at a time, and comparing the dimensions to the dimensions of the existing data, and doing nan padding of each dimension so that the two match up. Either the new data or the old data might be larger so we have to be prepared to grow either of them.
The code could be simpler if it were only one specific dimension that could be a different size; it could also be simpler if you could do zero padding instead of nan padding.
Note: the incoming data is always converted to double precision, in order to permit NaN to be stored. For your purposes it might be acceptable to convert it to single precision instead of double precision. This data type conversion would not be necessary if zero padding could be used instead of nan padding.
The nan padding makes it clear where the boundaries of each sub-volume are, since each could be smaller rows, columns, panes, or hyperpanes.
A completely different approach that would not require any padding, would be to read into a cell array.
  3 Comments
mohd akmal masud
mohd akmal masud on 11 Sep 2022
its ok sir.
I convert all my data one by one.
first I run this code,
clc
clear all
close all
then i drag the 4D data into command window
then I evaluate this code.
frame_i=cropVol(:,:,:,4);
then, in workspace, I right click on my 3D data, then save as matfile into new folder.
Its taken time :)

Sign in to comment.

Categories

Find more on Convert Image Type 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!

Translated by