Convert multiple images in a single .mat file into jpg images

There is a .mat file that contains images and I don't have much information about it. I'd like to convert each of these images stored in the single .mat file to jpg and view them. I would also like to know why these images are stored in a single .mat file as opposed to multiple .mat files. Please help.

 Accepted Answer

Who knows why they chose a single one instead of multiple files. Anyway, to get the images out, assuming they are called iamge1, image2, etc.
s = load(fullFileName)
imwrite('image1.png', s.image1); % Don't use jpg or you'll have compression artifacts.
imwrite('image2.png', s.image2); % Don't use jpg or you'll have compression artifacts.
imwrite('image3.png', s.image3); % Don't use jpg or you'll have compression artifacts.

7 Comments

Thank you for your response. I am sorry I am not sure how many images there are in that mat file. Could you give me some input on that please.
What do you see in the command window when you do this:
s = load(fullFileName)
This is what I got.
>> s = load('file_one.mat')
s =
struct with fields:
output_array: [384×4096 double]
I only see one matrix (image) in there, called output_array. Why do you think there are multiple images in there???
s = load(fullFileName)
imwrite('output_array.png', s.output_array); % Don't use jpg or you'll have compression artifacts.
Hi, Am sorry about the previous message. I had some confusion. Thank you for your response. It was mentioned that there are multiple images in that mat file. They are series of brain MRI images strored as array. I was able to display the image using the above code but I recieve a warning : "Image is too big to fit on screen; displaying at 33% " and the image is not clear since it is big. I get a horizontal long image with patches. Is there a way I can display these series of patchy brain images clearly?
You have to figure out how many patches (images) are across that, and then divide the image up into parts
numImages = 8; % Or whatever it is.
s = load('file_one.mat')
output_array = s.output_array;
[rows, columns, numberOfColorChannels] = size(output_array);
if numberOfColorChannels == 3
output_array = rgb2gray(output_array);
end
% Extract and display them all in separate subplots.
columnsPerPatch = floor(columns / numImages)
plotRows = ceil(sqrt(numImages));
k = 1;
for col = 1 : columnsPerPatch : columns
thisImage = output_array(:, col : col + columnsPerPatch - 1);
subplot(plotRows, plotRows, k);
imshow(thisImage, []);
k = k + 1;
end
Thank you for your response. It worked!

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Products

Asked:

on 28 Jan 2022

Commented:

on 8 Feb 2022

Community Treasure Hunt

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

Start Hunting!