A weighted mask which can give u the direction of the next pixel is called the direction mask, usually direction masks are used over a binary image .
Code:
DMASK = 4 3 2
5 0 1
6 7 8
Multiply the DMASK with the image block of same size and now the center pixel will be 0 and its the orgin, then u have the direction by means of weights of the near by piixels, and u can traverse the image depening on the direction of u r choice,
google ' chain code' algo which uses direction masks over a binary image for more info
hope it helped u a little