MATLAB: How to write loop coding for dicom image.

dicomimage analysisimage processingImage Processing Toolboximage segmentation

Dear all,
i have image dicom (as attached) for SPECT machine. But in this image, there are 72 slice (frame) can see in dicominfo.
But when i want to apply adaptthresh to every image, i have to write as code below for every slice. So then i have to change the number slice every time.
%let say I want to know the slice number 38
I = dicomread('spect128x128', 'frame', 38);
%determine threshold
T = adaptthresh(I, 0.4);
%change grey to binary
BW = imbinarize(I,T);
%open image
figure
imshowpair(I, BW, 'montage')
CC = bwconncomp(BW)
[r, c] = cellfun(@(x) ind2sub(size(BW), x), CC.PixelIdxList, 'UniformOutput', 0)
T = regionprops('table', BW,'Area','Centroid')
My question is, how i want to write for the loop code for the first slice(frame) till last slice. So that i no need to change the number of slice every time, and adaptthresh can apply for all the slice.
Please help me.

Best Answer

Try this:
for sliceIndex = 1 : numSlices
% Read this slice.
fprintf('Reading slice #%d...\n', sliceIndex);
thisImage = dicomread('spect128x128', 'frame', sliceIndex);
% Determine threshold
threshold = adaptthresh(thisImage, 0.4);
% Change grey to binary
BW = imbinarize(thisImage, threshold);
% Display images.
imshowpair(thisImage, BW, 'montage')
drawnow;
% CC = bwconncomp(BW);
% [r, c] = cellfun(@(x) ind2sub(size(BW), x), CC.PixelIdxList, 'UniformOutput', 0)
propTables{sliceIndex} = regionprops('table', BW, 'Area', 'Centroid')
end