Ask Your Question
1

Sort contours in python

asked 2014-06-04 11:49:50 -0600

Solution Developer gravatar image

How can we sort contours from left to right and top to bottom in python

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
3

answered 2014-06-04 21:06:16 -0600

Abid Rahman K gravatar image

It is explained with code in the middle part of this SOF: http://stackoverflow.com/a/11366549/1134940

Below is the extracted part of that answer:

resulting centroids won't be sorted. Check out below image to see their order:

enter image description here

So we sort them from left to right, top to bottom.

centroids = np.array(centroids,dtype = np.float32)
c = centroids.reshape((100,2))
c2 = c[np.argsort(c[:,1])]

b = np.vstack([c2[i*10:(i+1)*10][np.argsort(c2[i*10:(i+1)*10,0])] for i in xrange(10)])
bm = b.reshape((10,10,2))

Now see below their order :

enter image description here

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-06-04 11:49:50 -0600

Seen: 4,135 times

Last updated: Jun 04 '14