How to get pixel neighbors in MATLAB?

computer-vision, matlab

Solution

You could always define a displacement vector

d = [ 1 0; -1 0; 1 1; 0 1; -1 1; 1 -1; 0 -1; -1 -1]; 

Then the neighbors of location `loc =[i j]` are

neighbors = d+repmat(loc,[8 1]);

Hope is useful to you...

Problem

I need to get pixel neighbors in order to get a sequence of boundary points , so my plan is to :- - Find a boundary pixel . - Find its neighbor ( it should be a boundary pixel too ) . - Recursively do this until i reach the starting pixel . How can i get pixel neighbors in MATLAB ?

Original source