How to save image from dataset in MATLAB

2 ビュー (過去 30 日間)
Med Future
Med Future 2022 年 4 月 11 日
回答済み: yanqi liu 2022 年 4 月 19 日
Hello everyone, I hope you are doing well.
with reference to my previous question :
In previous question i have the dataset of shape 3000x1000 and each row has value from 1 to 1000
and each class has 1000 samples in each row
so the code create the image of each row 1000x1000 and save in the folder
Now i have the dataset of shape 100x1000. but now i have in each row has value from 1 to 10000.
The only different is that my values in each rows change from 1 to 10000
When i try to save the image it gives blank image How can i save the value as image
%% create grayscale shapes that resemble the data
[numImages, lenImage] = size(Dataset1000);
imSz = 1000; % assuming images are 1000x1000
imbg = false(imSz); % background "color"
imfg = ~imbg(1,1); % forground "color"
imSizeOut=[1000 1000]; % resize to 224 by 224
for imNum = 1:numImages
imData = Dataset1000(imNum,:); % get pattern
[~,Y] = meshgrid(1:imSz); % make a grid
% black and white image
BW = imbg;
BW(Y==imData)=imfg;
% resize (from 1000x1000 to 224x224)
BW=imbinarize(imresize(double(BW),imSizeOut));
% convert to uint8 (0 255)
im = im2uint8(BW);
% flip
im = flipud(im);
folderName = 'C/user/images';
im_FullFilename = fullfile(folderName,sprintf('im_%06g.png',imNum));
imwrite(im,im_FullFilename);
end

採用された回答

yanqi liu
yanqi liu 2022 年 4 月 19 日
load matlab1.mat
%% create grayscale shapes that resemble the data
[numImages, lenImage] = size(Dataset1000);
Dataset1000 = round(mat2gray(Dataset1000)*1000);
imSz = 1000; % assuming images are 1000x1000
imbg = false(imSz); % background "color"
imfg = ~imbg(1,1); % forground "color"
imSizeOut=[1000 1000]; % resize to 224 by 224
for imNum = 10
imData = Dataset1000(imNum,:); % get pattern
[~,Y] = meshgrid(1:imSz); % make a grid
% black and white image
BW = imbg;
BW(Y==imData)=imfg;
% resize (from 1000x1000 to 224x224)
BW=imbinarize(imresize(double(BW),imSizeOut));
% convert to uint8 (0 255)
im = im2uint8(BW);
% flip
im = flipud(im);
folderName = 'C/user/images';
im_FullFilename = fullfile(folderName,sprintf('im_%06g.png',imNum));
imwrite(im,im_FullFilename);
end

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeImages についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by