How to save data as binary Image in MATLAB

11 visualizaciones (últimos 30 días)
Med Future
Med Future el 18 de Oct. de 2022
Comentada: DGM el 21 de Oct. de 2022
Hello Everyone,I hope you are doing well. I want to save the following data as Binary Image in MATLAB
Each has shape 1x1000
Can anybody help me with this?
I have tried the following Code but it does not work.
[numImages, lenImage] = size(Dataset1000);
imSz = 1000; % assuming images are 1000x1000
imbg = false(imSz); % background "color"
imfg = ~imbg(1,1); % forground "color"
imSizeOut=[1000 1000]; % ImageSize
for imNum = 1:numImages
imData = Dataset1000(imNum,:); % get pattern
[~,Y] = meshgrid(1:imSz); % make a grid
% black and white image
BW = imbg;
% resize (from 1000x1000)
% convert to uint8 (0 255)
im = im2uint8(BW);
  3 comentarios
Med Future
Med Future el 18 de Oct. de 2022
@KSSV I have write the image but it save as blank Image.
Med Future
Med Future el 18 de Oct. de 2022
@KSSV The code is not working on this. can you please look whats wrong in it?

Iniciar sesión para comentar.

Respuesta aceptada

DGM el 18 de Oct. de 2022
Editada: DGM el 18 de Oct. de 2022
There's nothing to save but a black image, since the binarized image is always empty. I'm not sure what you're trying to do with the binarization comparison, but I crammed a placeholder operation in there to show that the rest can work with a bit of cleanup.
load dataset2.mat
[numImages, lenImage, ~] = size(Dataset1000);
imSizeOut=[1000 lenImage]; % i'm assuming the width is inherited
% create this outside the loop
SE = strel('disk',2);
for imNum = 1:numImages % these datasets only have one row
imData = Dataset1000(imNum,:); % get pattern
Y = 1:lenImage; % make a grid
% binarize image
% this does the same thing as before
% Y is a vector of integers from [0 1000]
% imData is a vector of FP values from approx [0 1]
% there will be no matches, and i can't guess at what you're trying to match
%BW = Y==imData; % this needs to be fixed
% resize (from 1x1000 to 1000x1000)
% this would work if you need to change both dimensions
%BW = imresize(BW,imSizeOut); % i don't think the recasting is necessary here
% but this would be much cheaper if only one dimension needs to be changed
BW = repmat(BW,[imSizeOut(1) 1]);
% since the image is binarized, the results are the same in logical and uint8
% but processing this in logical should be faster
BW = imdilate(BW,SE);
% convert to uint8 (0 255)
BW = im2uint8(BW);
Considering that all the information is in the vector, it'd probably be faster to do the dilation before replication/resizing. In that case, ones(1,5) would be equivalent to a r = 2 disk strel.
Saving the image with imwrite should be simple enough
imwrite(BW,sprintf('myimage_%04d.png',imNum)) % or something similar
Note that like most formats, PNG supports uint8, but it also supports logical images. If you use PNG, you wouldn't necessarily have to recast the output to uint8 if you didn't want to. Besides any convenience factor, using logical can result in a significantly smaller file size.
  13 comentarios
Med Future
Med Future el 21 de Oct. de 2022
@DGM Yes they are constant value, Is there is any modification in above code to get this line in middle or little bit above the bottom.
DGM el 21 de Oct. de 2022
For cases where there's zero variance, you can do something to conditionally position the line. In reality, there's really no point preserving the line, since it contains no more information than any other horizontal line. You can literally just create a new line in the middle of the image.
load dataset4.mat
outsize = [500 500];
% rescale data to fit width, generate indices
dlen = numel(Dataset1000);
xidx = 1:outsize(2);
yidx = interp1(linspace(1,outsize(2),dlen),Dataset1000,xidx);
if range(yidx) == 0
yidx(:) = round(outsize(1)/2);
yidx = outsize(1) - (outsize(1)-1)*normalize(yidx,'range');
% display a dummy image to fix geometry
% create ROI object and configure
ROI = images.roi.Polyline(gca);
ROI.Position = [xidx(:) yidx(:)];
% convert to logical mask
outpict = createMask(ROI);

Iniciar sesión para comentar.

Más respuestas (0)




Community Treasure Hunt

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

Start Hunting!

Translated by