How to save each row as image in MATLAB

4 ビュー (過去 30 日間)
Med Future
Med Future 2022 年 3 月 9 日
編集済み: Med Future 2022 年 3 月 21 日
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?

採用された回答

AndresVar
AndresVar 2022 年 3 月 9 日
編集済み: AndresVar 2022 年 3 月 10 日
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 件のコメント
Med Future
Med Future 2022 年 3 月 21 日
@Image Analyst but [X,Y] = meshgrid(1:imSz); create the above image i have shared how can i change that?
Med Future
Med Future 2022 年 3 月 21 日
編集済み: Med Future 2022 年 3 月 21 日
@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);

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by