- fullfile(a,b) is smarter than [a, '/', b], because it considers the fileseparators of the platform.
- The names of the variables look confusing: "filenames" is not a list of file names, "a" is meaningless,
- labels = reshape(labels, [length(labels), 1]) can be written as labels = labels(:).
- The condition if subDir == '.' | strcmp(subDir, '..') == 1 can be simplified. Remember that == is the elementwise operation and subDir is a char vector. If subDir is '..', the comparison replied [true, true]. Because if requires a scalar condition, Matlab inserts an all implicitly. Then this is suffcient actually: if subDir == '.', because it catchs '..' also. Writing this explicitly is nicer to read: if all(subDir == '.'). This would trigger also for '...', but this is not an allowed file name under Windows, but maybe under Linux (?). Therefore I prefer the explicit: if strcmp(subDir, '.') || strcmp(subDir, '..')
Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Hi,
I am working on character recogntiion and my images are in gray images.
here in the code on read all images but I am facing error.
function [images, labels, imageFiles] = readDataset(dirPath)
imageFiles = '';
% Get a list of all files and folders in this folder.
files = dir(dirPath);
% Get a logical vector that tells which is a directory.
dirFlags = [files.isdir];
% Extract only those that are directories.
subFolders = files(dirFlags);
% add all image file name
for i = 1 : length(subFolders)
subDir = subFolders(i).name;
if subDir == '.' | strcmp(subDir, '..') == 1
continue;
end
files = dir(strcat(dirPath, '/', subDir));
fileFlags = ~[files.isdir];
files = files(fileFlags);
for j = 1: length(files)
filename = strcat(subDir, '/', files(j).name);
imageFiles = strvcat(imageFiles, filename);
end
end
% shuffle file list
idx = randperm(length(imageFiles));
imageFiles = imageFiles(idx, :);
% read images and labels
images = [];
labels = [];
for j = 1: length(imageFiles)
filenames = strsplit(imageFiles(j, :), '/');
a = str2num(filenames{1});
labels = [labels, str2num(filenames{1})];
img = imread(strcat(dirPath, '/', imageFiles(j, :)));
%img = rgb2gray(img);
img = imresize(img, [32, 32]);
img = double(img) / 255;
images(:, :, length(labels)) = img;
end
labels = reshape(labels, [length(labels), 1]);
images = reshape(images, [32, 32, 1, length(labels)]);
end
0 Commenti
Risposta accettata
Jan
il 18 Mar 2019
Modificato: Jan
il 18 Mar 2019
Use the debugger to find the cause of the problem. Type e.g. this in the command window:
dbstop if error
Run the code again. When Matlab stops at the error, check the currently used variables:
size(img)
size(images)
Maybe the rgb2gray is important?
Some hints:
Prefer to work with cell strings (or strings) instead of CHAR matrices. Replace:
imageFiles = '';
...
files = dir(strcat(dirPath, '/', subDir));
fileFlags = ~[files.isdir];
files = files(fileFlags);
for j = 1: length(files)
filename = strcat(subDir, '/', files(j).name);
imageFiles = strvcat(imageFiles, filename);
end
by
imageFiles = {};
...
files = dir(fullfile(dirPath, subDir));
fileNames = {files(~[files.isdir]).name};
imageFiles = cat(2, imageFiles, fileNames);
...
% Instead of imageFiles(j, :) use imageFiles{j} afterwards
3 Commenti
Jan
il 18 Mar 2019
I've mentioned already, that you can use the debugger to find the reasons of the errors. I've suggested several improvements of your code. Now you post two different errors. How can we help you?
Più risposte (1)
KSSV
il 18 Mar 2019
function [images, labels, imageFiles] = readDataset(dirPath)
imageFiles = '';
% Get a list of all files and folders in this folder.
files = dir(dirPath);
% Get a logical vector that tells which is a directory.
dirFlags = [files.isdir];
% Extract only those that are directories.
subFolders = files(dirFlags);
% add all image file name
for i = 1 : length(subFolders)
subDir = subFolders(i).name;
if subDir == '.' | strcmp(subDir, '..') == 1
continue;
end
files = dir(strcat(dirPath, '/', subDir));
fileFlags = ~[files.isdir];
files = files(fileFlags);
for j = 1: length(files)
filename = strcat(subDir, '/', files(j).name);
imageFiles = strvcat(imageFiles, filename);
end
end
% shuffle file list
idx = randperm(length(imageFiles));
imageFiles = imageFiles(idx, :);
% read images and labels
images = cell(length(imageFiles),1);
labels = [];
for j = 1: length(imageFiles)
filenames = strsplit(imageFiles(j, :), '/');
a = str2num(filenames{1});
labels = [labels, str2num(filenames{1})];
img = imread(strcat(dirPath, '/', imageFiles(j, :)));
%img = rgb2gray(img);
img = imresize(img, [32, 32]);
img = double(img) / 255;
images{j} = img;
end
% labels = reshape(labels, [length(labels), 1]);
% images = reshape(images, [32, 32, 1, length(labels)]);
end
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!