Saving Images in a Cell Array

16 visualizzazioni (ultimi 30 giorni)
Nour Aburaed
Nour Aburaed il 28 Ago 2018
Commentato: Stephen23 il 8 Set 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!

Risposta accettata

Stephen23
Stephen23 il 28 Ago 2018
Modificato: Stephen23 il 28 Ago 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 Commento
Nour Aburaed
Nour Aburaed il 29 Ago 2018
Modificato: Nour Aburaed il 29 Ago 2018
Actually, not converting "row" and "col" to num was my only mistake! And also not taking ".jpg" into account in col. After I had fixed this, my code started working just fine. I don't know how I missed something so trivial. I'm also surprised that Matlab didn't give me an error for trying to use char vectors for indexing. Thank you for your help!!

Accedi per commentare.

Più risposte (1)

Yuvaraj Venkataswamy
Yuvaraj Venkataswamy il 28 Ago 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 Commenti
Zoe Millar
Zoe Millar il 8 Set 2019
Thanks so much! Why do you need to use the curly brackets when writing I{i} = imread.....
Stephen23
Stephen23 il 8 Set 2019
"Why do you need to use the curly brackets when writing I{i} = imread..."
You don't "need" to.
  1. 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.
  2. 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).
  3. 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
  4. For newer releases you could use an image datastore:https://www.mathworks.com/help/matlab/ref/matlab.io.datastore.imagedatastore.html

Accedi per commentare.

Categorie

Scopri di più su Images in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by