- Remove the loop for "j = 1:size(mask1,3)" as it is unnecessary.
- Replace the existing nested loops with two new loops, one for the rows (i-direction) and one for the columns (j-direction).
- Modify the code within the new nested loops to find the indices where "mask1" and "mask2" have non-zero elements in the current row and column.
- Adjust the conditions in the loop and distance calculation to properly account for the z-direction.
Calculate the maximum distance between two perimeter of 3d binary masks in a straight line in the z-direction.
4 views (last 30 days)
Show older comments
Hello everyone,
For a project I am attempting to calculate the maximum distance between an ITV and a PTV in the x, y and z-direction. For this first I have made a code with two synthetic 3d binary masks. For the x-direction and the y-direction I was able to get a correct value. For the y-direction it is 1 and x is 2. For the z-direction for the masks I created the distance should be 3. However I am not able to figure it out. Maybe some of you can help. The entire code is down below, the z-direction that I tried is at the bottom.
%% X-direction (Loop)
% Create the two binary masks
mask1 = zeros(10,10,10);
mask1(4:6,4:6,4:6) = 1;
mask1 = bwperim(mask1);
mask2 = zeros(10,10,10);
mask2(2:7,2:8,3:9) = 1; %gaat hier dus om de rows
mask2 = bwperim(mask2);
% Initialize the maximum distances
max_distances = zeros(size(mask1,1), size(mask1,2), size(mask1,3));
% Loop over each slice in the z-direction
for k = 1:size(mask1,3)
% Find the current slices
slice1 = mask1(:,:,k);
slice2 = mask2(:,:,k);
% Find the rows in which slice1 and slice2 have non-zero elements
[row1, col1] = find(slice1);
[row2, col2] = find(slice2);
% Loop over each point in slice1 that is 1
for i = 1:numel(row1)
% Initialize the maximum distance to a point in slice2 that is 1
max_distance = -Inf;
% Loop over each point in slice2 that is 1
for j = 1:numel(row2)
% Check if the row of the current point in slice2 is the same as
% the row of the current point in slice1
if row2(j) == row1(i)
% Calculate the x-distance between the two points
x_distance = col2(j) - col1(i);
% Check if the distance is positive and less than the maximum
% distance found so far, and that there are no points of slice1
% between the two points
if x_distance > 0 && x_distance > max_distance && ~any(slice1(row1(i),col1(i)+1:col2(j)-1))
% Update the maximum distance
max_distance = x_distance;
end
end
end
% Store the maximum distance for the current point in slice1
max_distances(row1(i), col1(i), k) = max_distance;
end
end
% Find the maximum distance over all points and all slices in slice1
max_distance_x = max(max(max_distances(:,:,:)));
max_distance_value_x = max(max_distance_x);
disp(['The maximum distance between the two binary images in the x-direction is ', num2str(max_distance_value_x), '.']);
%% Y-direction (Loop)
% Create the two binary masks
mask1 = zeros(10,10,10);
mask1(4:6,4:6,4:6) = 1;
mask1 = bwperim(mask1);
mask2 = zeros(10,10,10);
mask2(2:7,2:8,3:9) = 1; %gaat hier dus om de rows
mask2 = bwperim(mask2);
% Initialize the maximum distances
max_distances = zeros(size(mask1,1), size(mask1,2), size(mask1,3));
% Loop over each slice in the y-direction
for j = 1:size(mask1,2)
% Find the current slices
slice1 = mask1(:,j,:);
slice2 = mask2(:,j,:);
% Find the columns in which slice1 and slice2 have non-zero elements
[row1, col1] = find(slice1);
[row2, col2] = find(slice2);
% Loop over each point in slice1 that is 1
for i = 1:numel(row1)
% Initialize the maximum distance to a point in slice2 that is 1
max_distance = -Inf;
% Loop over each point in slice2 that is 1
for k = 1:numel(row2)
% Check if the column of the current point in slice2 is the same as
% the column of the current point in slice1
if col2(k) == col1(i)
% Calculate the y-distance between the two points
y_distance = row2(k) - row1(i);
% Check if the distance is positive and less than the maximum
% distance found so far, and that there are no points of slice1
% between the two points
if y_distance > 0 && y_distance > max_distance && ~any(slice1(row1(i)+1:row2(k)-1,col1(i)))
% Update the maximum distance
max_distance = y_distance;
end
end
end
% Store the maximum distance for the current point in slice1
max_distances(row1(i), j) = max(max_distances(row1(i), j), max_distance);
end
end
% Find the maximum distance over all points and all slices in slice1
max_distance_y = max(max(max_distances(:)));
disp(['The maximum distance between the two binary images in the y-direction is ', num2str(max_distance_y), '.']);
%% Z-direction (loop)
% Create the two binary masks
mask1 = zeros(10,10,10);
mask1(4:6,4:6,4:6) = 1;
mask1 = bwperim(mask1);
mask2 = zeros(10,10,10);
mask2(2:7,2:8,3:9) = 1; %gaat hier dus om de rows
mask2 = bwperim(mask2);
% Initialize the maximum distances
max_distances = zeros(size(mask1,1), size(mask1,2), size(mask1,3));
% Loop over each slice in the z-direction
for j = 1:size(mask1,3)
% Find the current slices
slice1 = mask1(j,:,:);
slice2 = mask2(j,:,:);
% Find the columns in which slice1 and slice2 have non-zero elements
[row1, col1] = find(slice1);
[row2, col2] = find(slice2);
% Loop over each point in slice1 that is 1
for i = 1:numel(row1)
% Initialize the maximum distance to a point in slice2 that is 1
max_distance = -Inf;
% Loop over each point in slice2 that is 1
for k = 1:numel(row2)
% Check if the column of the current point in slice2 is the same as
% the column of the current point in slice1
if col2(k) == col1(i)
% Calculate the z-distance between the two points
z_distance = row2(k) - row1(i);
% Check if the distance is positive and less than the maximum
% distance found so far, and that there are no points of slice1
% between the two points
if z_distance > 0 && z_distance > max_distance && ~any(slice1(row1(i)+1:row2(k)-1,col1(i)))
% Update the maximum distance
max_distance = z_distance;
end
end
end
% Store the maximum distance for the current point in slice1
max_distances(row1(i), j) = max(max_distances(row1(i), j), max_distance);
end
end
% Find the maximum distance over all points and all slices in slice1
max_distance_z = max(max(max_distances(:)));
disp(['The maximum distance between the two binary images in the z-direction is ', num2str(max_distance_z), '.']);
0 Comments
Answers (1)
Himanshu
on 25 Apr 2023
Hello Sander,
As per my understanding, you are facing an error while calculating the maximum distance between an ITV (Internal Target Volume) and a PTV (Planning Target Volume) in three dimensions (x, y, and z).
The issue you are facing can be eliminated by making the following changes in the z-direction code:
% Loop over each row in the i-direction
for i = 1:size(mask1,1)
% Loop over each column in the j-direction
for j = 1:size(mask1,2)
% Find the indices in which mask1 and mask2 have non-zero elements in the current row and column
idx1 = find(mask1(i,j,:));
idx2 = find(mask2(i,j,:));
% rest of the code remains the same
end
end
I hope this helps.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!