|
Arvind:
I don't know. All I can say is to not use reshape. Just read it out
slice by slice, like I do. Here is a snippet of my code where I'm
reading slices into a 3D array using fread():
[snip]
if(stHeader.BytesPerVoxel == 1)
dataLengthString = '*uint8'; % You need the *, otherwise fread
returns doubles.
% Initialize a 3D data array.
data3D = uint8(zeros([subsampledXSize, subsampledYSize,
subsampledZSize]));
elseif(stHeader.BytesPerVoxel == 2)
dataLengthString = '*uint16'; % You need the *, otherwise fread
returns doubles.
% Initialize a 3D data array.
data3D = uint16(zeros([subsampledXSize, subsampledYSize,
subsampledZSize]));
else
error('Unsupported BytesPerVoxel %d', stHeader.BytesPerVoxel);
end
bytesPerVoxel = stHeader.BytesPerVoxel;
[snip]
% Read in data slice by slice to avoid out of memory error.
% We'll build up the 3D array slice by slice along the Z direction.
sliceNumber = 1;
for z = stValidParameters.ZStart : stValidParameters.Subsample :
stValidParameters.ZEnd
% Read in slice z from input image and put into slice sliceNumber of
output image.
% Reads from the current file pointer position.
% Note: fread requires that x_size and y_size be doubles.
oneFullSlice = fread(fileHandle, [x_size, y_size],
dataLengthString);
if needToCropOrSubsample == 1
% Crop it and subsample it.
croppedSlice = oneFullSlice
(stValidParameters.XStart:stValidParameters.Subsample:stValidParameters.XEnd,
stValidParameters.YStart:stValidParameters.Subsample:stValidParameters.YEnd);
% Assign it, but don't transpose it like in some other formats.
data3D(:, :, sliceNumber) = croppedSlice;
else
% Take the full slice, (not transposed like in some other formats).
data3D(:, :, sliceNumber) = oneFullSlice;
end
%disp(['Read in slice ' num2str(z) ' of input, slice ' num2str
(sliceNumber) ' of output']);
% Skip the next slices if we are subsampling.
% For example, if we just read slice 1 and the subsampling is 3, the
next slice we should
% read is 4, so we need to skip slices 2 and 3 (skip subsampling-1
slices).
if stValidParameters.Subsample > 1
% Calculate how many bytes to skip.
bytesToSkip = int32(x_size * y_size * bytesPerVoxel *
(stValidParameters.Subsample - 1));
% Skip that many past the current position, which is at the end of
the slice we just read.
fseek(fileHandle, bytesToSkip, 'cof');
end
% Increment the slice we are on in the output array.
sliceNumber = sliceNumber + 1;
end
% Close the file.
fclose(fileHandle);
Pay particular attention to the line:
oneFullSlice = fread(fileHandle, [x_size, y_size],
dataLengthString);
This is what allows you to read it in as a 2D array and stuff it
directly into your 3D array -- no reshape needed. It's pretty fast
too.
Good luck,
ImageAnalyst
|