Unable to read certain .JPG files in a folder

2 views (last 30 days)
Hello,
I am having a similar problem to this post: unable-to-read-image-jpg-file
Basically, I have a loop that uses imread to find .jpg files then applies a rotation to the image. The loop worked fine when I was testing with a small batch of images. Now, when I add new .jpg files, I get the error "Error using imread" and that certain files were not found. I tested this with 'exist' and get the output 2. Based on the documentation this means: "2 — name is a file with extension .m, .mlx, or .mlapp, or name is the name of a file with a non-registered file extension (.mat, .fig, .txt)." How can I move forward? Why are some .jpg files being read while some are not?
Here is the relevant portion of my code:
display ('top');
%Get list of all files in the folder with the desired file name pattern
filePattern = fullfile(myFolder, '*.jpg'); %find files that are .jpg
jpegFiles = dir(filePattern); %dir = list all files that match .jpg (from filePattern)
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
display ('before loop');
for k = 1:length(jpegFiles) %loop through all .jpg files
display ('inside loop');
baseFileName = jpegFiles(k).name; %name of .jpgs (indexed individually (k)); Ex: SDC10705_RS.jpg
fullFileName = fullfile(myFolder, baseFileName); %fullfile builds filename from parts = myfolder\baseFileName (full path of ^)
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName); %read specific FULLfilename from store of .jpg files
%this should be the full
%Rotate
display ('rotation');
%raw= jpegFiles(k).name;
I = imread(jpegFiles(k).name); %must have imread for next step to work (rotation of I)
R = imrotate(I, 355, 'bicubic'); %put angle and type of rotation here. 'bicubic' works best/ least distortion
imshow(R); %display image
%drawnow; %force display to update immediately
%Break up fileparts to allow saving with '_rotate' extension when looping through files
[tmp_path, tmp_name, tmp_ext] = fileparts(fullFileName); %separating fullFileName, into path, name, and ext (.jpg) (pretty much opposite of fullfile)
%need to do this so .jpg is at the end
fullFileName_rotate = [tmp_path, filesep, tmp_name, '_rotate',tmp_ext];
imwrite(R, fullFileName_rotate); %new rotated image
%saved as filename_rotate
end;
Thank you.
  3 Comments
Holly Pothier
Holly Pothier on 17 Apr 2018
Edited: Guillaume on 17 Apr 2018
Sorry I put it as an answer. Here it is:
Error using imread (line 349) File "SDC10705_RS.jpg" does not exist.
Error in Rotate_WithLoop (line 27) I = imread(jpegFiles(k).name); %must have imread for next step to work (rotation of I)
Holly Pothier
Holly Pothier on 17 Apr 2018
Also, just to clarify some of the results from 'exist'. I did 'exist' on a few more files in the folder. The files that work output 2, the ones that do not and report as missing output 0.

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 17 Apr 2018

Most likely, the line

imageArray = imread(fullFileName); %read specific FULLfilename from store of .jpg files

works fine for all the files. However, you're getting an error with the next imread:

I = imread(jpegFiles(k).name);

because that one looks for the file in the local folder instead of myFolder.

I = imread(fullFileName);

would fix the issue, but I don't see the point of reloading an image that you've already loaded in memory, so the simplest is to get rid of that imread and use the existing imageArray instead of I in the next line:

R = imrotate(imageArray, 355, 'bicubic'); %put angle and type of rotation here. 'bicubic' works best/ least distortion                           
  2 Comments
Holly Pothier
Holly Pothier on 17 Apr 2018
Hello Guillaume,
I tried your suggestion but I still get the same error on certain images. The loop works fine with most of my images, but when it gets to certain images, I get the error that the .jpg file does not exist. How is it that some of them are recognized but some are not? They are all .jpg files, but acquired from different image sets.
Thank you.
Guillaume
Guillaume on 18 Apr 2018
Edited: Guillaume on 18 Apr 2018
What is the exact error message you get now? I would suspect the problem is that you're still trying to load files from the wrong location.

Sign in to comment.

More Answers (1)

Holly Pothier
Holly Pothier on 18 Apr 2018
Sorry for the confusion, once I made all your suggested edits it worked. Thank you for the help, and for clarifying the redundancy/error in the code.

Tags

Community Treasure Hunt

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

Start Hunting!