title: Finding best neighbour pixel
body: <p>I am trying to do shape analysis of an object in an image in MATLAB (specifically). To do so, I have found boundary pixels. For each boundary pixel I am calculating it's neighbor using 8-neighborhood theory. Now I am calculating tangent of a point to its only one neighbor (depends how I select clockwise or other way). My algorithm works fine if every pixel has exactly two neighbors. For shapes shown in <a href="https://i.stack.imgur.com/P6mOx.png" rel="nofollow noreferrer">this picture</a> (order 9 X 15 pixels).</p>
  
  <p>But if there are more than 2 neighbors of a pixel then my algorithm gets confused. For example as shown in (order 9 X 15 pixels).</p>
  
  <p><img src="https://i.stack.imgur.com/i0PqQ.png" alt="enter image description here"></p>
  
  <p>I want to take tangent of each boundary pixel with its neighbor pixel in clockwise or anti-clockwise direction and if you notice second image, which is valid boundary image, if I am moving in clockwise direction then neighbor of red pixel would be green and neighbor of green in clockwise direction would be "1" and neighbor of "1" is "2" but then I cannot come back to blue and brown pixel and I cannot visit and take tangent of each boundary pixel with its neighbor pixel. </p>
  
  <p>I have learned graph's node visiting algorithms in which you maintain queue or stack but then in this case I don't just want to visit each pixel but also take tangent of every pixel with only right neighbor pixel according to what direction I am moving. </p>
  
  <p>This is an example problem and similar problems can occur in some other way so I am trying to generate some generic algorithm for it. I will appreciate your help. Thank you.</p>