I'm working with a database which has 472 pgm files inside a folder named faces. I want to read all 472 and store it I
also each file inside the folder is name cmu_0000.pgm..........cmu_00471.pgm

4 件のコメント

Stephen23
Stephen23 2017 年 8 月 17 日
Why not read the MATLAB documentation?:
Or any of the hundreds of threads on this forum that explain how to read files in a loop:
etc., etc.
kitty varghese
kitty varghese 2017 年 8 月 17 日
編集済み: Stephen23 2017 年 8 月 17 日
I tried this but i'm getting error
_Error in cbcldata (line 22)
I = imread([thepath D(i+1).pgm]);
function V = cbcldata
% cbcldata - read face image data from cbcl database
%
global imloadfunc;
imloadfunc='x'
% This is where the cbcl face images reside
thepath = 'C:\Users\kitty\Dropbox\non negative factorization\faces\face.test\test\face\ *.m';
% Create the data matrix
V = zeros(19*19,472);
% Read the directory listing
D = dir(thepath);
% Step through each image, reading it into the data matrix
fprintf('Reading in the images...\n');
for i=1:472,
I = imread([thepath D(i+1).pgm]);
end
V(:,i) = reshape(I,[19*19, 1]);
% if rem(i,100)==1
% fprintf('[%d/24]',floor(i/100));
% end
end
Stephen23
Stephen23 2017 年 8 月 17 日
@kitty Varghese: today I formatted your code correctly for you. In future you can do this yourself: simply select the code text and then click the {} Code button.
Stephen23
Stephen23 2017 年 8 月 17 日
@kitty Varghese: there is no point in getting dir to search for files with a .m extension:
thepath = ... *.m';
if what you are after is .pgm files. Do not hard-code how many files there are:
for i=1:472,
when it is much more robust to use numel. I would also recommend avoid i as a variable name.

サインインしてコメントする。

 採用された回答

Stephen23
Stephen23 2017 年 8 月 17 日
編集済み: Stephen23 2017 年 8 月 17 日

0 投票

Something like this perhaps (untested):
P = 'C:\Users\kitty\Dropbox\non negative factorization\faces\face.test\test\face';
D = dir(fullfile(P,'*.pgm'));
C = cell(size(D));
for k = 1:numel(D)
C{k} = imread(fullfile(P,D(k).name));
end
Note I loaded that data into a cell array: this is because images commonly are stored as a 3D matrix: as an alternative you could store them is a 4D numeric array.

12 件のコメント

Stephen23
Stephen23 2017 年 8 月 18 日
編集済み: Stephen23 2017 年 8 月 18 日
@kitty Varghese: after running the code, please show the outputs of these commands:
size(C)
size(D)
disp(D(1).name)
disp(D(end).name)
disp(k)
"only the last file is getting read."
How are you checking this? What are you looking at that tells you that only the last file is being read?
Stephen23
Stephen23 2017 年 8 月 18 日
編集済み: Stephen23 2017 年 8 月 18 日
@kitty Varghese: Are you certain that all of the images have the same size?
Note that as I mentioned in my answer, most likely your images are 3D RGB images, which means that concatenating them along the 1st, 2nd, or 3rd dimension will probably not give you a very useful numeric array. That I why I put them into a cell array, then each image is conveniently stored as its own array.
If you really want to merge them into one numeric array then the most important step is to ensure that all of the images have the same size:
cell2mat(cellfun(@size,C(:),'uni',0))
and check that all sizes are the same. You could concatenate them along any dimension that you choose:
cat(4,C{:})
would likely be the most useful for further analysis. Alternatively, if you really have 2D images(e.g. grayscale or indexed) then you could concatenate along the third dimension.
kitty varghese
kitty varghese 2017 年 8 月 18 日
>size(C)
472 1
> size(D)
472 1
> disp(D(1).name)
cmu_0000.pgm
>disp(D(end).name)
cmu_0471.pgm
>disp(k)
472
Stephen23
Stephen23 2017 年 8 月 18 日
編集済み: Stephen23 2017 年 8 月 18 日
@kitty Varghese: those values look okay, and the k values tells us that all 472 loop iterations have been completed. You have not explained why you think that only the last image has been loaded into MATLAB.
kitty varghese
kitty varghese 2017 年 8 月 18 日
I wanted to reshape it as
V(:,i) = reshape(C,[19*19, 1]);
ie i want to read one file in one column only. But can't do it as its a cell array.
kitty varghese
kitty varghese 2017 年 8 月 18 日
@Stephen Cobeldick I'm sorry I didn't see my mistake.Your code is perfectly correct. Thanks
Stephen23
Stephen23 2017 年 8 月 18 日
編集済み: Stephen23 2017 年 8 月 18 日
@kitty verghese: If you do not need all of the images stored after the loop then of course you can get rid of the cell array altogether:
I = imread(fullfile(P,D(k).name));
and continue with whatever processing you wish to do.
kitty varghese
kitty varghese 2017 年 8 月 21 日
@Stephen cobeldick when I try to concatenate for 2D images I'm getting its size as [19 8968] whereas I want it to be [361 472] i.e [19*19 472]
Stephen23
Stephen23 2017 年 8 月 21 日
編集済み: Stephen23 2017 年 8 月 21 日
It sounds like each image is sized 19x19, and you want to reshape each image into a column, before concatenating into one array. Here is one solution (untested):
for k = 1:numel(D)
I = imread(fullfile(P,D(k).name));
C{k} = I(:); % reshape image into column
end
M = horzcat({:});
kitty varghese
kitty varghese 2017 年 8 月 21 日
@Stephen Cobeldick yes, you are correct.I want to reshape each image into the column and make it a matrix .But I don't want it to be in an array.
kitty varghese
kitty varghese 2017 年 8 月 21 日
編集済み: Walter Roberson 2017 年 8 月 21 日
close all;
clear all;
clc;
% cbcldata - read face image data from cbcl database
% This is where the cbcl face images reside
P = 'C:\Users\kitty\Dropbox\non negative factorization\faces\face.test\test\face';
% Create the data matrix
D=dir(fullfile(P,'*.pgm'));
C=cell(size(D));
for k=1:numel(D)
C{k}=imread(fullfile(P,D(k).name));
col(:,k)=C(:);
V{k}=C(:);
end
this works perfectly .
Thanks @Stephen Cobeldick
Vipasha Sharma
Vipasha Sharma 2019 年 5 月 23 日
can anyone help me with code for reading multiple HDF files?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeImages についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by