Can anyone help me to find out why I can't replace pixel values at (x, y-1) and (x+1, y) with the value at (x, y) ?
1 次查看(过去 30 天)
显示 更早的评论
I am using Matlab and I need your help! Assume that the 8 neighbors of the location (x,y) are:
top-left (x-1,y-1);
top-middle (x,y-1);
top-right (x+1,y-1);
middle-left (x-1,y);
middle-middle(x,y);
middle-right (x+1,y);
bottom-left (x-1,y+1);
bottom-middle(x,y+1);
bottom-right (x+1,y+1);
With the following example (of A = magic(5)), I can only replace the numbers or 'let's say' pixel values at (x-1, y) and (x, y-1) with the value at (x, y). But, I can’t replace the pixel values at (x, y+1) and (x+1, y) with the value at (x, y).
Can any help me to understand why ? In addition, can you please help me to write a code that would replace the pixel values at the locations (x-1, y); (x, y-1); (x, y-1) and (x+1, y) with the value of (x,y) location - without affecting other locations not nearest or adjacent to (x,y)? Many thanks in advance!
A = magic(5);
M1 = size(A,1);
M2 = size(A,2);
E = [5 20];
for x = 2:(M1-1)
for y = 2:(M2-1)
for k = 1:length(E)
if E(k) == A(x,y)
A(x-1,y) = A(x,y);
A(x,y-1) = A(x,y);
end
end
end
end
The expected results should look like this (with reference to E).
A =
17 5 1 8 15
5 5 5 20 16
4 5 20 20 20
10 12 19 20 3
11 18 25 2 9
0 个评论
采纳的回答
Stephen23
2016-1-13
编辑:Stephen23
2016-1-13
N = 5;
E = [5,20];
%
X = magic(N);
Y = false(N+2);
for k = 1:numel(E);
Y(2:end-1,2:end-1) = X==E(k);
Z = Y(1:end-2,2:end-1) | Y(3:end,2:end-1) | Y(2:end-1,3:end) | Y(2:end-1,1:end-2);
X(Z) = E(k);
end
produces this:
X =
17 5 1 8 15
5 5 5 20 16
4 5 20 20 20
10 12 19 20 3
11 18 25 2 9
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Numeric Types 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!