Using inpolygon to remove points from a matrix removing additional points

24 views (last 30 days)
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
Matt J
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).

Sign in to comment.

Accepted Answer

Jon
Jon on 1 Feb 2024
Edited: Jon on 1 Feb 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)
x = 5×4
0.1695 0.4728 0.2457 0.7596 0.4200 0.7474 0.9445 0.2262 0.6105 0.4166 0.3947 0.3059 0.1429 0.0023 0.0848 0.7207 0.9435 0.1089 0.6335 0.4535
% Assign x and y indices that you want removed
idx = [1,1,2],idy = [3,4,4];
idx = 1×3
1 1 2
idy = 1×3
3 4 4
% Try removing them using the x and y indices, takes out whole corner
x(idx,idy) = 0
x = 5×4
0.1695 0.4728 0 0 0.4200 0.7474 0 0 0.6105 0.4166 0.3947 0.3059 0.1429 0.0023 0.0848 0.7207 0.9435 0.1089 0.6335 0.4535
% Now let's do it again using linear indexing
x = rand(5,4)
x = 5×4
0.8833 0.0860 0.8114 0.7123 0.5659 0.1062 0.1245 0.5257 0.6030 0.2118 0.2629 0.3114 0.6374 0.5529 0.4754 0.2636 0.3732 0.1420 0.0338 0.8525
iRemove = sub2ind(size(x),idx,idy)
iRemove = 1×3
11 16 17
% Remove using linear indexing, get desired corner cut off
x(iRemove) = 0
x = 5×4
0.8833 0.0860 0 0 0.5659 0.1062 0.1245 0 0.6030 0.2118 0.2629 0.3114 0.6374 0.5529 0.4754 0.2636 0.3732 0.1420 0.0338 0.8525
  5 Comments
Jon
Jon on 2 Feb 2024
Edited: Jon on 2 Feb 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)
X =
X(:,:,1) = 0.7228 0.4083 0.5089 0.1964 0.4855 0.9477 0.1278 0.6732 0.3726 0.8225 0.3869 0.4286 0.2536 0.0373 0.1602 0.5277 0.6876 0.9445 0.2434 0.9791 X(:,:,2) = 0.3561 0.8884 0.2360 0.8117 0.8981 0.3569 0.8848 0.2641 0.0956 0.5146 0.6196 0.1707 0.2148 0.2551 0.5088 0.4535 0.7291 0.6014 0.8648 0.0448 X(:,:,3) = 0.9815 0.5391 0.5564 0.6536 0.9329 0.9367 0.2982 0.0554 0.7986 0.8253 0.2478 0.9675 0.0957 0.4938 0.4426 0.3916 0.0986 0.2656 0.9161 0.3396 X(:,:,4) = 0.1736 0.3683 0.3199 0.0986 0.8140 0.7724 0.2853 0.7954 0.2846 0.6249 0.1674 0.0218 0.1612 0.6296 0.4696 0.2914 0.0304 0.7926 0.2379 0.7218 X(:,:,5) = 0.8619 0.9415 0.4666 0.1291 0.8941 0.2780 0.9041 0.2818 0.9422 0.7186 0.9689 0.1090 0.1272 0.4131 0.9089 0.5182 0.4045 0.9261 0.5799 0.1037 X(:,:,6) = 0.8194 0.6566 0.8531 0.8746 0.0343 0.5744 0.6397 0.8254 0.2440 0.1648 0.8961 0.8997 0.3083 0.7085 0.6023 0.3857 0.2486 0.6462 0.1262 0.8343
% Get number of "pages" in 3rd dimension
sz = size(X);
numPages = sz(3);
% indices of coordinates to be removed
iRow = [1,1,2]
iRow = 1×3
1 1 2
jCol = [3,4,4]
jCol = 1×3
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)
(:,:,1) = 0.7228 0.4083 0 0 0.4855 0.9477 0.1278 0 0.3726 0.8225 0.3869 0.4286 0.2536 0.0373 0.1602 0.5277 0.6876 0.9445 0.2434 0.9791 (:,:,2) = 0.3561 0.8884 0 0 0.8981 0.3569 0.8848 0 0.0956 0.5146 0.6196 0.1707 0.2148 0.2551 0.5088 0.4535 0.7291 0.6014 0.8648 0.0448 (:,:,3) = 0.9815 0.5391 0 0 0.9329 0.9367 0.2982 0 0.7986 0.8253 0.2478 0.9675 0.0957 0.4938 0.4426 0.3916 0.0986 0.2656 0.9161 0.3396 (:,:,4) = 0.1736 0.3683 0 0 0.8140 0.7724 0.2853 0 0.2846 0.6249 0.1674 0.0218 0.1612 0.6296 0.4696 0.2914 0.0304 0.7926 0.2379 0.7218 (:,:,5) = 0.8619 0.9415 0 0 0.8941 0.2780 0.9041 0 0.9422 0.7186 0.9689 0.1090 0.1272 0.4131 0.9089 0.5182 0.4045 0.9261 0.5799 0.1037 (:,:,6) = 0.8194 0.6566 0 0 0.0343 0.5744 0.6397 0 0.2440 0.1648 0.8961 0.8997 0.3083 0.7085 0.6023 0.3857 0.2486 0.6462 0.1262 0.8343
Jon
Jon on 2 Feb 2024
If you prefer not to use any loops you could do this.
% Make example 3-d matrix
X = rand(5,4,6)
X =
X(:,:,1) = 0.1547 0.6908 0.0082 0.0845 0.1983 0.9875 0.5274 0.4167 0.5854 0.1792 0.1095 0.1206 0.6780 0.5580 0.5314 0.0807 0.7006 0.4176 0.4115 0.6519 X(:,:,2) = 0.3425 0.3627 0.7873 0.0809 0.3959 0.3006 0.7177 0.2089 0.4397 0.1453 0.2863 0.4039 0.6207 0.0461 0.5759 0.1471 0.6258 0.3431 0.1623 0.0645 X(:,:,3) = 0.6547 0.1659 0.0520 0.5733 0.3993 0.5376 0.8846 0.0433 0.7358 0.6139 0.7143 0.3560 0.9927 0.1450 0.6719 0.6831 0.2543 0.5028 0.1792 0.1853 X(:,:,4) = 0.5303 0.5546 0.3005 0.0758 0.6853 0.6392 0.2304 0.6021 0.7515 0.1247 0.0014 0.1388 0.0636 0.4550 0.7493 0.1898 0.4966 0.6654 0.2620 0.5268 X(:,:,5) = 0.7332 0.1948 0.1690 0.0047 0.3962 0.1158 0.4472 0.0470 0.9343 0.9918 0.6399 0.5561 0.1760 0.8929 0.9854 0.5776 0.1741 0.2707 0.2486 0.2232 X(:,:,6) = 0.5967 0.5123 0.1762 0.3414 0.0444 0.5175 0.7696 0.4830 0.8057 0.8212 0.7085 0.9446 0.3736 0.5967 0.9443 0.7366 0.1669 0.1121 0.4751 0.3602
% Get number of "pages" in 3rd dimension
sz = size(X);
numPages = sz(3);
% indices of coordinates to be removed
iRow = [1,1,2]
iRow = 1×3
1 1 2
jCol = [3,4,4]
jCol = 1×3
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)
idx = 1×3
11 16 17
% 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)
(:,:,1) = 0.1547 0.6908 0 0 0.1983 0.9875 0.5274 0 0.5854 0.1792 0.1095 0.1206 0.6780 0.5580 0.5314 0.0807 0.7006 0.4176 0.4115 0.6519 (:,:,2) = 0.3425 0.3627 0 0 0.3959 0.3006 0.7177 0 0.4397 0.1453 0.2863 0.4039 0.6207 0.0461 0.5759 0.1471 0.6258 0.3431 0.1623 0.0645 (:,:,3) = 0.6547 0.1659 0 0 0.3993 0.5376 0.8846 0 0.7358 0.6139 0.7143 0.3560 0.9927 0.1450 0.6719 0.6831 0.2543 0.5028 0.1792 0.1853 (:,:,4) = 0.5303 0.5546 0 0 0.6853 0.6392 0.2304 0 0.7515 0.1247 0.0014 0.1388 0.0636 0.4550 0.7493 0.1898 0.4966 0.6654 0.2620 0.5268 (:,:,5) = 0.7332 0.1948 0 0 0.3962 0.1158 0.4472 0 0.9343 0.9918 0.6399 0.5561 0.1760 0.8929 0.9854 0.5776 0.1741 0.2707 0.2486 0.2232 (:,:,6) = 0.5967 0.5123 0 0 0.0444 0.5175 0.7696 0 0.8057 0.8212 0.7085 0.9446 0.3736 0.5967 0.9443 0.7366 0.1669 0.1121 0.4751 0.3602

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!