Using inpolygon to remove points from a matrix removing additional points
显示 更早的评论
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 个评论
xcoord = XQ(inon)'; % X-Coordinates Of XinonQ Points
ycoord = YQ(inon)';
Catalytic
2024-2-1
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
2024-2-1
Cameron
2024-2-1
Matt J
2024-2-1
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).
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Neighborhood and Block Processing 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
