Path: news.mathworks.com!newsfeed-00.mathworks.com!news.kjsl.com!news.glorb.com!news2.glorb.com!postnews.google.com!m26g2000yqb.googlegroups.com!not-for-mail
From: ImageAnalyst <imageanalyst@mailinator.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Creating an image from a binary mask file using RESHAPE
Date: Tue, 3 Nov 2009 11:44:27 -0800 (PST)
Organization: http://groups.google.com
Lines: 78
Message-ID: <4294a472-eefb-449a-a92b-9f5c4166e063@m26g2000yqb.googlegroups.com>
References: <hcq0hf$r5g$1@fred.mathworks.com>
NNTP-Posting-Host: 192.44.136.113
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
X-Trace: posting.google.com 1257277467 19131 127.0.0.1 (3 Nov 2009 19:44:27 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Tue, 3 Nov 2009 19:44:27 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: m26g2000yqb.googlegroups.com; posting-host=192.44.136.113; 
	posting-account=0rLUzAkAAABojYSRC64DkTbtiSCX77HH
User-Agent: G2/1.0
X-HTTP-Via: 1.1 bdci2px (NetCache NetApp/6.0.7)
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; 
	CyberSafe-IWA-Enable; .NET CLR 1.1.4322; .NET CLR 2.0.50727; MS-RTC LM 8; 
	.NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 
	3.5.30729),gzip(gfe),gzip(gfe)
Xref: news.mathworks.com comp.soft-sys.matlab:582147


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