How to read .png image from a folder iteratively?

I have a folder with multiple images. The format of all images is .png. When I try to read all those images in a cell array I am getting following error.
Error:
Error using imread>get_format_info (line 543)
Unable to determine the file format.
Error in imread/call_format_specific_reader (line 464)
fmt_s = get_format_info(fullname);
Error in imread (line 440)
[X, map] = call_format_specific_reader();111.png
%Specify training and test folders
train_fold = ['..',filesep,'Train_Set',filesep];
test_fold = ['..',filesep,'Test_Set',filesep];
%Read train and test set images folder information
train_ims = dir(train_fold);
test_ims = dir(test_fold);
%Remove all the train folders that do not contain images
for i = 1:length(train_ims)
if (length(train_ims(i).name)<10)
train_ims(i).name = [];
end
end
train_ims = train_ims(~cellfun('isempty',{train_ims.name}));
% Remove all the test folders that do not contain images
for i = 1:length(test_ims)
if (length(test_ims(i).name)<10)
test_ims(i).name = [];
end
end
test_ims = test_ims(~cellfun('isempty',{test_ims.name}));
for i = 1:length(train_ims)
ims{i} = imread([train_fold,filesep,'compositeImage', num2str(i), '.png']);
end

4 comentarios

Rik
Rik el 28 de Abr. de 2019
Editada: Rik el 28 de Abr. de 2019
Can you show the file name that causes the issue? You can put in this change to easily tell:
%change this
ims{i} = imread([train_fold,filesep,'compositeImage', num2str(i), '.png']);
%to this
clc,disp([train_fold,filesep,'compositeImage', num2str(i), '.png'])
ims{i} = imread([train_fold,filesep,'compositeImage', num2str(i), '.png']);
Hassan Ashraf
Hassan Ashraf el 28 de Abr. de 2019
Thank You Rik, for your reply. I've tried your suggested solution but stil getting the same error message. One thing that I have noticed, is that when the file I am trying to read has no details of its height and width in its properties.
May be due to this the error is occuring?
Rik
Rik el 28 de Abr. de 2019
My code was not to fix the problem, but to find out which file is causing it. Can you attach the file that is causing the problem? Where are you reading its properties? In windows? From Matlab?
Hassan Ashraf
Hassan Ashraf el 28 de Abr. de 2019
I am reading its properties in windows. In MATLAB can't read file info or anything else. I have attached the file that I am trying to read.

Iniciar sesión para comentar.

 Respuesta aceptada

Guillaume
Guillaume el 28 de Abr. de 2019
Editada: Guillaume el 28 de Abr. de 2019
The problem is obvious once you've seen the code in your other question that you've used to create these images. These images are not images at all but mat files. Sticking a png extension onto a mat file does not make it a png image.
The files could be read back into matlab with:
matcontent = load(fullfile(train_fold, sprintf('CompositeImage%d.png', i)); %read all the variables in the mat file
ims{i} = matcontent.CompositeImage; %ang get the CompositeImage variable out of it.
But no program will ever recognise these files as png images.

Más respuestas (0)

Categorías

Más información sobre Images en Centro de ayuda y File Exchange.

Preguntada:

el 28 de Abr. de 2019

Comentada:

el 29 de Abr. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by