Using inpolygon to remove points from a matrix removing additional points
Show older comments
I'm trying to use a user drawn polygon to remove all points from a matrix that lie in/on the polygon. In the following 2D image, the user-drawn polygon is shown on top of the matrix (all plotted points are value 255):

Double checking the points that are identified for removal in the code below:

However, the code I've created is clearly removing a decent chunk of the other points between it and the remaining points where it looks like a square has been cut into the image. The matrix is 3D dimensional. What I've coded is:
plot3(rcv(:,1),rcv(:,2),rcv(:,3)) % Generate image for user to plot polygon
view(2)
roi = drawpolygon('Color','r');
% Get all the values in/on the polygon to delete
xq = (1:max(size(target_cluster))); % Grid X-Coordinates
yq = (1:max(size(target_cluster))); % Grid Y-Coordinates
[XQ,YQ] = meshgrid(xq,yq);
xv = [roi.Position(:,1)' roi.Position(1,1)]; % Polygon X-Coordinates
yv = [roi.Position(:,2)' roi.Position(1,2)]; % Polygon Y-Coordinates
[in,on] = inpolygon(XQ,YQ, xv,yv); % Logical Matrix
inon = in | on; % Combine ‘in’ And ‘on’
idx = find(inon(:)); % Linear Indices Of ‘inon’ Points
xcoord = XQ(idx)'; % X-Coordinates Of XinonQ Points
ycoord = YQ(idx)';
% 'delete' the coordinates by setting their value to 0.
target_cluster(xcoord,ycoord,:) = 0;
5 Comments
xcoord = XQ(inon)'; % X-Coordinates Of XinonQ Points
ycoord = YQ(inon)';
Catalytic
on 1 Feb 2024
the code I've created is clearly removing a decent chunk of the other points between it and the remaining points where it looks like a square has been cut into the image.
To me at least, it isn't clear. Aren't the purple points the ones you want to remove?
Cameron
on 1 Feb 2024
Cameron
on 1 Feb 2024
Matt J
on 1 Feb 2024
It wasn't intended to do anything different, just to show you that find() is superfluous.
As for your difficulty, I, like @Catalytic, am waiting for you to demonstrate the problem. If the code you've posted is meant to do that, you haven't given us the means to run it. You would need to provide (XQ,YQ, xv,yv).
Accepted Answer
More Answers (0)
Categories
Find more on Neighborhood and Block Processing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!