MATLAB Answers

how to write loop coding for dicom image.

조회 수: 61(최근 30일)
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.

채택된 답변

Image Analyst
Image Analyst 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
Image Analyst
Image Analyst 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

댓글을 달려면 로그인하십시오.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by