How can I save every 5 images in a loop?

1 view (last 30 days)
spider_plantt
spider_plantt on 23 Jul 2020
Commented: spider_plantt on 23 Jul 2020
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 Comments
Walter Roberson
Walter Roberson on 23 Jul 2020
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 on 23 Jul 2020
Works like an absolute charm! Cannot thank you enough!

Sign in to comment.

Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!