How to fix error: "Index in position 1 is invalid. Array indices must be positive integers or logical values"
19 views (last 30 days)
Show older comments
Hello everyone! I am making an automatic software in MATLAB (R2019b) which converts 2D images (e.g. depth maps, reflective maps etc.) into a 3D point cloud matrix. I have been investigating methods of denoising which finds the neighbours of a single point in a rxr kernel and stores their indices. This process is repeated for all points. I can't seem to find a reliable method of scanning the points on the edges as it will return an error that reads "Index in position 1 is invalid. Array indices must be positive integers or logical values." I thought padding the image in zeros would help but that seems to be only a temporary solution because once I increase the radius of the kernel, the same error pops up. If anyone can help solve this I would really appreciate it! I'll attach the code below:
clc; clear all; close all;
% Image and radius are going to be function variables but for testing purposes are set to constants
Image = [21:2:37; 90:98; 31:2:47; 71:79; 53:2:69; 41:49; 21:29; 81:89; 11:19] %input matrix
Radius = 3; % function variable
r = Radius - 2; % box filter parameter
%Indices of neighbouring points in a moving kernel for point cloud
Image = padarray(Image,[r,r],0,'both');
n = 1; % enables for indice loop
[row,col] = size(Image);
for i = 2: row -1
for j = 2: col - 1
LocationMat=zeros(row,col);
for iKer = -r/2:r/2
for jKer = -r/2:r/2
LocationMat(i + iKer,j + jKer) = 1 %kernel of rxr
Indices(n,:)=find(LocationMat); %finds the indices of the box kernel
end
end
n = n + 1;
end
end
0 Comments
Answers (1)
Cris LaPierre
on 5 Mar 2021
Edited: Cris LaPierre
on 5 Mar 2021
You need to incorprate a method that restricts your indices to the resolution of the pixels of your image. I find min and max work well for this purpose. Perhaps something like this
LocationMat(min(row,max(1,i + iKer)),min(col,max(1,j + jKer)))
4 Comments
Cris LaPierre
on 6 Mar 2021
I am not an expert on what it is you are trying to do, so take that into consideration with my comment. If you use the min/max method, i don't think you need to pad with zeros. The reason you are padding is to acocunt for your kernel.
When you use the min/max, it never extends beyond the edges.
If that ends up not being the problem, I suggest closing this question about the indexing error, and opening a new one about your issue denoising.
See Also
Categories
Find more on Point Cloud Processing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!