How to store images in a single array or matrix

조회 수: 178 (최근 30일)
Bashir
Bashir 2013년 5월 27일
편집: DGM 2023년 2월 12일
Hello, i have a number of jpeg images stored in a folder; how can i read all the images and store them as a single matrix or array (reserving the image format).

채택된 답변

Image Analyst
Image Analyst 2013년 5월 27일
Straight out of the FAQ:
myFolder = 'C:\Documents and Settings\yourUserName\My Documents\My Pictures';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.jpg');
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);
imshow(imageArray); % Display image.
drawnow; % Force display to update immediately.
end
Obviously, you can adapt as needed, because I'm not sure what you mean when you say "store them as a single matrix". There is a montage() function that will do that if that's what you want. It will stitch together all the image files into one giant image.
  댓글 수: 16
Ganesh Gajavelli
Ganesh Gajavelli 2017년 6월 28일
편집: Ganesh Gajavelli 2017년 6월 28일
One thing to look out for with montage() is that it requires all of the pictures to be of the same dimensions. But thanks anyways!
Ganesh Gajavelli
Ganesh Gajavelli 2017년 6월 28일
편집: Ganesh Gajavelli 2017년 6월 28일
Another thing that could be useful is imageSet() Maybe this could work.

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

추가 답변 (5개)

Azzi Abdelmalek
Azzi Abdelmalek 2013년 5월 27일
f=dir('*.jpg')
files={f.name}
for k=1:numel(files)
Im{k}=imread(files{k})
end
  댓글 수: 12
Jan
Jan 2019년 3월 11일
[MOVED from flags] UKM wrote:
Very simple and Very perfect>> Thanks alot.
@UKM: Please use flags only to inform admins and editors about inappropriate content like sopam or rudeness.
Abhishek Singh
Abhishek Singh 2019년 7월 2일
@Image Analyst After I have the array for the images can I compare those images without using loop? I need to compare two images using ssim() which was suggested by you only but I think loop is very slow in Matlab I was looking for something in arrays and vectors which could help me.

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


Shelly Smith
Shelly Smith 2017년 2월 15일
편집: Image Analyst 2017년 2월 15일
Hey
I have a problem and will be grateful if someone help me. I am reading 200 images through this code:
folder = 'C:\Users\whatever'
filePattern = fullfile(folder, '*.pgm');
f=dir(filePattern)
files={f.name}
for k=1:numel(files)
fullFileName = fullfile(folder, files{k})
cellArrayOfImages{k}=imread(fullFileName)
imshow(cellArrayOfImages{k});
end
I get a row vector with 200 length. In each cell there's a 50x50 uint8 image. I need to have the elements value of 50x50 image as well. So I want to basically read all the images (200 images) and stored the value of all images in a matrix. Can anyone tell me how can I do that? Thanks in advance.
  댓글 수: 1
Image Analyst
Image Analyst 2017년 2월 15일
I don't understand the question. You are reading in 200 images, which are 50 x 50 uint8 images, and storing them. It seems to be doing what you are asking for, so what's the problem? Do you want a 2-D array of cells instead of a 1-D row vector of cells? If so, why? And how many rows and columns do you want?

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


Lav Palve
Lav Palve 2019년 1월 21일
Store images in cell array
clc;
clear all;
close all;
arr = cell(25,77); % Create Cell array
k = 1;
% get 'zero' 25 image cell array
img_folder = ('E:\4th year\Project\Images\Chardatabase\Sample0'); % Enter name folder and its path
filenames = dir(fullfile(img_folder,'*.jpg')); % Read all image with specified extantion
Total_image = numel(filenames); % Count total image
for i=1:Total_image
j = 1;
for j=1:77
f = fullfile(img_folder,filenames(i).name); % Stroe ith image path
Output = imread(f); % read image
Output = imresize(Output,[11 7]);
Output = im2bw(Output);
Output = reshape(Output,[],77); % cell array divide by '77'
Output = im2double(Output);
arr{k,j} = Output(1,j); % get all pixel value of 'Output' image
end
k = k+1;
end

Vojtech Raska
Vojtech Raska 2020년 3월 13일
편집: Vojtech Raska 2020년 3월 13일
Hi, i´m using this code to make 3-D matrix of 2-D MRI images, but if I load more than 3 images that every image besides firts three and the last one is just black and all values of that matrices are zeros. The images are uint8 format. I tried to trasfer them to double but it didn´t work. Is there some solution or the image matrices can be only three for each other?
clc; clear all; close all;
[Name, Path] = uigetfile('*.*', 'All Files (*.*)','MultiSelect','on');
if ~iscell(Name)
Name = {Name};
end
%% matrix
for i = 1:1:length(Name)
Nazev = Name(1,i);
Nazev = char(Nazev);
Img_info = [Path Nazev];
Img = imread(Img_info);
Img(:,:,i) = rgb2gray(Img);
end

Shreyas S
Shreyas S 2020년 11월 8일
편집: DGM 2023년 2월 12일
im = {'image1.png','image2.png','image3.png'};
for k = 1:length(im)
image1 = im2double(imread(im{k}));
% ......
end

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by