How to save plot graphs as binary image with 64x64x1 dimensions in matlab

3 Ansichten (letzte 30 Tage)
San May
San May am 11 Aug. 2021
Bearbeitet: Dave B am 11 Aug. 2021
I would like to know how to save graphs as binary image (pixels are 0 and 1).
And, the graph images in my code are color images with size (256x256x3 dimensions). I also want to change the size to 64x64x1 dimension.
Instead of using rgb2gray, imbinarize and imresize functions, is there another way to save as binary images automically from the plot?
Here is my code.
for i = 1:5;
for j = 0.2:0.1:0.6;
x = linspace(-2*pi,2*pi);
y1 = i*j*sin(x);
y2 = i*j*cos(x);
fig1 = figure;
set(gcf, 'Position', [0, 0, 164, 164])
plot(x,y1,x,y2)
saveas(fig1, strcat('i', num2str(i), '_', 'j', num2str(j), '_', '.jpg'));
end
end
thank you so much.

Antworten (1)

Dave B
Dave B am 11 Aug. 2021
Bearbeitet: Dave B am 11 Aug. 2021
You could use getframe to get a matrix representing the image
x = linspace(-2*pi,2*pi);
y1 = i*j*sin(x);
y2 = i*j*cos(x);
plot(x,y1,x,y2);
f=getframe(gcf);
im=frame2im(f);
Now you have a bunch of numbers to work with. If you are against using RGB2GRAY and IMBINARIZE you have to decide how you'd like to turn it into zeros and 1s...is it everything that's not exactly white?
imshow(~any(im<255,3))
Or maybe a slightly reduced threshold?
imshow(~any(im<200,3))
To turn these zeros and ones into pixels, you'll want to transform it to integers between one and 255.
outim = uint8(255*~any(im<200,3));
We can use interp2 to resize by interpolation, to do that we'll need a grid of values ranging over the size of the image, and we'll want 64 values in our final image. meshgrid is an easy way to make this kind of grid:
[xi,yi]=meshgrid(linspace(1,width(outim),64),linspace(1,height(outim),64));
Now we can interpolate, and use the nearest option which will make sure that everything stays binary.
outim=interp2(outim,xi,yi,'nearest');

Kategorien

Mehr zu Images finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by