How to save each row as image in MATLAB
6 views (last 30 days)
Show older comments
Hello everyone, I hope you are doing well.
I have the following dataset which consists three class and dataset shape 3000x1000
first 1000x1000 belongs to class 1. next 1000x1000 belongs to class 2 and next 1000x1000 belongs to class 3 to make total of 3000x1000
i want so save each row as image form to train Resnet50 How can i do that?
0 Comments
Accepted Answer
AndresVar
on 9 Mar 2022
Edited: AndresVar
on 10 Mar 2022
Edit: use rescale the entire dataset instead of each row.
Edit: note in the example I put padding on the data to shape it into a square, but you could just reshape rectangular and then resize to square.
You can try making RGB images from the data like in (example below): Classify Time Series Using Wavelet Analysis and Deep Learning - MATLAB & Simulink Example (mathworks.com)
Or look into other ways to use the 1D data: Deep Learning with Time Series and Sequence Data - MATLAB & Simulink (mathworks.com)
clear;
load("Dataset1000x1000.mat")
[labelNums,~,labelIdx] = unique(labels1000,'rows');
labelStrs = strcat('Label_',strrep(cellstr(num2str(labelNums)),' ',''))
%% make the folders
for ii = 1:numel(labelStrs)
labelStr = labelStrs{ii};
if ~isfolder(labelStr)
mkdir(labelStr);
end
end
%% create RGB from data
% for example as in https://www.mathworks.com/help/wavelet/ug/classify-time-series-using-wavelet-analysis-and-deep-learning.html
[numImages, lenImage] = size(Dataset1000);
nextSquareLen = (floor(sqrt(lenImage))+1)^2;
squarePadLen = nextSquareLen-lenImage;
im_size = [224 224];
DataSet1000Scaled = rescale(Dataset1000);
for ii = 1:100:numImages
im_orig = DataSet1000Scaled(ii,:);
im_orig_pad = reshape(... % reshape to a square image
padarray(... % pad to a square length
im_orig,[0,squarePadLen],'post'),sqrt(nextSquareLen),[]);
im_rgb = imresize(... % resize for compatibility with NN
ind2rgb(... % make into RGB with a colormap
im2uint8(...
im_orig_pad),jet(255)),im_size);
folderName = labelStrs{labelIdx(ii)};
im_FullFilename = fullfile(folderName,sprintf('im_%06g.jpg',ii));
imwrite(im_rgb,im_FullFilename);
end
Edit: OR save patterns inferred from the data. Change loop conditions to get all images. Change SE size (instead of 16) to something smaller to your liking.
%% create grayscale shapes that resemble the data
[numImages, lenImage] = size(Dataset1000);
imSz = 1000; % assuming images are 1000x1000
imbg = true(imSz); % background "color"
imfg = ~imbg(1,1); % forground "color"
imSizeOut=[224 224]; % resize to 224 by 224
for imNum = 1:200:numImages
imData = Dataset1000(imNum,:); % get pattern
[~,Y] = meshgrid(1:imSz); % make a grid
% black and white image
BW = imbg;
BW(Y==imData)=imfg;
% make pattern thicker by eroding (helps during resizing)
SE=strel("disk",16); % adjust element size, 16 might be too big for cluttered patters
BW=imerode(BW,SE);
% resize (from 1000x1000 to 224x224)
BW=imbinarize(imresize(double(BW),imSizeOut));
% convert to uint8 (0 255)
im = im2uint8(BW);
% flip
im = flipud(im);
folderName = labelStrs{labelIdx(imNum)};
im_FullFilename = fullfile(folderName,sprintf('im_%06g.png',imNum));
imwrite(im,im_FullFilename);
end
24 Comments
More Answers (0)
See Also
Categories
Find more on Image Data Workflows in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!