Explain the average of 8 nearest neighbors
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!