Using inpolygon to remove points from a matrix removing additional points
14 visualizaciones (últimos 30 días)
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
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
Jon
el 1 de Feb. de 2024
Editada: Jon
el 1 de Feb. de 2024
I didn't look into your code in detail, but I think your issue is related to your assignment of certain indices in your array to zero. You should use linear indexing for this. Here is a little 2d example to illustrate the issue
% Make an example array
x = rand(5,4)
% Assign x and y indices that you want removed
idx = [1,1,2],idy = [3,4,4];
% Try removing them using the x and y indices, takes out whole corner
x(idx,idy) = 0
% Now let's do it again using linear indexing
x = rand(5,4)
iRemove = sub2ind(size(x),idx,idy)
% Remove using linear indexing, get desired corner cut off
x(iRemove) = 0
5 comentarios
Jon
el 2 de Feb. de 2024
Editada: Jon
el 2 de Feb. de 2024
Here is one way to do it using a loop that loops through each page. There may be some clever way of doing this without any loops, but I think the following is relatively straightforward, and as is often mentioned, the performance penalties for using loops are often not that large or maybe non-existent
% Make example 3-d matrix
X = rand(5,4,6)
% Get number of "pages" in 3rd dimension
sz = size(X);
numPages = sz(3);
% indices of coordinates to be removed
iRow = [1,1,2]
jCol = [3,4,4]
numRemove = numel(iRow); % assume iRow and jRow jave number of elements
for k = 1:numPages
kPage = repmat(k,1,numRemove); % same (current) page for each point
iRemove = sub2ind(size(X),iRow,jCol,kPage);
X(iRemove) = 0;
end
% Display result
disp(X)
Jon
el 2 de Feb. de 2024
If you prefer not to use any loops you could do this.
% Make example 3-d matrix
X = rand(5,4,6)
% Get number of "pages" in 3rd dimension
sz = size(X);
numPages = sz(3);
% indices of coordinates to be removed
iRow = [1,1,2]
jCol = [3,4,4]
numRemove = numel(iRow); % assume iRow and jRow have number of elements
% Get linear indices of these elements on the first page
idx = sub2ind(sz,iRow,jCol)
% Calculate vector of linear indices that will repeat these same row and
% column locations of all of the other pages
% (use scalar expansion to get column of indices for each page)
idx = idx(:) + (0:numPages-1)*sz(1)*sz(2);
iRemove = idx(:); % unwrap columnwise into a vector
% Remove elements
X(iRemove) = 0;
% Display result
disp(X)
Más respuestas (0)
Ver también
Categorías
Más información sobre Geometric Transformation and Image Registration en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!