Unable to read certain .JPG files in a folder

조회 수: 2 (최근 30일)
Holly Pothier
Holly Pothier 2018년 4월 17일
답변: Holly Pothier 2018년 4월 18일
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
Holly Pothier
Holly Pothier 2018년 4월 17일
편집: Guillaume 2018년 4월 17일
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 2018년 4월 17일
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.

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

채택된 답변

Guillaume
Guillaume 2018년 4월 17일

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
Holly Pothier
Holly Pothier 2018년 4월 17일
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 2018년 4월 18일
편집: Guillaume 2018년 4월 18일
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.

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

추가 답변 (1개)

Holly Pothier
Holly Pothier 2018년 4월 18일
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.

카테고리

Help CenterFile Exchange에서 Image Segmentation and Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by