# [Matlab] Read Multiple Images in a Folder

Hello guys,  in this occasion I would like to share about how to read multiple images in a folder, and save the information into array. This code is conducted by Matlab, that really approachable for graduate students (subjective opinion 😀 ) . I’ll present this code for my lovely friend Ratih, and perhaps is also helpful for you all, because this case is often happened when we talk about classification, clustering, information retrieval in image processing or computer vision.

Well, for instances I have a folder named “capturedImage” as shown in a figure below. In a folder contains 24 images (let’s say 24 different colors below are 24 images hehe..).

Let’s read the information inside image into code that is described in a brief code below. In every line has its own narrative and may help you to understand what is the code indicate. I’ll take a case about how to get mean among intensities in every image, so I used data_mean to define collected information of all images in a folder through an array. Check this code out.

[code language=”matlab”]
% Cara ini digunakan untuk membaca gambar-gambar dalam satu folder
% dan kemudian meletakkkan nilai fitur gambar tersebut ke dalam sebuah
% array
% =====================================================================

clc
clear

gambar = ‘capturedImage/’; % nama folder "campturedImage"
% mengindetifikasi isi folder dalam file yang berekstensi .JPG
files = dir([gambar,’*.JPG’]);

m=struct2cell(files); % menentukan informasi gambar yang ada dalam folder
data_mean=[]; % inisialisasi awal array

for k=1:size(m,2)
file = [gambar m{1,k}]; % identifikasi nama file beserta path-nya
dataMean = mean(mean((file))) % menghitung mean dari masing-masing gambar
data_mean=[data_mean;dataMean]; % memasukkan data mean ke dalam array
end
[/code]

Ok, that’s all 🙂 As usual the open question to share is free welcome. Thank you and happy coding 🙂

## 0 thoughts on “[Matlab] Read Multiple Images in a Folder”

1. kaka klo misalnya nilai mean diganti sama ambil nilai histogram bisa gak kak…
bales ya kaka.. 🙂

• @Marwiyansyah : dicoba diganti aja…
kalau dalam kasus ini jika niali histogramnya bentuknya vektor gak bisa..

• contoh nilai histogramnya sendiri bagaimana yang dimaksud?
Output nilai histogram kira-kira ingin seperti apa?