Importing multiple jpg files using imread. File format error.

조회 수: 2 (최근 30일)
Kelsey Frewin
Kelsey Frewin 2016년 6월 24일
댓글: Walter Roberson 2016년 6월 24일
Recently created a subset of script to import a number of .jpg files by putting them in a loop. This script has been working perfectly for me, until today. I've not changed anything and have tried this both on my personal PC and University PCs, and still get the same error.
I got the information for the script from this link and followed it exactly. http://uk.mathworks.com/help/matlab/import_export/process-a-sequence-of-files.html
My script looks like this:
jpegFiles = dir('*.jpg');
numfiles = length(jpegFiles);
images = cell(1, numfiles);
for k = 1:numfiles
images{k} = imread(jpegFiles(k).name);
end
Previously, as I mentioned, there have been no problems. I now suddenly get this error, and have tried all I can think of!
Error using imread (line 362)
Unable to determine the file format.
Error in imagesin (line 6)
images{k} = imread(jpegFiles(k).name);
I am new to this and any suggestions would be appreciated!
Thanks in advance.

채택된 답변

Walter Roberson
Walter Roberson 2016년 6월 24일
One of your files has a .jpg suffix but is not a valid JPEG file. Try
jpegFiles = dir('*.jpg');
numfiles = length(jpegFiles);
images = cell(1, numfiles);
for k = 1:numfiles
thisfile = jpegFiles(k).name;
try
images{k} = imread(thisfile);
catch
warning('File #%d, name "%s" does not appear to be a valid image, skipping it. The file size for it is %d bytes', k, thisfile, jpegFiles(k).bytes );
images{k} = uint8([]);
end
end
  댓글 수: 3
Kelsey Frewin
Kelsey Frewin 2016년 6월 24일
Just re-examined the folder and found a number of corrupted files. Issue seems to be sorted now they have been removed. Thank you so much!
Walter Roberson
Walter Roberson 2016년 6월 24일
You see that ._ ? That file is a thumbnail. It is likely the thumbnail will get generated again.
After you do
jpegFiles = dir('*.jpg');
do
isdotfile = strncmp({jpegFiles.name}, '.', 1);
jpegFiles(isdotfile) = [];

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

추가 답변 (1개)

Muhammad Usman Saleem
Muhammad Usman Saleem 2016년 6월 24일
jpegFiles = dir('*.jpeg');
numfiles = size(jpegFiles);
try to use this
  댓글 수: 1
Kelsey Frewin
Kelsey Frewin 2016년 6월 24일
Have input this, and get this error returned:
Error using cell
Size inputs must be scalar.
Error in imagesin (line 3)
images = cell(1, numfiles);

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

카테고리

Help CenterFile Exchange에서 Desktop에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by