How to save each row as image in MATLAB

1 view (last 30 days)
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?

Accepted Answer

AndresVar
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.
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
Med Future
Med Future on 21 Mar 2022
@Image Analyst but [X,Y] = meshgrid(1:imSz); create the above image i have shared how can i change that?
Med Future
Med Future on 21 Mar 2022
Edited: Med Future on 21 Mar 2022
@Image Analyst when i run this code above But when i saved my image the grid is shown as above but i want it to be in Y axis should be start from 0 -1000
clear;
load("Dataset1000x1000.mat")
imNum = 2600; % which image (row?)
imSz = 1000;
imbg = true(imSz); % background "color"
imfg = ~imbg(1,1); % forground "color"
imSizeOut=[1000 1000]; % resize to 224 by 224
imFullFilenameOut = sprintf('im%06g.png',imNum); % name the file im######.tif
imData = Dataset1000(imNum,:); % get pattern
[X,Y] = meshgrid(1:imSz);
% black and white image
BW = imbg;
BW(Y==imData)=imfg;
% make pattern thicker by eroding
SE=strel("disk",16);
BW=imerode(BW,SE);
% resize
BW=imbinarize(imresize(double(BW),imSizeOut));
% convert to uint8 (0 255)
im = im2uint8(BW);
imwrite(im,imFullFilenameOut);

Sign in to comment.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!