How to save an average voxel values from multiple ROIs in a dicom slice?

I want to extract an average voxel values form each ROIs drawn on dicom slice. How can I do that? Using the following code I read the dicom slice, then draw 4 rois, now I want to save the ROI intensty statistics such as the average, minimum, maximum and standard deviation in voxel values within the ROI. How to complete the code below to get that done?
close all; clear; clc;
%% load and read dicom slice
info_ct = dicominfo('37.100.dcm');
Height = info_ct.Height;
Width = info_ct.Width;
slope = info_ct.RescaleSlope;
intercept = info_ct.RescaleIntercept;
ct_matrix = slope * double(dicomread(info_ct)) + intercept; % respect slope and intercept
%% Now, draw 4 ROIs on dicom slice
figure
N = 4;
I = imshow(ct_matrix(:,:,1), [-800 1000]);
% Define the Region of Interest
e = drawcircle(gca, 'Label', '1'); % draw 1 ROI
f = drawcircle(gca, 'Label', '2'); % draw 2 ROI
g = drawcircle(gca, 'Label', '3'); % draw 3 ROI
h = drawcircle(gca, 'Label', '4'); % draw 4 ROI
% Create a Mask
BW1 = createMask(e, I); % 1s at ROI and 0s everywhere
BW1 = double(BW1);
BW2 = createMask(f, I);
BW2 = double(BW2);
BW3 = createMask(g, I);
BW3 = double(BW3);
BW4 = createMask(h, I);
BW4 = double(BW4);
....

 Accepted Answer

BW1 = createMask(e, I);
Data=I(BW1);
Now you can calculate the statistics you need.
I would suggest using a struct array:
% Generate example data (yes, it is an MRI, but the code works)
D=load('mri');D=double(D.D);
ct_matrix= ( D/max(D(:)) *1800 ) -800;
f=figure(1);clf(f)
N = 4;
IM=ct_matrix(:,:,1);
I = imshow(IM, [-800 1000]);
set(f,'WindowState','maximized')
% Define the Region of Interests
H=cell(N,1);
for n = 1:N
H{n} = drawcircle(gca, 'FaceAlpha', 0.05, 'Label', sprintf('%d',n));
end
% Extract parameters
output=struct;
for n = 1:N
BW = createMask(H{n}, IM);
data = IM(BW);
%or:
% BW = createMask(H{n}, I.CData);
% data = I.CData(BW);
output(n).mean = mean(data);
output(n).SD = std(data);
end

7 Comments

Thank you for the little for loop.
1.But uisng the sprintf('%d') all ROIs label are gone!
2. Using the following code -- Matlab says "The logical indices contain a true value outside of the array bounds.
Error in check_roi (line 25)
data = I(BW); "
How can I improve this?
close all; clear; clc;
%% load and read dicom slice
info_ct = dicominfo('37.100.dcm');
Height = info_ct.Height;
Width = info_ct.Width;
slope = info_ct.RescaleSlope;
intercept = info_ct.RescaleIntercept;
ct_matrix = slope * double(dicomread(info_ct)) + intercept; % respect slope and intercept
%% Now, draw 4 ROIs on dicom slice
figure
N = 4;
I = imshow(ct_matrix(:,:,1), [-800 1000]);
%set(gcf, 'Position', get(0,'Screensize')); % Maximize figure
% Define the Region of Interests
for n = 1:N
H(n) = drawcircle(gca, 'FaceAlpha', 0.05, 'Label', sprintf('%d'));
end
output=struct;
for n = 1:N
BW = createMask(H(n), I);
data = I(BW);
output(n).mean = mean(data);
output(n).SD = std(data);
end
num2str(n) insetad of sprintf('%d') is working for label and ct_matrix in place of I in the above code seems working. Thank you for your tips for for loop.
That should have been sprintf('%d',n).
About the logical matrix: I'll get back to you when I can test the code on a computer.
You are trying to apply the logical matrix to the handle to the image object, instead of applying it to the image. You either need to define the image separately, or retrieve the CData.
Thank you very much it is working fine now.
Do you have an idea about how can I preserve the size and loaction of ROIs that were drawn uisng this code for another image? I want to draw ROIs in another image; after some transformation/manipulation of the original ct slice? But want to keep the ROIs in same position.
You can store the logical matrices.
Hi Rik, how can I copy ROIs from previous image and apply it to the next image using the following code? Could you give me some insights?
% Generate example data (yes, it is an MRI, but the code works)
D = load('mri');D = double(D.D);
ct_matrix= ( D/max(D(:)) *1800 ) -800;
f=figure(1);clf(f)
N = 4;
IM = ct_matrix(:,:,1);
I = imshow(IM, [-800 1000]);
set(f,'WindowState','maximized')
% Define the Region of Interests
H = cell(N,1);
for n = 1:N
H{n} = drawcircle(gca, 'FaceAlpha', 0.05, 'Label', sprintf('%d',n));
end
% Extract parameters
output = struct;
for n = 1:N
BW = createMask(H{n}, IM);
data = IM(BW);
%or:
% BW = createMask(H{n}, I.CData);
% data = I.CData(BW);
output(n).mean = mean(data);
output(n).SD = std(data);
end
% save mask.mat (ROIs) data
filename = 'ROIs.mat';
save(filename, 'BW');
%% load next image
IM1 = new_matrix(:,:,1);
I1 = imshow(IM1, [-800 1000]);
set(f,'WindowState','maximized')
% Apply the same ROIs (defined above) to this image, data from ROIs.mat file, and save statistics
% how can I move forward?

Sign in to comment.

More Answers (0)

Categories

Find more on Convert Image Type in Help Center and File Exchange

Asked:

on 16 Mar 2020

Commented:

on 18 Mar 2020

Community Treasure Hunt

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

Start Hunting!