3

I'm trying to extract coordinates of a curve in an image using OpenCV in Python. However, I need these coordinates ordered in a list such that the first element of this list one extremity of the curve, the second one is the point next to that extremity, etc, right up to the last element being the other extremity.

Say I have an image of the letter "C", imported with openCV as the following numpy array :

 arr=
 [0,0,0,0,0,0,0
  0,0,1,1,1,0,0
  0,0,1,0,0,0,0     
  0,0,1,0,0,0,0
  0,0,1,1,1,0,0
  0,0,0,0,0,0,0]

Then I would like a list returning the coordinates of the pixels in the following order :

 arr=
 [0,0,0,0,0,0,0
  0,0,3,2,1,0,0
  0,0,4,0,0,0,0     
  0,0,5,0,0,0,0
  0,0,6,7,8,0,0
  0,0,0,0,0,0,0]

i.e. a list (coordinates of pixel "1" in the array, coordinates of pixel "2", etc).

This idea of "continuity" is somewhat present in findContours(), but it does not work very well for finding a contour of such a 1-pixel wide object. A lot of pixels come up twice in the resulting contour (not too much of a problem), but it may also not start at one extremity, and yield point coordinates in an order such as

 arr=
 [0,0,0,0,0,0,0
  0,0,1,7,8,0,0
  0,0,2,0,0,0,0     
  0,0,3,0,0,0,0
  0,0,4,5,6,0,0
  0,0,0,0,0,0,0]

This is because the algorithm basically goes from 1 to 6 than back up to 1, goes on to 8 and goes back to 1 in order to yield a "contour". I also tried simple thresholding + numpy.nonzero, it of course does not order them as I wish, it yields in the order

 arr=
 [0,0,0,0,0,0,0
  0,0,1,2,3,0,0
  0,0,4,0,0,0,0     
  0,0,5,0,0,0,0
  0,0,6,7,8,0,0
  0,0,0,0,0,0,0]

Am I missing something big ? I'm not new to Python or image processing, but I've just started OpenCV.

1 Answer 1

2

I don't know about a good opencv implementation for this but you can use Moore-Neighbor Tracing and modfiy it to so it we will set the value of the point that you found in the boudary an incremntal value.

Sign up to request clarification or add additional context in comments.

1 Comment

'Moore-Neighbor Tracing' looks promising I got to try it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.