How to save image from dataset in MATLAB

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

Accepted Answer

yanqi liu
yanqi liu on 19 Apr 2022
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

More Answers (0)

Categories

Find more on Images 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!