How to create empty matrix in matlab?

206 visualizaciones (últimos 30 días)
ahmad Al sarairah
ahmad Al sarairah el 14 de Oct. de 2019
Comentada: Image Analyst el 15 de Oct. de 2019
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) .

Respuestas (2)

Adam
Adam el 14 de Oct. de 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 comentarios
Stephen23
Stephen23 el 14 de Oct. de 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.
ahmad Al sarairah
ahmad Al sarairah el 14 de Oct. de 2019
Editada: ahmad Al sarairah el 14 de Oct. de 2019
Yes , all my images are the same size 64x64. I want a code to do this task.

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 14 de Oct. de 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 comentarios
ahmad Al sarairah
ahmad Al sarairah el 15 de Oct. de 2019
Editada: Image Analyst el 15 de Oct. de 2019
I have used the code below:
currentimage = imread('C:\Users\user\Desktop\D.gif');
[row,col,~]=size( currentimage );
height=fix(row/10);
width=fix(col/10);
arrayCells=cell(10);
for i=1:10
for j=1:10
arrayCells{i,j}=imcrop(currentimage,[((j-1)*height) ((i-1)*width) height width]);
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
In result, I got 100 blocks successfully, and the original image is 643x643 ,but only the first block is 64x64 ,while the remaining 99 block is 65x65 or 64x65 or 65x64 . I want to know what is the reason for this?
Image Analyst
Image Analyst el 15 de Oct. de 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

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by