フィルターのクリア

How to read and store greyscaleimage into single matrix?

2 ビュー (過去 30 日間)
subha
subha 2014 年 11 月 21 日
コメント済み: subha 2014 年 11 月 24 日
i have 2429 images in pgm format. each is in 19*19 size. Now i need to read all the images one by one and store in single matrix. With the help of previous mathworks available examples i read my file. now how to store in single matrix.
myFolder = 'C:\Users\smanohar\Documents\MATLAB\RBMimplementation\Gaussian RBM\gdrbm\greyscsalegdrbm\face';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.pgm');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
end

採用された回答

Geoff Hayes
Geoff Hayes 2014 年 11 月 21 日
Subha - if all images are of the same dimension, 19x19, then you can save all of them to a single array of size 19x19x2429. Try something like the following
% pre-size the image array
imageArray = zeros(19,19,2429);
filePattern = fullfile(myFolder, '*.pgm');
pgmFiles = dir(filePattern);
for k = 1:length(pgmFiles)
baseFileName = pgmFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray(:,:,k) = imread(fullFileName);
end
Note how the kth image is stored in the imageArray. You may want to cast the imageArray to the appropriate data type before or after you have copied the images into it. (The appropriate data type being that of the pgm images.)
  4 件のコメント
Geoff Hayes
Geoff Hayes 2014 年 11 月 22 日
Subha - why are you doing the following
imageArray= reshape(imageArray,19*19,k);
so that it is dependent upon k? Is this during each iteration or once finished and so outside the for loop?
If you want a matrix that is 2429x361, then why not do the following
% pre-size the image array
imageArray = zeros(2429,19*19);
filePattern = fullfile(myFolder, '*.pgm');
pgmFiles = dir(filePattern);
for k = 1:length(pgmFiles)
baseFileName = pgmFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray(k,:) = reshape(imread(fullFileName),1,361);
end
The above does the reshape on the image as it is read from the file, converting it from the 19x19 matrix to one that is 1x361.
subha
subha 2014 年 11 月 24 日
thanks Geoff.

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

その他の回答 (1 件)

Image Analyst
Image Analyst 2014 年 11 月 22 日
Why not try the montage() function?

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by