How to compare two 3D volumes using dice metric?
12 views (last 30 days)
Show older comments
Hello everyone,
I am trying to compare two segmentations regarding their overlap.
Each Segmentation is defined by N 3D coordinates (2 examples attached)
1) I want to interpolate a volume for every segmentation.
2) I want to create a binary mask for each segmentation (everything inside the volume = 1, outside = 0).
3) I want to calculate the dice metric between volume A and volume B.
So far I have:
% Extract points for the current segmentation
points = ROIs(19).points; %ROIs contains n=30 segmentation,
% Remove duplicate points
unique_points = unique(points, 'rows');
% Calculate the volume of the convex hull
[k1, volume] = convhull(unique_points(:,1), unique_points(:,2), unique_points(:,3));
volumes(i) = volume;
% visualize segmentation volume
figure(1)
trisurf(k1,unique_points(:,1), unique_points(:,2), unique_points(:,3))
How would you create a 3D binary mask and compare two volumes using dice metric.
Many thanks in advance!
Max
0 Comments
Accepted Answer
Jaynik
on 26 Feb 2024
Hi Max,
To create a 3D binary mask and compare two volumes using the Dice metric, you have already made progress by extracting points and calculating the volume of the convex hull. The next step is to create a binary mask for each volume. You can use the 'inpolyhedron' function available on File Exchange to obtain the binary mask: https://www.mathworks.com/matlabcentral/fileexchange/37856-inpolyhedron-are-points-inside-a-triangulated-volume
Based on the provided code and the sample files, you can refer the following code to calculate the "Sørensen–Dice" coefficient:
fileID_1 = fopen('Seg1.txt', 'r');
fileID_2 = fopen('Seg2.txt', 'r');
formatSpec = '%f %f %f';
data_1 = textscan(fileID_1, formatSpec);
data_2 = textscan(fileID_2, formatSpec);
fclose(fileID_1);
fclose(fileID_2);
unique_points_1 = [data_1{1}, data_1{2}, data_1{3}];
unique_points_2 = [data_2{1}, data_2{2}, data_2{3}];
[k1, volume1] = convhull(unique_points_1(:,1), unique_points_1(:,2), unique_points_1(:,3));
[k2, volume2] = convhull(unique_points_2(:,1), unique_points_2(:,2), unique_points_2(:,3));
% Plotting the convex hulls
figure;
trisurf(k1, unique_points_1(:,1), unique_points_1(:,2), unique_points_1(:,3), 'FaceColor', 'red', 'FaceAlpha', 0.5);
hold on;
trisurf(k2, unique_points_2(:,1), unique_points_2(:,2), unique_points_2(:,3), 'FaceColor', 'blue', 'FaceAlpha', 0.5);
hold off;
all_points = [unique_points_1; unique_points_2];
% Define the 3D grid bounds (you need to define gridX, gridY, gridZ)
% For example, you can use the min and max of the combined points
gridX = linspace(min(all_points(:,1)), max(all_points(:,1)), 100);
gridY = linspace(min(all_points(:,2)), max(all_points(:,2)), 100);
gridZ = linspace(min(all_points(:,3)), max(all_points(:,3)), 100);
binary_mask_1 = inpolyhedron(k1, unique_points_1, gridX, gridY, gridZ);
binary_mask_2 = inpolyhedron(k2, unique_points_2, gridX, gridY, gridZ);
intersection = binary_mask_1 & binary_mask_2;
dice_metric = 2 * nnz(intersection) / (nnz(binary_mask_1) + nnz(binary_mask_2));
disp(['Dice metric: ', num2str(dice_metric)]);
The grid resolution ("gridX", "gridY", "gridZ") is set to 100 in the sample code can be adjusted based on the input data. A higher resolution will provide a more detailed mask but will require more computation.
Hope this helps!
2 Comments
More Answers (0)
See Also
Categories
Find more on Computational Geometry 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!