How can I save every 5 images in a loop?

1 次查看(过去 30 天)
I have a binary file that's 256x256x256 pixels, displaying a 3D image of a sphere on an XYZ axis. I am trying to write a loop that will save "sliced" images of this object every 4 pixels, resulting in a slowly-increasing circle in the y-axis. The sphere "floats" on the XYZ axis, visible from y=29 to y=141. So I am trying to slice and save images at y=29, y=34, y=39, etc, until I reach y=141. As of now, it will just start at y=29 and save images at every single pixel value from 29 up till 141, so I'm ending up with 112 different images saved to my PC instead of the desired 28.
My code is below:
load ('sphere.mat');
N=141;
for k=29:N
imagesc(squeeze(sphere(:,k,:)));
k=k+4;
imsave();
end
  2 个评论
Walter Roberson
Walter Roberson 2020-7-23
load ('sphere.mat');
N=141;
for k=29:4:N
img = mat2gray(squeeze(sphere(:,k,:)));
filename = sprintf('out%d.png', k);
imwrite(img, filename)
end
spider_plantt
spider_plantt 2020-7-23
Works like an absolute charm! Cannot thank you enough!

请先登录,再进行评论。

回答(0 个)

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by