When drawpolygon(), how to programtically delete a series of vertices?

3 views (last 30 days)
I want to refine the automatically generated mask (denoted as init_mask below). I set corresponding points in drawpolygon().
xys = cell2mat(bwboundaries(init_mask));
x1 = xys(:,2);
y1 = xys(:,1);
ROI_vertices = [x1,y1];
h = drawpolygon(gca,'Position',ROI_vertices);
I want to delete all the points between point 1 and point2, i.e. delete those points around head. For now, I just right click on every point and then delete it. I was wondering if there is any way I can do this programtically?
At first, I tried to use contextMenu property of polygon ROI to retrieve the coordinates of the point I am right clicking on. However, this contextMenu property belongs to the whole ROI object and does not have information about the point I am clicking on. Do you have any thoughts?

Answers (1)

Balavignesh
Balavignesh on 14 Feb 2024
Edited: Balavignesh on 14 Feb 2024
Dear Xingwang,
It appears that you are aiming to programmatically eliminate all points located between point 1 and point 2 in the vicinity of the head region. To facilitate this, I recommend identifying a 'nose point' that will serve as a reference for demarcating the upper facial area. Subsequently, you can modify the polygon to exclude any points that lie above this nose point.
Unfortunately, I do not have access to the 'init_mask' that you are utilizing. Nonetheless, I have constructed my own binary mask to execute the task. The sample code provided below should offer clarity on the concept I've described:
% Create a figure
figure;
subplot(2,1,1)
% Define the size of the mask
maskSize = [300, 300];
% Create a binary mask with a circular object in the middle
[X, Y] = meshgrid(1:maskSize(2), 1:maskSize(1));
center = maskSize / 2;
radius = 75; % Increase radius to make sure the circle is within the mask
binaryMask = sqrt((X - center(2)).^2 + (Y - center(1)).^2) <= radius;
% Display the binary mask
imshow(binaryMask, 'InitialMagnification', 'fit');
hold on;
% Draw a polygon around the entire circular object to represent the face
theta = linspace(0, 2*pi, 100); % Increase the number of points for a smoother circle
circleX = center(2) + radius * cos(theta);
circleY = center(1) + radius * sin(theta);
initialPolygonPosition = [circleX' circleY'];
% Create the polygon object with the initial position
h = drawpolygon(gca, 'Position', initialPolygonPosition);
title('Initial Polygon');
pause(2);
subplot(2,1,2)
imshow(binaryMask);
hold on;
% Define the nose point (e.g., slightly below the center of the face)
nosePoint = center + [0, 10]; % Adjust this value as needed
% Plot the nose point for visualization
plot(nosePoint(1), nosePoint(2), 'r+', 'MarkerSize', 10, 'LineWidth', 2);
% Get the current position of the polygon vertices
currentPosition = h.Position;
% Find the indices of points above the nose point (with a smaller y-coordinate value)
pointsToDelete = currentPosition(:,2) < nosePoint(2);
% Create a logical mask to indicate the points to keep
pointsToKeep = ~pointsToDelete;
% Adjust the logical mask to ensure that the polygon remains closed after deletion
if any(diff(find(pointsToKeep)) > 1)
firstPointBelowNose = find(currentPosition(:,2) >= nosePoint(2), 1);
lastPointBelowNose = find(currentPosition(:,2) >= nosePoint(2), 1, 'last');
pointsToKeep(firstPointBelowNose:lastPointBelowNose) = true;
end
% Update the polygon with the remaining vertices
h = drawpolygon(gca, 'Position', initialPolygonPosition);
h.Position = currentPosition(pointsToKeep, :);
title('Final Polygon')
% Hold off the drawing
hold off;
title('Final Polygon')
% Hold off the drawing
hold off;
Kindly refer to the following documentation links to have more information on:
Hope that helps!
Balavignesh

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!