How do i store sequence of 2D matrices into a 3D array in Matlab? - arrays

I have these series of 2D CT images and i have been able to read them into Matlab using "imread". The issue however is that i need the image read-in as a single 3D matrix rather than stack of several 2D matrices. I have been made aware that it is possible to store the number of 2D layers as the 3rd dimension, but i have no idea how to do this as i am still a learner.
The code i have for reading in the 2D stack are as follows:
a = dir('*.tif');
for i = 1: numel(a)
b = imread(a(i).name); %read in the image
b_threshold = graythresh(b); %apply threshold
b_binary = im2bw(b, b_threshold); %binarize image
[m, n] = size(b); %compute the size of the matrix
phi(i) = ((m*n) - sum((b_binary(:))))/(m*n); %compute the fraction of pore pixels in the image
phi(:,i) = phi(i); %store each of the above result
end
I have added just a single image although several of these are needed. Nevertheless, one can easily duplicate the image to create a stack of 2D images. For the code to work, it is however important to rename them in a numerical order.pore_image
Any help/suggestions/ideas is welcomed. Thanks!

You can simply assign along the third dimension using i as your index
stack_of_images(:,:,i) = b_binary

Well, the first advice is try to don't use the variable i and j in matlab because they are reserved (have a look here and here).
After it depends on along which dimension you want to store the 2D images:
if you want to store the images along the first dimension just use this code:
a = dir('*.tif');
for ii = 1: numel(a)
b = imread(a(ii).name); %read in the image
b_threshold = graythresh(b); %apply threshold
b_binary = im2bw(b, b_threshold); %binarize image
[m, n] = size(b); %compute the size of the matrix
phi(ii) = ((m*n) - sum((b_binary(:))))/(m*n); %compute the fraction of pore pixels in the image
phi(:,ii) = phi(ii); %store each of the above result
matrix_3D_images(ii,:,:)=b_binary; %adding a new layer
end
If you want to store the images along other dimensions it is easy to do: just change the posizion of the "pointer" ii:
matrix_3D_images(:,ii,:)=b_binary; or
matrix_3D_images(:,:,ii)=b_binary;

Related

How to create a loop in a cell array and reshape matrix

I have the following script to calculate fluid perturbations.
I am reading in 2D velocity fields and then storing this data in a cell array.
In the present example i have only 4 velocity fields from different time steps, but eventually I will have around 300+
The velocity fields are stored in the cell array and that part works. What i need help with is to create a loop to then reshape the matrices in the cell array and store as a separate matrix.
So far i have
%% Calculating the Perturbation
% The perturbation is calculated by subtracting the average velocity form
% the instantaneous.
% Below are the instantaneous velocity fields in each direction
% u velocity
U = num2cell(u,1);
% v velocity
V = num2cell(v,1);
% w velocity
W = num2cell(w,1);
From here i now want to reshape the matrix in the cell arrays and save as follows:
%% Reshape the velocity vectors into matrices
u1d = reshape(cell2mat(U(1,1)),[nx ny]);
u2d = reshape(cell2mat(U(1,2)),[nx ny]);
u3d = reshape(cell2mat(U(1,3)),[nx ny]);
u4d = reshape(cell2mat(U(1,4)),[nx ny]);
v1d = reshape(cell2mat(V(1,1)),[nx ny]);
v2d = reshape(cell2mat(V(1,2)),[nx ny]);
v3d = reshape(cell2mat(V(1,3)),[nx ny]);
v4d = reshape(cell2mat(V(1,4)),[nx ny]);
w1d = reshape(cell2mat(W(1,1)),[nx ny]);
w2d = reshape(cell2mat(W(1,2)),[nx ny]);
w3d = reshape(cell2mat(W(1,3)),[nx ny]);
w4d = reshape(cell2mat(W(1,4)),[nx ny]);
now this method is very long and i am not sure how to create a loop to reshape the matrix and then rename them as shown above. Individually will take too long and i want to process 300+ files. I would definitely appreciate help with this. I have done it manually as thats were my knowledge goes to.
The reason i want it as u1d, u2d...v1d, v2d .... w1d... is to calculate the two point correlation. So once i get u1d....etc i will subtract it from the average velocity field. Which is another part of the code. I need help to create a loop for this section.
First, some pointers:
cell2mat(U(1,1)) is the same as U{1,1}. You should prefer the latter, it's much more efficient because it doesn't do a function call.
Naming variables u1d, u2d, u3d, ... is usually a bad idea. You already figured you could use a cell array U to store your vectors, you should store these also in a cell array: ud{1}, ud{2}, ud{3}, ...
Given these two points, you can do your thing using a loop:
for ii=1:4
ud{ii} = reshape(U{ii},[nx ny]);
end
However, using a cell array in this case is actually not even necessary, it is just as much work to do indexing like this: U{1,1} as like this: u(:,1). You should prefer the latter, as that is how your data comes in: you avoid the copy needed to generate the cell array.
Given you numeric array u of size nxk, with k vectors (k=4 in your example), and n==nx*ny the number of elements in each, you can do:
ud = reshape(u, nx, ny, k);
which is the same as
ud = reshape(u, nx, ny, []);
In this last version MATLAB figures out what k should be given the size of u.
From here on, you index ud(:,:,1) to get your u1d, etc.
Do note that reshape does not copy data. ud will reference the same data as u until you change one of the two arrays. As soon as you try to change data in one of them, a copy of the full array will be made. This is called "lazy copying". If you don't need to preserve u, I recommend that you don't create a new variable, simply do u = reshape(u...). This will prevent the data from being copied.

Store arrays in index in a nested for loop in matlab

I have 50 images, stored as arrays in a 1x50 cell index called AllImages. Basically I want to make a new index with arrays that contain elements in the same position of the 50 arrays.
I want to see how each pixel in the same spot of the 50 images changes in the 50 images.
Theoretically, I would get an index of arrays with 50 elements each, because I want the first element of each of the 50 arrays in its own array, the second element of each of the 50 arrays in its own array, so on and so forth.
So far, here is my code:
for m = 1:5000 % number of pixels per image
for n = 1:50 % for the 50 images, all the same size
pixels(n) = allImages{n}(m)
end
allpixels{m} = pixels
end
I end up getting a 1x50 cell index for allpixels, even though I want 5000. I'm not sure what I did wrong.
Is there an easier way to do this or fix the code? Thanks so much!
are the images of the same size?
in that case first change them to a matrix using cell2mat
[i,j] = size(allImages{1})
n = numel(allImages)
allImages = cell2mat(allImages);
allImages = reshape(allImages,[i,j,n]);
because now you can just select your pixel. for example:
pixel = squeeze(allImages(1,1,:))
To get them all in a new cell you could permute and reshape your matrix
allImages = permute(allImages ,[3 1 2]);
allImages = reshape(allImages ,[n,i*j]);
pixels = mat2cell(allImages,n,ones([1,i*j]));
But for most mathematical operations it is easier to just keep them as one matrix.
As two rules of thumb in matlab you want to use matrices as much as possible, and avoid for-loops.

MATLAB - copying specific values from one 3d array to another

I need to update image 1 with rgb values from image 2 for specific coordinates.
I have two 2d matrices (im1Cart_toupdate 2x114056 and im2EstCart_tocopyfrom also 2x114056). These contain the ordered x-y pairs for which I want to copy rgb values from image 2 to image 1.
i.e. there are 114,056 pixels where I want to copy colours across.
im1 (440x1370x3) and im2 (240x320x3) are the image arrays. Note im2 is going to be stretched, so some pixels from im2 will appear more than once in im2EstCart_tocopyfrom.
I need an efficient way of doing this, as even with the above image sizes my current implementation is very slow. I had thought that there may be some approach using sub2ind - but am not sure how to do this with 3d arrays.
Here's my current code. It's the for loop that's killing me!
%Create a matrix of all pixel coordinates in im1 (homogenised form)
[im1gridx im1gridy]=meshgrid(1:im1width,1:im1height);
im1Cart = [im1gridx(:) im1gridy(:)]';
im1Hom = [im1Cart; ones(1,numel(im1gridy))];
%transform pixel positions with homography (HEst is a matrix built
%elsewhere) to find where they are in the coordinates of image 2
im2EstHom = HEst*im1Hom;
im2EstCart = im2EstHom(1:2,:)./repmat(im2EstHom(3,:),2,1);
im2EstCart = round(im2EstCart);
%check if the the transformed position is within the boundary of image 2
validCoords = im2EstCart(1,:)>0 & im2EstCart(2,:)>0 & im2EstCart(1,:)<=im2width & im2EstCart(2,:)<=im2height;
im1Cart_toupdate=im1Cart(:,validCoords);
im2EstCart_tocopyfrom=im2EstCart(:,validCoords);
%copy colour from image 2 to image 1 - currently pixel by pixel
%but CAN THIS BE VECTORISED?
for i=1:size(im1Cart_toupdate,2)
im1y=im1Cart_toupdate(1,i);
im1x=im1Cart_toupdate(2,i);
im2y=im2EstCart_tocopyfrom(1,i);
im2x=im2EstCart_tocopyfrom(2,i);
im1(im1y,im1x,:) = im2(im2y,im2x,:);
drawnow
end
Many thanks for any advice!
Approach #1
This would be one vectorized approach using linear indexing with bsxfun -
[m2,n2,r2] = size(im2);
RHS_idx1 = (im2EstCart_tocopyfrom(2,:)-1)*m2 + im2EstCart_tocopyfrom(1,:)
RHS_allidx = bsxfun(#plus,RHS_idx1(:),(0:r2-1)*m2*n2)
[m1,n1,r1] = size(im1);
LHS_idx1 = (im1Cart_toupdate(2,:)-1)*m1 + im1Cart_toupdate(1,:)
LHS_allidx = bsxfun(#plus,LHS_idx1(:),(0:r1-1)*m1*n1)
im1(LHS_allidx) = im2(RHS_allidx)
Approach #2
Here's another approach that reshapes the input 3D array to a 2D array after merging the first two dimensions, then using linear indexing for extracting and setting values and finally reshaping back to its original 3D size, like so -
[m2,n2,r2] = size(im2)
RHS_idx1 = (im2EstCart_tocopyfrom(2,:)-1)*m2 + im2EstCart_tocopyfrom(1,:)
im2r = reshape(im2,[],r2)
[m1,n1,r1] = size(im1)
LHS_idx1 = (im1Cart_toupdate(2,:)-1)*m1 + im1Cart_toupdate(1,:)
im1r = reshape(im1,[],r1)
im1r(LHS_idx1,:) = im2r(RHS_idx1,:)
im1 = reshape(im1r,size(im1));

MATLAB pairwise differences in Nth dimension

Say i have a N-Dimensional matrix A that can be of any size. For example:
A = rand([2,5,3]);
I want to calculate all possible pairwise differences between elements of the matrix, along a given dimension. For example, if i wanted to calculate the differences along dimension 3, a shortcut would be to create a matrix like so:
B = cat(3, A(:,:,2) - A(:,:,1), A(:,:,3) - A(:,:,1), A(:,:,3) - A(:,:,2));
However, i want this to be able to operate along any dimension, with a matrix of any size. So, ideally, i'd like to either create a function that takes in a matrix A and calculates all pairwise differences along dimension DIM, or find a builtin MATLAB function that does the same thing.
The diff function seems like it could be useful, but it only calculates differences between adjacent elements, not all possible differences.
Doing my research on this issue, i have found a couple of posts about getting all possible differences, but most of these are for items in a vector (and ignore the dimensionality issue). Does anyone know of a quick fix?
Specific Dimension Cases
If you don't care about a general solution, for a dim=3 case, it would be as simple as couple lines of code -
dim = 3
idx = fliplr(nchoosek(1:size(A,dim),2))
B = A(:,:,idx(:,1)) - A(:,:,idx(:,2))
You can move around those idx(..) to specific dimension positions, if you happen to know the dimension before-hand. So, let's say dim = 4, then just do -
B = A(:,:,:,idx(:,1)) - A(:,:,:,idx(:,2))
Or let's say dim = 3, but A is a 4D array, then do -
B = A(:,:,idx(:,1),:) - A(:,:,idx(:,2),:)
Generic Case
For a Nth dim case, it seems you need to welcome a party of reshapes and permutes -
function out = pairwise_diff(A,dim)
%// New permuting dimensions
new_permute = [dim setdiff(1:ndims(A),dim)];
%// Permuted A and its 2D reshaped version
A_perm = permute(A,new_permute);
A_perm_2d = reshape(A_perm,size(A,dim),[]);
%// Get pairiwse indices for that dimension
N = size(A,dim);
[Y,X] = find(bsxfun(#gt,[1:N]',[1:N])); %//' OR fliplr(nchoosek(1:size(A,dim),2))
%// Get size of new permuted array that would have the length of
%// first dimension equal to number of such pairwise combinations
sz_A_perm = size(A_perm);
sz_A_perm(1) = numel(Y);
%// Get the paiwise differences; reshape to a multidimensiona array of same
%// number of dimensions as the input array
diff_mat = reshape(A_perm_2d(Y,:) - A_perm_2d(X,:),sz_A_perm);
%// Permute back to original dimension sequence as the final output
[~,return_permute] = sort(new_permute);
out = permute(diff_mat,return_permute);
return
So much for a generalization , huh!

Matlab access vectors from a multi-dimensional array

I have a 4D matrix of size 300x200x3x20 where 300x200 is the size of one video frame, 3 is the number of channels (Red-Green-Blue channels) and 20 is the number of frames.
I want to extract all the color vectors from this matrix and store them in a 2D array of size 3x1,200,000 (300 x 200 x 20 = 1,200,000) where each row represents a component of the RGB color space and each column contain the RGB values of one pixel in the original matrix.
Besides, I want to carry out pixel-wise operations on this data such as extracting visual features but I cannot find a way to effectively access vectors along the third dimension.
How could I efficiently do these, possible without using loops?
Try this code -
IN = your_4D_data;
OUT = reshape(permute(IN,[3 1 2 4]),3,numel(IN)/3);
help reshape says:
B = reshape(A,m,n,p,...) or B = reshape(A,[m n p ...]) returns an n-dimensional array with the same elements as A but reshaped to have the size m-by-n-by-p-by-.... The product of the specified dimensions, m*n*p*..., must be the same as numel(A).
is this what you are looking for?
also, you can adress pixels like this: Matrix(i,j,:,k) which gives you the 3 colorchanels of pixel i,j in frame k.

Resources