Saving Images in a Cell Array

16 次查看(过去 30 天)
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!

采纳的回答

Stephen23
Stephen23 2018-8-28
编辑:Stephen23 2018-8-28
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 个评论
Nour Aburaed
Nour Aburaed 2018-8-29
编辑:Nour Aburaed 2018-8-29
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!!

请先登录,再进行评论。

更多回答(1 个)

Yuvaraj Venkataswamy
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 个评论
Zoe Millar
Zoe Millar 2019-9-8
Thanks so much! Why do you need to use the curly brackets when writing I{i} = imread.....
Stephen23
Stephen23 2019-9-8
"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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by