How to matwrite file from 4D to into 3D

4 ビュー (過去 30 日間)
mohd akmal masud
mohd akmal masud 2022 年 9 月 10 日
コメント済み: mohd akmal masud 2022 年 9 月 11 日
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);

回答 (1 件)

Walter Roberson
Walter Roberson 2022 年 9 月 10 日
編集済み: Walter Roberson 2022 年 9 月 10 日
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 件のコメント
Walter Roberson
Walter Roberson 2022 年 9 月 11 日
編集済み: Walter Roberson 2022 年 9 月 11 日
Your code is structured to read in all of the files first before converting to 3d (and the method of converting to 3d is not present in the code).
In order to read the files all in first, the total size of the files needs to fit in memory.
Your first file is about 24 megabytes times the number of bytes per entry. The multiplier would be 1 for uint8, and 8 for double.
You are trying to create a single array with the file contents, using the file number as the 5th dimension. That requires that all files have the same size as each other in dimensions 1 to 4. 24 megabytes times 400 files times 8 bytes per entry gives about 72 gigabytes.
If your data is uint8 then the size could be cut down, but you need a different strategy to mark the boundaries of each file that is read in, since you indicate that they are different sizes.
mohd akmal masud
mohd akmal masud 2022 年 9 月 11 日
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 :)

サインインしてコメントする。

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by