How to create empty matrix in matlab?
186 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I need to create an empty matrix, and I have 100 images . I want to store these images in this matrix by nested loop (i,j) ,So that is matrix(0,0) is the first image ,and the second image is matrix(0,1) ,......, the final image is matrix(10,10) .
0 Commenti
Risposte (2)
Adam
il 14 Ott 2019
myMatrix = cell( 10 );
would create a 10x10 cell array. If all your images are the same size though you'd be better off with them in a numeric array.
Also, note that Matlab indexes from 1 so (0,0) would not be a valid index.
2 Commenti
Stephen23
il 14 Ott 2019
"If all your images are the same size though you'd be better off with them in a numeric array."
Storing images in a cell array has a few advantages over one numeric array:
- non-contiguous memory usage,
- simpler indexing,
- allows for different sizes.
Image Analyst
il 14 Ott 2019
Try making a 3-D matrix
allImages = zeros(64, 64, 100, 'uint8'); % or whatever class they are.
for slice = 1 : 100
thisSlice = % ...however you get it - the 64x64 matrix - like imread() or whatever.....
allImages(:,:,slice) = thisSlice;
end
2 Commenti
Image Analyst
il 15 Ott 2019
I don't get that. I get various sizes of 60 and 61. Anyway, 643 is not an integer multiple of 64, so what sizes are you expecting for all of them?
Plus you switched the order in imcrop. The x/columns/width comes first, not y/rows/height: imcrop(yourImage, [xLeftColumn, yTopRow, width, height]);
Plus, your x and y are zero sometimes, and evidently imcrop() does not throw an error, it just makes the cropped image one row or column smaller.
Try this code, if you want to use an inefficient cell array instead of the more efficient 3-D array that I suggested.
fullFileName = 'C:\Users\user\Desktop\D.gif'
currentImage = imread(fullFileName);
imresize(currentImage, [643, 643]);
[rows, columns, numberOfColorChannels]=size( currentImage );
height=fix(rows/10);
width=fix(columns/10);
arrayCells=cell(10);
for i=1:10
for j=1:10
thisCol = (j-1)*height; % x
thisRow = (i-1)*width; % y
fprintf('Trying to crop image Block {%d, %d} at row %d, column %d\n', i, j, thisRow, thisCol);
arrayCells{i,j} = imcrop(currentImage,[thisCol, thisRow, width, height]);
[rows, columns, ~] = size(arrayCells{i,j});
fprintf(' Block {%d, %d} has %d rows, and %d columns\n', i, j, rows, columns);
end
end
% for k=1:numel(arrayCells)
% filename = sprintf('D%d.gif', k);%<--- lower case k, instead of upper case K
% imwrite(arrayCells{k}, filename);
% end
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!