I need to import multiple images (10.000) in Matlab (2013b) from a subdirectory of the predefined directory of Matlab.

I don't know the exact names of the images.

I tried this:

file = dir('C:\Users\user\Documents\MATLAB\train');
NF = length(file);

for k = 1 : NF
    img = imread(fullfile('C:\Users\user\Documents\MATLAB\train', file(k).name));
end

But it throws this error though I ran it with the Admin privileges:

Error using imread (line 347)
Can't open file "C:\Users\user\Documents\MATLAB\train\." for reading;
you may not have read permission.
有帮助吗?

解决方案

The "dir" command returns the virtual directory elements "." (self directory) and ".." parent, as your error message shows.

A simple fix is to use a more specific dir call, based on your image types, perhaps:

file = dir('C:\Users\user\Documents\MATLAB\train\*.jpg');

其他提示

Check the output of dir. The first two "files" are . and .., which is similar to the behaviour of the windows dir command.

file = dir('C:\Users\user\Documents\MATLAB\train');
NF = length(file);

for k = 3 : NF
    img = imread(fullfile('C:\Users\user\Documents\MATLAB\train', file(k).name));
end

In R2013b you would have to do

file = dir('C:\Users\user\Documents\MATLAB\train\*.jpg');

If you have R2014b with the Computer Vision System Toolbox then you can use imageSet:

images = imageSet('C:\Users\user\Documents\MATLAB\train\');

This will create an object containing paths to all image files in the train directory, regardless of format. Then you can read the i-th image like this:

im = read(images, i);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top