Explain the average of 8 nearest neighbors

조회 수: 2 (최근 30일)
A R
A R 2020년 3월 31일
댓글: A R 2020년 4월 3일
Hi, for dead pixel replacement I came across this code from MATLAB file exchange,https://in.mathworks.com/matlabcentral/fileexchange/4551-inpaint_nans . It takes average of 8 nearest neighbors.
% % generate sparse array to average 8 nearest neighbors
% % for each nan element, be careful around edges
fda=spalloc(n*m,n*m,size(nan_list,1)*9);
% -1,-1
L = find((nan_list(:,2) > 1) & (nan_list(:,3) > 1));
nl=length(L);
if nl>0
fda=fda+sparse(repmat(nan_list(L,1),1,2), ...
repmat(nan_list(L,1),1,2)+repmat([-n-1, 0],nl,1), ...
repmat([1 -1],nl,1),n*m,n*m);
end
% eliminate knowns
rhs=-fda(:,known_list)*A(known_list);
% and solve...
B=A;
k=nan_list(:,1);
B(k)=fda(k,k)\rhs(k);
Can you please explain what is [-1, -1]and how the neighbor and its average is calculated in the above code.

답변 (1개)

Steven Lord
Steven Lord 2020년 3월 31일
Consider the following matrix. Each element is equal to its linear index.
>> nRows = 4;
>> nCols = 6;
>> A = reshape(1:(nRows*nCols), [nRows, nCols])
A =
1 5 9 13 17 21
2 6 10 14 18 22
3 7 11 15 19 23
4 8 12 16 20 24
What are the eight neighbors of 18?
Can you find a way to calculate the indices of the neighbors of a point given nothing but the index of the center point and the values nRows and nCols? [Assume that the center point is not on one of the edges of A.]
The left neighbor of C is ...
The right neighbor of C is ...
Repeat for the other six neighbors. Try using the formulae to calculate the indices of the neighbors of a different element, say 11.
  댓글 수: 1
A R
A R 2020년 4월 3일
Hello Steven, thanks a lot for your response. Can you please ellaborate the steps in the above program, how the 8 neighbors values are found .

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by