how to find the differnce between all the neighbouring elements in an array???
Show older comments
i have a 64*64 array i need to find the differnce between all the adjacent elements in the array. for instance, if i take a particular element i need to subtract it from the neighhbouring elemnts
Answers (2)
Thorsten
on 18 Jan 2013
If you want to add all the differences of the center pixel within it's 8-neighborhood, you can construct a filter and apply it to the image:
F = -ones(3); F(2,2) = 1;
Y = conv2(I, F)
5 Comments
maria
on 18 Jan 2013
Image Analyst
on 18 Jan 2013
Edited: Image Analyst
on 18 Jan 2013
Then see Matt's answer. You specify which direction you want, out of 8 possible directions. You can get them one direction at a time if that's what you want. Just be aware that convolution "flips" the window so the direction is 180 from what you might think.
maria
on 18 Jan 2013
Image Analyst
on 18 Jan 2013
Same answer. Use kernels:
[0 -1 0; 0 1 0; 0 0 0]
[0 0 0; 0 1 0; 0 -1 0]
[0 0 0; -1 1 0; 0 0 0]
[0 0 0; 0 1 -1; 0 0 0]
for the 4 different directions.
Matt J
on 18 Jan 2013
If you only want the 4 vertical/horizontal neighbours, it's probably easier (and faster??) to use DIFF.
The diff() command will give you horizontal and vertical differences. Convolution can give you differences along any direction, e.g.,
conv2(A,[0 0 0; 0 1 0; 0 0 -1]) %differences between A(i,j) and A(i-1,j-1)
Categories
Find more on Operators and Elementary Operations 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!