필터 지우기
필터 지우기

How to save images as jpgs from .mat files struct

조회 수: 20 (최근 30일)
Mamoona Nisar
Mamoona Nisar 2019년 2월 19일
이동: DGM 2024년 1월 9일
Hi ,I have 3064 .mat files dataset of brain tumors ,Prepareing data with lables for CNN.Each .mat file has struct 1x1 . Struct has following has these information: cjdata.image and cjdata.label.
And image is stored as cjdata.image.
I want to load all the .mat files:
1.mat ,2.mat ,3.mat ........................3064.mat
Want to apply these three opertions on each .mat file iteratively using a loop
1- accessing image from struct
2.convert into gray scale
3.save grayscale image as as 1.jpg 2.jpg.....3065.jpg
% Reading folder that has 3064 .mat files
myFolder = 'C:\Users\join2\Desktop\FIGSHARE\figshare data progress\figshare jpg data\3064 images';
filePattern = fullfile(myFolder, '*.mat');
Pgraymap = dir(filePattern);
for I = 1:length(Pgraymap)
baseFileName = Pgraymap(I).name;
str = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', str);
img1 = imread(str); % new
% load .mat
d=load(filePattern);
% acessing images from .mat files. each .mat has image as strcture,that
% is "cjdata.image"
d.cjdata.image;
% gray scale conversion
d=im2uint8(d);
% save all d.cjdata.image as jpgs in myFolder .plz help i don't know how to save
end

채택된 답변

Akira Agata
Akira Agata 2019년 2월 19일
To save matrix as an image, please use imwrite function.
In addition, if you save your data as an image file, I would recommend saving as .tiff or .png to keep data accuracy, because .jpg is a lossy compression method.
The following is a simple example.
inputFolder = 'C:\Users\join2\Desktop\FIGSHARE\figshare data progress\figshare jpg data\3064 images';
outputFolder = pwd; % Please change, if needed.
fileList = dir(fullfile(inputFolder,'*.mat'));
for kk = 1:numel(fileList)
S = load(fullfile(fileList(kk).folder,fileList(kk).name));
I = S.cjdata.image;
I = mat2gray(I);
fileName = replace(fileList(kk).name,'.mat','.tiff');
imwrite(I,fullfile(outputFolder,fileName));
end
  댓글 수: 12
Asaf Raza
Asaf Raza 2021년 4월 10일
이동: DGM 2024년 1월 9일
Mamoona kindly can you share the images data with me
Meenakshi
Meenakshi 2024년 1월 9일
이동: DGM 2024년 1월 9일
can you share the image data?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by