How to vectorize multiple images in 3D matrix?

3 views (last 30 days)
Hello everyone,
I have a 3D matrix of MxNx16 images loaded in matlab. I am trying to transform each image inside that 3D matrix in a vector using (:). But I am unable to write a for loop that can do that for all images. I want my 3D matrix to contain all 16 images, but each of them transformed from MxN matrix to vector(:).
I tried to do it like for a single image but I only get the vector of all images.
Example.
STACKMat = logical(STACK(:)); % This is for a single image
Can anyone help me? Thanks in advance.

Accepted Answer

Adam
Adam on 27 Feb 2015
Edited: Adam on 27 Feb 2015
myImages = cat( 3, ones(10), 2*ones(10), 3*ones(10), 4*ones(10) );
imSize = size( myImages, 1 ) * size( myImages, 2 );
myImagesVec = reshape( myImages, imSize, size( myImages, 3 ) );
should give you what you want. Obviously replacing my made-up 'images' with your own and naming variables sensibly!
If you want them all converted to logical then obviously you can apply that at any point without affecting the reshape.

More Answers (1)

Mario
Mario on 27 Feb 2015
Thank you Adam , that worked!
Now I have some other problem. After I vectorized my images inside my 3D stack (with Adam's help), I want to calculate some parameters from those images as I have two 3D matrix of 16 images called True and Predicted. I want to calculate TP, TN, FP and FN to form a confusion matrix. Below I have those parameters calculated for only one vectorized image.
I need to be able to do this perhaps in a for loop for all images in my 3D stack, thus creating 16 values of TP, TN, FP and FN and to store all values of each of those four parameters in a separate 16x1 matrix (TP(16x1), TN(16x1), FP(16x1) and FN(16x1)).
This is what I have done, but for only one vectorized image:
TP = length(find(add == 2)); % add is a result of adding two vectorized images (Predicted + True)
TN = length(find(add == 0));
FP = length(find(subt == -1)); % subt is a result of subtracting two vectorized images (True-Predicted)
FN = length(find(subt == 1));
I need to find a way to extract these values from all 16 images compared, and put those values in a 16x1 matrix containing all those calculated values.
Does anybody have an idea how can I do that?
I hope that I explained it properly for everybody to understand.
Thanks in advance!

Community Treasure Hunt

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

Start Hunting!