How to create a single .mat file from multiple images?

Asked by Le Nguyen on 5 Oct 2012
Latest activity Commented on by Le Nguyen on 6 Oct 2012

Hi all,

Assume that I have 5 images as following: 1.bmp, 2.bmp, 3.bmp, 4.bmp, 5.bmp. I want to combine those 5 images into a single .mat file. Anyone knows how to solve this issue, please help me!!

Thanks in advance!!

0 Comments

Le Nguyen

Tags

Products

No products are associated with this question.

1 Answer

Answer by Azzi Abdelmalek on 5 Oct 2012
Edited by Azzi Abdelmalek on 5 Oct 2012
Accepted answer
for k=1:5
h{k}=imread(sprintf('%d.bmp',k));
end
save file h

6 Comments

Matt J on 6 Oct 2012

Le Nguyen, you double check it. You're supposed to be studying the code people suggest to you and understanding why they work or don't work.

In this case, I suspect that your bitmap files are not 2D, but rather have color channels. Thus they cannot be treated as the slices of a 3D array, as Matt Kindig's code attempts to do.

Image Analyst on 6 Oct 2012

Matt J is probably right. Here are some code snippets that may help you:

% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);

Alternatively,

% Get a weighted sum of the Red, Green, and Blue channels.
grayImage = rgb2gray(rgbImage);
Le Nguyen on 6 Oct 2012

Hi all,

After taking a look around Matlab for a while, I seem to know how to solve this issue!! You are right, Matt J!! My bitmap files are all 3D as well as colourful!! That's why Matt Kindig's solution didnt work at all!! Here is my modification and it really works well!!

img = rgb2gray(imread('1.bmp'));

[r,c]= size(img);

h = NaN(r,c,5); %pre-allocate memory

h(:,:,1) = img;

for k=2:5

   h(:,:,k) = rgb2gray(imread(sprintf('%d.bmp',k)));

end

save filename h

I am totally a newbie to Matlab, thus, all of your suggestions are always helpful to me and I am really in debt of it!!

Again, thanks a lot!! :)

PS: if there are any modifications for better performance as well as optimization, please let me know!!

Azzi Abdelmalek

Contact us