Saving Images in a Cell Array
24 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Nour Aburaed
el 28 de Ag. de 2018
Comentada: Stephen23
el 8 de Sept. de 2019
I have images (RGB) in a folder named "1-1.jpg", "1-2.jpg"...."1-23.jpg", "2-1.jpg", "2-2.jpg" etc. up to "23-23.jpg"
I want to retrieve these images and concatenate them in a cell in that order in order to form one full image. Meaning, I want to put image "1-1" in location "1-1" of the cell, and "2-1" in location "2-1" of the cell, etc. I am frankly not an expert in Matlab, so I am not sure if it is even possible to do it this way, but that is my current approach. If it is not possible to do this, can you advise some other way?
Also, this is my code
for ii=1:nfiles
imagename = imagefiles(ii).name;
position = split(imagename,'-'); %cell array that holds row and columns numbers
image = imread(imagename);
[rows,cols] = size(rgb2gray(image));
row = position{1,1};
col = position{2,1};
full{row,col} = image; %getting an error here
block_position=strcat('(',int2str(row),',', int2str(col), ')');
positions{row,col} = block_position;
imshow (image);
end
I'm getting an error for the cell array assignment, which says "Expected one output from a curly brace or dot indexing expression, but there were 5 results". I would appreciate it if someone can help me to fix this error or approach the problem differently. Thanks!
0 comentarios
Respuesta aceptada
Stephen23
el 28 de Ag. de 2018
Editada: Stephen23
el 28 de Ag. de 2018
There are several problems, such as that you have not converted the row and col character data to numeric (indexing must be numeric or logical), and that you have not accounted for the .jpg file extension in the col string. I suggest that you review the fundamental data types, to avoid confusion between char vectors representing numbers, and actual numeric data.
Here is one solution:
D = 'directory where the files are';
S = dir(fullfile(D,'*-*.jpg'));
N = {S.name};
M = sscanf(sprintf('%s',N{:}),'%d-%d.jpg',[2,Inf]);
C = cell(max(M,[],2));
for k = 1:numel(N)
I = imread(fullfile(D,N{k}));
C{M(1,k),M(2,k)} = I;
...
end
This code assumes that only the jpg files that you need match the
'*-*.jpg'
pattern.
1 comentario
Más respuestas (1)
Yuvaraj Venkataswamy
el 28 de Ag. de 2018
Hi, first You have to save your input images as "001_001.jpg", "001_002.jpg"...."001_023.jpg", "002_001.jpg", "002_002.jpg" etc. up to "023-023.jpg".
Then you can call it in for loops.
if true
srcFiles = dir('E:\ Folder\*.jpg');
for i = 1 : length(srcFiles)
filename = strcat('E:\New Folder\',srcFiles(i).name);
I{i} = imread(filename);
end
end
3 comentarios
Zoe Millar
el 8 de Sept. de 2019
Thanks so much! Why do you need to use the curly brackets when writing I{i} = imread.....
Stephen23
el 8 de Sept. de 2019
"Why do you need to use the curly brackets when writing I{i} = imread..."
You don't "need" to.
- In many instances it is not possible to store all required images in memory, in which case putting them all into any kind of array will lead to memory errors.
- In some cases it may be possible/advantageous to store all images in one numeric array (which should be preallocated, and all images must have the same size).
- In some cases it is easiest to handle all images as separate numeric arrays, in which case a cell array (using curly braces) is a simple way to achieve that:https://www.mathworks.com/help/matlab/cell-arrays.html But you do not "need" to use a cell array, as MATLAB has other container classes too, e.g.: structures and tables: https://www.mathworks.com/help/matlab/matlab_prog/fundamental-matlab-classes.html
- For newer releases you could use an image datastore:https://www.mathworks.com/help/matlab/ref/matlab.io.datastore.imagedatastore.html
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!