I have a 4D image of size 60 x 80 x 12 x 350, i.e. it is a 3D image where each voxel has a time series (of 350).
Now I want to use mat2cell to divide the 3D image into cubes of dimension k*k*k. Each voxel in the cube is a vector of size 350 (the time series).
I think I could do it with mat2cell but I don't know how exactly. Each cell should contain in the end a 3D block of the image where each voxel of the block is a vector of size 350.
Assuming your 4D matrix is called M. You need to have vectors whose elements sum to size(M, i) where i = 1:4. Assuming k has some value, I tried both 4 (because it's a common factor of the sizes you specified) and 3 (because it's not).
k = 3;
MPrime = mat2cell(M, ...
[k*ones(1, floor(size(M,1)/k)), mod(size(M,1), k)], ...
[k*ones(1, floor(size(M,2)/k)), mod(size(M,2), k)], ...
[k*ones(1, floor(size(M,3)/k)), mod(size(M,3), k)], ...
ones(1, size(M,4)));
Related
The function to perform an N-dimensional convolution of arrays A and B in matlab is shown below:
C = convn(A,B) % returns the N-dimensional convolution of arrays A and B.
I am interested in a 3-D convolution with a Gaussian filter.
If A is a 3 x 5 x 6 matrix, what do the dimensions of B have to be?
The dimensions of B can be anything you want. There is no set restriction in terms of size. For the Gaussian filter, it can be 1D, 2D or 3D. In 1D, what will happen is that each row gets filtered independently. In 2D, what will happen is that each slice gets filtered independently. Finally, in 3D you will be doing what is expected in 3D convolution. I am assuming you would like a full 3D convolution, not just 1D or 2D.
You may be interested in the output size of convn. If you refer to the documentation, given the two N dimensional matrices, for each dimension k of the output and if nak is the size of dimension k for the matrix A and nbk is the size of dimension k for matrix B, the size of dimension of the output matrix C or nck is such that:
nck = max([nak + nbk - 1, nak, nbk])
nak + nbk - 1 is straight from convolution theory. The final output size of a dimension is simply the sum of the two sizes in dimension k subtracted by 1. However should this value be smaller than either of nak or nbk, we need to make sure that the output size is compatible so that any of the input matrices can fit in the final output. This is why you have the final output size and bounded by both A and B.
To make this easier, you can set the size of the filter guided by the standard deviation of the distribution. I would like to refer you to my previous Stack Overflow post: By which measures should I set the size of my Gaussian filter in MATLAB?
This determines what the output size of a Gaussian filter should be given a standard deviation.
In 2D, the dimensions of the filter are N x N, such that N = ceil(6*sigma + 1) with sigma being the desired standard deviation. Therefore, you would allocate a 3D matrix of size N x N x N with N = ceil(6*sigma + 1);.
Therefore, the code you would want to use to create a 3D Gaussian filter would be something like this:
% Example input
A = rand(3, 5, 6);
sigma = 0.5; % Example
% Find size of Gaussian filter
N = ceil(6*sigma + 1);
% Define grid of centered coordinates of size N x N x N
[X, Y, Z] = meshgrid(-N/2 : N/2);
% Compute Gaussian filter - note normalization step
B = exp(-(X.^2 + Y.^2 + Z.^2) / (2.0*sigma^2));
B = B / sum(B(:));
% Convolve
C = convn(A, B);
One final note is that if the filter you provide has any of its dimensions that are beyond the size of the input matrix A, you will get a matrix using the constraints of each nck value, but then the border elements will be zeroed due to zero-padding.
I have 2D matrixs of dimensions 400 x 500,each of these matrixs show an image. my process contain 2 steps:
1) I have to partition these images (split matrix to equal sized sub-matrices)
2) I have to save each of these split in one matrix
first step is done and dimention of matrix change from 2D-->3D (the last index shows index of splits)
now for the step 2 I have 100 images and I want to have matrix with 4 dimensions which the last index show the number of images
sample : for accessing split 3 of image 40 : [:,:,3,40]
I already try to using permut and reshape but not successful
here is my code
nCol = 10;
nRow = 4;
K=dir(p);
Len=length(K);
for i=3:Len
x1=imread(strcat(p,'\',K(i).name));
[m,n,d1]=size(x1);
if d1==1
x=double(x1);
else
x=double(rgb2gray(x1));
end
x=imresize(x,NN);
%% determined width and height of divided matrix %%%%%%%%%%%%%%%%%%%%%%%%%%
m = size(x,1)/nRow;
n = size(x,2)/nCol;
T = permute(reshape(permute(reshape(x, size(x, 1), n, []), [2 1 3]), n, m, []), [2 1 3]);
Im=[Im T(:,:,:,i-2)];
end
any idea would be appreciated.
reshape picks elements in column major ordering so you might have to write convoluted code to get it to work. Rather than going the way of using permute and reshape to create 4D matrices and potentially running into an out of memory issue I would advice the use of mat2cell to split your matrix into a cell array because mat2cell splits a matrix like you would want to split an image.
Here I show an example with an image
RGB = imread('peppers.png');
x = rgb2gray(RGB); % x is a 384 x 512 matrix, we want to split in 3 rows and 2 columns
x2 = mat2cell(x,384*ones(3,1)/3,512*ones(2,1)/2); % 2D cell array, each cell holds a part of the image
imshow(x2{1,1}) % Top left part of the image
You could loop over all your images and create a 3D cell array where each layer in the array represents each image split into pieces. I would suggest to preallocate you array and assign the matrix in the correct layer within the loop rather than incrementally increasing the size of your matrix.
Also there seems to be an Image processing toolbox specific function to do what you are trying to : Check this : How to divide an image into blocks in MATLAB?
I have a n channel image and I have a 100x2 matrix of points (in my case n is 20 but perhaps it is more clear to think of this as a 3 channel image). I need to sample the image at each point and get an nx100 array of these image points.
I know how to do this with a for loop:
for j = 1:100
samples(j,:) = image(points(j,1),points(j,2),:);
end
How would I vectorize this? I have tried
samples = image(points);
but this gives 200 samples of 20 channels. And if I try
samples = image(points,:);
this gives me 200 samples of 4800 channels. Even
samples = image(points(:,1),points(:,2));
gives me 100 x 100 samples of 20 (one for each possible combination of x in X and y in Y)
A concise way to do this would be to reshape your image so that you force your image that was [nRows, nCols, nChannels] to be [nRows*nCols, nChannels]. Then you can convert your points array into a linear index (using sub2ind) which will correspond to the new "combined" row index. Then to grab all channels, you can simply use the colon operator (:) for the second dimension which now represents the channels.
% Determine the new row index that will correspond to each point after we reshape it
sz = size(image);
inds = sub2ind(sz([1, 2]), points(:,2), points(:,1));
% Do the reshaping (i.e. flatten the first two dimensions)
reshaped_image = reshape(image, [], size(image, 3));
% Grab the pixels (rows) that we care about for all channels
newimage = reshaped_image(inds, :);
size(newimage)
100 20
Now you have the image sampled at the points you wanted for all channels.
i have R 3d matrix,n varies from 1:100.
I have generated 20 such R matrix.
Now i have to average each R for this 20 experiment.
so that I'll get n,100 avg matrix.
How to average this 20, n Matrix?
I want to add(avg) all 20 times generated R for each n .I must have avg 100 R matrix .
Assuming you actually have a 3D matrix R, it is very easy to average:
R = rand(3,4,5); %Suppose this is your matrix
Now you just need to pick the dimension you want to average in:
mean(R,1) %First dimension
mean(R,2) %Second dimension
mean(R,3) %Third dimension
If you are not sure which one you need, just check the size of all three.
I want to take a large matrix and take the average of all 5x5 grids in it.
The matrix is 245x85x1255.I reshaped the matrix into a 5x4165x1255 size (the z dimension is not that important) and I want to take the average of elements 1:5, 5:10, 10:15 etc in each row. And then, with the resulting matrix, I want to average the five columns. Then I'll resize it back to it's original shape (but smaller of course).
I don't have to do it this way. I just need to take a 5x5 grid and average all the points in it. Then I take the next 5x5 grid next to it and average all those points.
Here's how I did it for the first 5x5 grid:
A = data_SpecificArea(:,1:5,:);
B = mean(A,2);
C = mean(B,1);
** Here's the working code using blockproc
% Change dataAll_SpecificArea to a 1x1 degree grid (5x5 block averaging)
fun = #(block_struct) mean(block_struct.data);
A = blockproc(dataAll_SpecificArea,[5 1],fun); % Size goes from 245x85x1255 to 49x85x1255
B = blockproc(A,[1 5],fun); % Size is 49x17x1255
You can use blockproc for that. For example,
fun = #(block_struct) mean(block_struct.data);
new_matrix = blockproc(old_matrix,[5 5],fun);