how to write loop coding for dicom image.

3 views (last 30 days)
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.

Accepted Answer

Image Analyst
Image Analyst on 6 Dec 2020
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
  8 Comments
mohd akmal masud
mohd akmal masud on 7 Dec 2020
is it like this?
for sliceIndex = 43 : 46
% Read this slice.
fprintf('Reading slice #%d...\n', sliceIndex);
thisImage = dicomread('I-131101', 'frame', sliceIndex);
% Determine threshold
threshold = adaptthresh(thisImage, 0.4);
% Change grey to binary
BW = imbinarize(thisImage, threshold);
% Display images.
figure
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')
% [r, c] = cellfun(@(x) ind2sub(size(BW), x), CC.PixelIdxList, 'UniformOutput', 0)
T = regionprops('table', BW,'Area','Centroid')
end
for k = 1 : 3
% Get the table for this image. (REPLACE WITH YOUR ACTUAL CODE)
thisTable = table(1000*rand(10, 1), 1000*rand(10, 2), 'VariableNames', {'Area', 'Centroid'})
% Append the table for this image onto the table for ALL images.
if k == 1
% No table yet, so make the table for the first one the master one.
overallTable = thisTable
else
% Overall table exists already so append thisTable onto that with vertcat().
overallTable = vertcat(overallTable, thisTable)
end
end
Image Analyst
Image Analyst on 7 Dec 2020
No. Just one loop.
% Processes slices 43 through 46.
for sliceIndex = 43 : 46
% Read this slice.
fprintf('Reading slice #%d...\n', sliceIndex);
thisImage = dicomread('I-131101', 'frame', sliceIndex);
% Determine threshold
fprintf(' Getting threshold image...\n');
threshold = adaptthresh(thisImage, 0.4);
% Change grey to binary
fprintf(' Thresholding slice #%d\n', sliceIndex);
BW = imbinarize(thisImage, threshold);
% Display images.
figure
imshowpair(thisImage, BW, 'montage')
drawnow;
% Get the measurements for this one slice.
fprintf(' Making measurements via regionprops()...\n');
thisTable = regionprops('table', BW,'Area','Centroid');
% Append the table for this image onto the table for ALL images.
fprintf(' Appending measurements to table...\n');
if k == 1
% No table yet, so make the table for the first one the master one.
overallTable = thisTable;
else
% Overall table exists already so append thisTable onto that with vertcat().
overallTable = vertcat(overallTable, thisTable);
end
end

Sign in to comment.

More Answers (0)

Categories

Find more on Image Processing Toolbox in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!