Ask Your Question
0

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

asked 2017-03-05 03:54:51 -0600

neojh gravatar image

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!

edit retag flag offensive close merge delete

Comments

1 answer

Sort by » oldest newest most voted
0

answered 2017-03-05 11:24:54 -0600

matman gravatar image

Since last commit the image is copied internally because of a bug. So there is no freeman chain code returned any more. If you need it you can open an issue about that.

To your second question: If your object is not closed and has a width of one, the algorithm will check them twice. This is the correct behaviour because it traces the contour and terminates when the start point is reached again. If you want to eliminate them, for example you can map contour points back to an image and delete duplicates.

edit flag offensive delete link more

Comments

Regarding to the contour detection, as you said, if the object is not closed then duplications might be understood. However, I tried another example which is closed, as below:

test_image = 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, 1, 0, 0, 1, 0, 0, 1, 0, 1],
[0, 1, 0, 0, 0, 1, 1, 0, 0, 1],
[0, 1, 0, 0, 0, 0, 0, 0, 0, 1],
[0, 1, 1, 0, 1, 1, 1, 1, 1, 1],
[0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],

], dtype=np.uint8)

Then the contours still contains lots of duplications:

[2, 1],[1, 2],[1, 3],[1 ...(more)

neojh gravatar imageneojh ( 2017-03-05 21:49:00 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-03-05 03:54:51 -0600

Seen: 4,096 times

Last updated: Mar 05 '17