Ask Your Question

Sort contours in python

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

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

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

Abid Rahman K gravatar image

It is explained with code in the middle part of this SOF:

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


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

Seen: 2,951 times

Last updated: Jun 04 '14