Create masks from polygon ROI using DICOM images

5 次查看(过去 30 天)
Hello,
for my project I have created ROI polygon segmentation of DICOM images (2D magnetic resonance images), using the ImageLabeler app.
I am trying now to extract the ROI polygon segmentation and create masks to use for my algorithm, and I found this
for i = 1:numPolygons
maskStack(:,:,i) = poly2mask(polygons{i}(:,1), ...
polygons{i}(:,2),imageSize(1),imageSize(2));
end
to convert the polygons into separate masks and insert it into the mask stack.
I have two problems with this, as when I try to run it, it gives me the following error
Index in position 2 exceeds array bounds. Index must not exceed 1.
Error in FL010 (line 15)
polygons{i}(:,2),imageSize(1));
Moreover, each of my magnetic resonance image sets (1 individual), is formed by multiple segmented images/slices and this code only allows me to process a maskstack for one image at a time.
Does anybody know how to fix this problem, or how would it be possible to create and extract the masks from my polygon labelling?
Thank you

回答(1 个)

Ayush
Ayush 2025-7-29
I understand you are working with polygon-based ROI segmentation from DICOM images using the "Image Labeler" app in MATLAB and are crrently looking to extract binary masks for each ROI across multiple image slices (2D MR images).
As per my understanding you are facing two issues:
a. An indexing error during poly2mask execution as described above.
b. You are facing limitations in processing mask stacks across an entire image volume.
Here are some suggestions to address both indexing error and how to organize your workflow for multi-slice data:
1. Some suggestions to fix the Indexing Error:
The error you are facing:
Index in position 2 exceeds array bounds. Index must not exceed 1.
This suggests that polygons{i} is either empty or not structured as expected. The Image Labeler stores ROI data in a label data structure (usually exported as a .mat file), which may not directly give you a cell array of 2-column [x y] coordinates unless explicitly extracted.
To debug the issue you can try making sure that "polygons" is a cell array where each entry is a [Nx2] matrix of X and Y polygon coordinates.
For example:
Try the following command
disp(size(polygons{i}));
You should see something like [N, 2] for each polygon. If instead you see [N, 1], it might mean you need to extract X and Y separately, such as:
x = polygons{i}.Position(:,1);
y = polygons{i}.Position(:,2);
2. Processing a Stack of Labeled Images
If each of your DICOM slices has associated polygon ROIs, you need to handle both:
  • The image slice (usually indexed as slice k)
  • The corresponding ROIs per slice
Assuming you exported the labeled data from the "Image Labeler" app, you can load and loop through the labeled data like this:
load('labeledData.mat'); % contains gTruth
% Get ROI information
data = gTruth.LabelData;
% Initialize volume mask
maskVolume = false(imageHeight, imageWidth, numSlices);
for k = 1:numSlices
% Read DICOM slice
I = dicomread(sliceFiles{k}); % Assuming you have a list of file names
% Get polygons for this slice
polygons = data{k}; % Update depending on gTruth format
% Create empty mask for this slice
sliceMask = false(size(I));
for i = 1:numel(polygons)
% Ensure X and Y are correctly extracted
x = polygons{i}(:,1);
y = polygons{i}(:,2);
sliceMask = sliceMask | poly2mask(x, y, size(I,1), size(I,2));
end
% Store mask in volume
maskVolume(:,:,k) = sliceMask;
end
You can read more about "Image Labeler" app here in the official documentation: https://www.mathworks.com/help/vision/ref/imagelabeler-app.html
You can read more about "poly2mask" function here: https://www.mathworks.com/help/images/ref/poly2mask.html
Hope it helps!

类别

Help CenterFile Exchange 中查找有关 DICOM Format 的更多信息

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by