Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Opencv Python 3.2: How to get freeman chain code from edge/contours

I'm trying to get Freeman chain code from binary image/edge by using Python version of OpenCV 3.2. However, it seems this feature is not yet implemented in Python. By now, there are just possible method: CHAIN_APPROX_NONE, CHAIN_APPROX_SIMPLE, CHAIN_APPROX_TC89_KCOS and CHAIN_APPROX_TC89_L1. Is it right? Anybody has same requirements as mine?

Another question related to the findContours function. Let's assume I created 10x10 images with numpy array as following:

np.array([
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 1, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 1, 0, 0, 1, 0, 0],
[0, 0, 0, 0, 0, 1, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

], dtype=np.uint8)

cv2.findContours(test_image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)

Returned list coordinate contains lots duplication:

   [[[2, 1]],

   [[1, 2]],

   [[1, 3]],

   [[1, 2]],

   [[2, 1]],

   [[3, 2]],

   [[4, 3]],

   [[4, 4]],

   [[5, 5]],

   [[6, 5]],

   [[7, 4]],

   [[8, 3]],

   [[7, 4]],

   [[6, 5]],

   [[5, 5]],

   [[4, 4]],

   [[4, 3]],

   [[3, 2]]]

Anyone can explain why there are so many duplications, and the order of coordinate seems very mess. Thanks!