How to fix error: "Index in position 1 is invalid. Array indices must be positive integers or logical values"

19 vues (au cours des 30 derniers jours)
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

Réponses (1)

Cris LaPierre
Cris LaPierre le 5 Mar 2021
Modifié(e) : Cris LaPierre le 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 commentaires
Fezan Tabassum
Fezan Tabassum le 6 Mar 2021
so should i replace the -r/2 : r/2 with a different method? How about something like this:
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);
LocationMat(min(row,max(1,i-r:i+r)),min(col,max(1,j-r:j+r)))
LocationMat(i-r:i+r,j-r:j+r)= 1
Indices(n,:)=find(LocationMat); %finds the indices of the box kernel
n = n + 1;
end
end
I replaced the kernel line to better suit the parameters but i still run into the same problem where I cant find the neighbours of the edge points. I can show a more in depth process if there is a way for us to message but if not, Ill post it here just encase.
Image =
21 23 25 27 29 31 33 35 37
90 91 92 93 94 95 96 97 98
31 33 35 37 39 41 43 45 47
71 72 73 74 75 76 77 78 79
53 55 57 59 61 63 65 67 69
41 42 43 44 45 46 47 48 49
21 22 23 24 25 26 27 28 29
81 82 83 84 85 86 87 88 89
11 12 13 14 15 16 17 18 19
In order for me to get the neighbours of the edge points, I have to pad out the "Image".
Image =
0 0 0 0 0 0 0 0 0 0 0
0 21 23 25 27 29 31 33 35 37 0
0 90 91 92 93 94 95 96 97 98 0
0 31 33 35 37 39 41 43 45 47 0
0 71 72 73 74 75 76 77 78 79 0
0 53 55 57 59 61 63 65 67 69 0
0 41 42 43 44 45 46 47 48 49 0
0 21 22 23 24 25 26 27 28 29 0
0 81 82 83 84 85 86 87 88 89 0
0 11 12 13 14 15 16 17 18 19 0
0 0 0 0 0 0 0 0 0 0 0
This allows me to include the edge points but using padding causes problems in the denoiser.
%this part is me changing the numbers manually in text
Idealy =
@ 1 0 0 0 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
where the @ is the centre point and the 1's are the neighbours. I need a method for the code to notice if the kernel is going past the boundaries of the image and just return a zero or to replicate the matrix to get this values.
Cris LaPierre
Cris LaPierre le 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.

Connectez-vous pour commenter.

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by