Using inpolygon to remove points from a matrix removing additional points
Mostrar comentarios más antiguos
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 comentarios
xcoord = XQ(inon)'; % X-Coordinates Of XinonQ Points
ycoord = YQ(inon)';
Catalytic
el 1 de Feb. de 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
el 1 de Feb. de 2024
Cameron
el 1 de Feb. de 2024
Matt J
el 1 de Feb. de 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).
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Neighborhood and Block Processing en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
