.mat converstion to jpg png jpeg etc
2 次查看(过去 30 天)
显示 更早的评论
How i can convert the whole data set into jpg jpeg formate from .mat formate? currently each image is in .mat formate
1 个评论
Walter Roberson
2021-4-11
Is it one mat file for each image? Is the variable name in the mat file consistent?
Are all images in the same .mat file? Are they in separate variables?
回答(1 个)
Gayathri
2025-6-10
Inorder to convert the images in '.mat' format to '.jpg', we should load each file and then convert using a "for loop". Please refer to the code below for achieving the same.
% Specify the folder containing the .mat files
folderPath = 'path_to_your_mat_files'; % Replace with your folder path
matFiles = dir(fullfile(folderPath, '*.mat')); % Get all .mat files
% Loop through each .mat file
for k = 1:length(matFiles)
% Load the .mat file
matFileName = fullfile(folderPath, matFiles(k).name);
data = load(matFileName);
% Extract the image data (adjust the variable name as necessary)
img = data.imageData; % Replace 'imageData' with the actual variable name
% Convert to uint8 if necessary
img = im2uint8(img); % Ensure the image is in the correct format
% Create a filename for the output image
[~, name, ~] = fileparts(matFiles(k).name); % Get the base name of the file
outputFileName = fullfile(folderPath, [name, '.jpg']); % Change extension to .jpg
% Save the image
imwrite(img, outputFileName);
end
The above code is written assuming that each image is stored in a separate '.mat' file. If it is a single MAT file for all images please modify the code accordingly.
For more information on "imwrite" function, refer to the following documentation link.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Import, Export, and Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!