Saving Images in a Cell Array
Afficher commentaires plus anciens
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!
Réponse acceptée
Plus de réponses (1)
Yuvaraj Venkataswamy
le 28 Août 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 commentaires
Nour Aburaed
le 29 Août 2018
Zoe Millar
le 8 Sep 2019
Thanks so much! Why do you need to use the curly brackets when writing I{i} = imread.....
Stephen23
le 8 Sep 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
Catégories
En savoir plus sur Matrix Indexing dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!