Hello guys, I have a question concerning my Matlab script. I try to read/load several pictures but I always get the same error. Could anyone have a quick look at my script? The files I try to load defintely exist and they all have the .jpg format.

2 次查看(过去 30 天)
affectivepictures = dir('C:\Users\Kilian\Desktop\Arbeit Köln\Matlab\Bilddateien\affektive_Bilder_neu/*.jpg');
numFiles = length(affectivepictures);
for i_pics = 1:numFiles
bild = imread(affectivepictures(i_pics).name);
image{i_pics} = double(bild)/255;
% convert image matrices to textures
theImage{i_pics} = Screen('MakeTexture', window, image{i_pics});
end
  1 个评论
tom
tom 2017-12-4
the error is: Error using imread>get_full_filename (line 516) File "1.jpg" does not exist.
Error in imread (line 340) fullname = get_full_filename(filename);
Could anyone help me out with this? Kind of desperate already

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2017-12-4
projectdir = 'C:\Users\Kilian\Desktop\Arbeit Köln\Matlab\Bilddateien\affektive_Bilder_neu';
affectivepictures = dir( fullfile(projectdir, '*.jpg') );
numFiles = length(affectivepictures);
filenames = fullfile( projectdir, {affectivepictures.name});
for i_pics = 1:numFiles
thisfile = filenames{i_pics};
bild = imread(thisfile);
images{i_pics} = im2double(bild);
% convert image matrices to textures
theImage{i_pics} = Screen('MakeTexture', window, images{i_pics});
end
  4 个评论
Walter Roberson
Walter Roberson 2017-12-4
What shows up for
ls(projectdir)
?
One thing I worry about is the ö in your string. In English versions of MATLAB, .m files are not stored as UTF-8, so although the ö would show up on your screen, what would be saved in the file might be something different. It is probably okay because ö is char(246) rather than being something about 255 that would require two bytes to represent, but it is something I would double check.
Image Analyst
Image Analyst 2017-12-4
The line of code you posted is
theImage{i_pics} = Screen('MakeTexture', window, images{i_pics});
yet the error is regarding this line of code:
Screen('DrawTexture', window, theImage{Identification(trial)}, [], instrImagePosition);
which is not in the code you posted. Note that the third argument of Screen() is "images" in the first code and "theImage" in the second code. So which is it? If you're using the second line of code, then theImage has never been defined yet so that's why you're getting the error message.
I think you need to take more care about your variable names and which are used where, and also in showing us the proper code and matching error message.

请先登录,再进行评论。

更多回答(3 个)

KL
KL 2017-12-4
Probably you're not working on the same directory. Try using fullfile,
bild = imread(fullfile(affectivepictures(i_pics).path,affectivepictures(i_pics).name));
  2 个评论
tom
tom 2017-12-4
Hey KL, thanks for your answer. Now I get this error "Reference to non-existent field 'path'."
sorry, I´am completely new to the field.
The strange thing is, a few days ago it already worked out fine with the Code above. I don´t think I changed something about it.
Any other ideas? Best regards

请先登录,再进行评论。


Image Analyst
Image Analyst 2017-12-4
You forgot to use fullfile() to prepend the folder.
This is a very F.A.Q. So see the faq (the second chunk of code): http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F

tom
tom 2017-12-5
Thank you all!
It works now totally fine. First question --> first answer. Great community.
best regards Kilian

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by