<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264871</link>
    <title>MATLAB Central Newsreader - Creating an image from a binary mask file using RESHAPE</title>
    <description>Feed for thread: Creating an image from a binary mask file using RESHAPE</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Tue, 03 Nov 2009 19:33:03 -0500</pubDate>
      <title>Creating an image from a binary mask file using RESHAPE</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264871#691815</link>
      <author>Arvind </author>
      <description>Hello superior MATLABers...&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;My problem is in dealing with creating an image from a mask file (from drawing regions of interests- ROIs).  I successfully read the ROI image data from a 192x256x20 file (image is 192x256, with 20 slices) using fopen and fread; this dumps the values into a large 1 dimensional vector.  &lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;I attempt to reshape the data back into its 192x256x20 form by simply writing:&lt;br&gt;
reshape(imagevector,192,256,20)&lt;br&gt;
and my image looks the data is not lining up and is skewed (there should only be 4 shapes; further below I provide an image that's close to the ROI that I actually drew).  &lt;br&gt;
&lt;br&gt;
Here's the output from the first line of code (looks like a mess):&lt;br&gt;
&lt;a href=&quot;http://img38.imageshack.us/i/rowsandcolumnscorrect.jpg/&quot;&gt;http://img38.imageshack.us/i/rowsandcolumnscorrect.jpg/&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
&amp;nbsp;&amp;nbsp;By accident, I accidentally wrote this previously (I swapped the rows and columns):&lt;br&gt;
reshape(imagevector,256,192,20), and I get an image that has the shapes at least as solid objects, but the image is flipped on the y=-x axis (I guess that goes along with putting in wrong dimensions).   Here's what that image looks like:&lt;br&gt;
&lt;br&gt;
(objects flipped around, but not looking like a total mess)&lt;br&gt;
&lt;a href=&quot;http://img266.imageshack.us/i/rowsandcolumnsflipped.jpg/&quot;&gt;http://img266.imageshack.us/i/rowsandcolumnsflipped.jpg/&lt;/a&gt;&lt;br&gt;
&lt;br&gt;
I'd appreciate any insight to what's going on!  I have a feeling I'd have to take a close look at how exactly how my image viewer is outputting its ROI image file...&lt;br&gt;
&lt;br&gt;
Thanks!&lt;br&gt;
-Arvind</description>
    </item>
    <item>
      <pubDate>Tue, 03 Nov 2009 19:44:27 -0500</pubDate>
      <title>Re: Creating an image from a binary mask file using RESHAPE</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264871#691817</link>
      <author>ImageAnalyst</author>
      <description>Arvind:&lt;br&gt;
I don't know.  All I can say is to not use reshape.  Just read it out&lt;br&gt;
slice by slice, like I do.  Here is a snippet of my code where I'm&lt;br&gt;
reading slices into a 3D array using fread():&lt;br&gt;
[snip]&lt;br&gt;
	if(stHeader.BytesPerVoxel == 1)&lt;br&gt;
		dataLengthString = '*uint8';	% You need the *, otherwise fread&lt;br&gt;
returns doubles.&lt;br&gt;
		% Initialize a 3D data array.&lt;br&gt;
		data3D = uint8(zeros([subsampledXSize, subsampledYSize,&lt;br&gt;
subsampledZSize]));&lt;br&gt;
&lt;br&gt;
	elseif(stHeader.BytesPerVoxel == 2)&lt;br&gt;
		dataLengthString = '*uint16';	% You need the *, otherwise fread&lt;br&gt;
returns doubles.&lt;br&gt;
		% Initialize a 3D data array.&lt;br&gt;
		data3D = uint16(zeros([subsampledXSize, subsampledYSize,&lt;br&gt;
subsampledZSize]));&lt;br&gt;
&lt;br&gt;
	else&lt;br&gt;
		error('Unsupported BytesPerVoxel %d', stHeader.BytesPerVoxel);&lt;br&gt;
	end&lt;br&gt;
	bytesPerVoxel = stHeader.BytesPerVoxel;&lt;br&gt;
&lt;br&gt;
[snip]&lt;br&gt;
	% Read in data slice by slice to avoid out of memory error.&lt;br&gt;
	% We'll build up the 3D array slice by slice along the Z direction.&lt;br&gt;
	sliceNumber = 1;&lt;br&gt;
	for z = stValidParameters.ZStart : stValidParameters.Subsample :&lt;br&gt;
stValidParameters.ZEnd&lt;br&gt;
		% Read in slice z from input image and put into slice sliceNumber of&lt;br&gt;
output image.&lt;br&gt;
		% Reads from the current file pointer position.&lt;br&gt;
		% Note: fread requires that x_size and y_size be doubles.&lt;br&gt;
		oneFullSlice = fread(fileHandle, [x_size, y_size],&lt;br&gt;
dataLengthString);&lt;br&gt;
		if needToCropOrSubsample == 1&lt;br&gt;
			% Crop it and subsample it.&lt;br&gt;
			croppedSlice = oneFullSlice&lt;br&gt;
(stValidParameters.XStart:stValidParameters.Subsample:stValidParameters.XEnd,&lt;br&gt;
stValidParameters.YStart:stValidParameters.Subsample:stValidParameters.YEnd);&lt;br&gt;
			% Assign it, but don't transpose it like in some other formats.&lt;br&gt;
			data3D(:, :, sliceNumber) = croppedSlice;&lt;br&gt;
		else&lt;br&gt;
			% Take the full slice, (not transposed like in some other formats).&lt;br&gt;
			data3D(:, :, sliceNumber) = oneFullSlice;&lt;br&gt;
		end&lt;br&gt;
		%disp(['Read in slice ' num2str(z) ' of input, slice ' num2str&lt;br&gt;
(sliceNumber) ' of output']);&lt;br&gt;
		% Skip the next slices if we are subsampling.&lt;br&gt;
		% For example, if we just read slice 1 and the subsampling is 3, the&lt;br&gt;
next slice we should&lt;br&gt;
		% read is 4, so we need to skip slices 2 and 3 (skip subsampling-1&lt;br&gt;
slices).&lt;br&gt;
		if stValidParameters.Subsample &amp;gt; 1&lt;br&gt;
			% Calculate how many bytes to skip.&lt;br&gt;
			bytesToSkip = int32(x_size * y_size * bytesPerVoxel *&lt;br&gt;
(stValidParameters.Subsample - 1));&lt;br&gt;
			% Skip that many past the current position, which is at the end of&lt;br&gt;
the slice we just read.&lt;br&gt;
			fseek(fileHandle, bytesToSkip, 'cof');&lt;br&gt;
		end&lt;br&gt;
		% Increment the slice we are on in the output array.&lt;br&gt;
		sliceNumber = sliceNumber + 1;&lt;br&gt;
	end&lt;br&gt;
&lt;br&gt;
	% Close the file.&lt;br&gt;
	fclose(fileHandle);&lt;br&gt;
&lt;br&gt;
Pay particular attention to the line:&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;oneFullSlice = fread(fileHandle, [x_size, y_size],&lt;br&gt;
dataLengthString);&lt;br&gt;
This is what allows you to read it in as a 2D array and stuff it&lt;br&gt;
directly into your 3D array -- no reshape needed.  It's pretty fast&lt;br&gt;
too.&lt;br&gt;
Good luck,&lt;br&gt;
ImageAnalyst</description>
    </item>
    <item>
      <pubDate>Tue, 03 Nov 2009 19:46:01 -0500</pubDate>
      <title>Re: Creating an image from a binary mask file using RESHAPE</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264871#691818</link>
      <author>Arvind </author>
      <description>Whoops, I swapped my rows and columns around.  The correct format is 256x192x20, which explains why when I swapped the r/c around, the image looked wider than taller.  Hope that helps make the problem sound less confusing. </description>
    </item>
    <item>
      <pubDate>Tue, 03 Nov 2009 19:51:01 -0500</pubDate>
      <title>Re: Creating an image from a binary mask file using RESHAPE</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/264871#691821</link>
      <author>Arvind </author>
      <description>Last edit post, I promise :P&lt;br&gt;
&lt;br&gt;
I forgot to mention I have a workaround just by doing the wrong reshape command when I swapped the rows and columns:&lt;br&gt;
x=reshape(vector,192,256,20)&lt;br&gt;
&lt;br&gt;
but since it was flipped on the y=-x axis, I just did a 'rot90' followed by a 'flipud'...this works (ie, if I plot it using IMSHOW, I see the exact ROI I drew in my image viewer)...but I'm still not sure what's going on with reshape. </description>
    </item>
  </channel>
</rss>

