Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

조회 수: 1 (최근 30일)
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

채택된 답변

Jan
Jan 2019년 3월 18일
편집: Jan 2019년 3월 18일
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:
  • 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, '..')
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
neha gautam
neha gautam 2019년 3월 18일
Index in position 1 exceeds array bounds (must not exceed 1).
Error in readDataset (line 23)
imageFiles = imageFiles(idx, :);
Error in run (line 9)
[train_images, train_labels] = readDataset('dataset/train');
this is the error
Jan
Jan 2019년 3월 18일
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?

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

KSSV
KSSV 2019년 3월 18일
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
  댓글 수: 1
neha gautam
neha gautam 2019년 3월 18일
Error using imread>get_format_info (line 543)
Unable to determine the file format.
Error in imread (line 391)
fmt_s = get_format_info(fullname);
Error in readDataset (line 35)
img = imread(strcat(dirPath, '/', imageFiles(j, :)));
This is the errors

댓글을 달려면 로그인하십시오.

카테고리

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