Thread Subject: Creating an image from a binary mask file using RESHAPE

Subject: Creating an image from a binary mask file using RESHAPE

From: Arvind

Date: 3 Nov, 2009 19:33:03

Message: 1 of 4

Hello superior MATLABers...

    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.

    I attempt to reshape the data back into its 192x256x20 form by simply writing:
reshape(imagevector,192,256,20)
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).

Here's the output from the first line of code (looks like a mess):
http://img38.imageshack.us/i/rowsandcolumnscorrect.jpg/

  By accident, I accidentally wrote this previously (I swapped the rows and columns):
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:

(objects flipped around, but not looking like a total mess)
http://img266.imageshack.us/i/rowsandcolumnsflipped.jpg/

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...

Thanks!
-Arvind

Subject: Creating an image from a binary mask file using RESHAPE

From: ImageAnalyst

Date: 3 Nov, 2009 19:44:27

Message: 2 of 4

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

Subject: Creating an image from a binary mask file using RESHAPE

From: Arvind

Date: 3 Nov, 2009 19:46:01

Message: 3 of 4

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.

Subject: Creating an image from a binary mask file using RESHAPE

From: Arvind

Date: 3 Nov, 2009 19:51:01

Message: 4 of 4

Last edit post, I promise :P

I forgot to mention I have a workaround just by doing the wrong reshape command when I swapped the rows and columns:
x=reshape(vector,192,256,20)

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.

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com