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

6 views (last 30 days)
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

Image Analyst
Image Analyst on 28 Jan 2022
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
Image Analyst
Image Analyst on 1 Feb 2022
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

Sign in to comment.

More Answers (0)

Categories

Find more on Images in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!