views:

389

answers:

1

I'm using the Python OpenCV bindings to find the contours in an Image. I'm know looking for the possibility to sort this sequence.

It seems the usual python ways for list sorting don't apply here because of the linked list structure generated from OpenCV.

Do you know a good way to sort the Contours by Size (Area/BoundingRectangle) in python? Is it possible to give some example code?

+1  A: 

You have to be able to look at an entire sequence in order to sort it (easily). Thus you should copy it to sort it.

I would do something like

   contourList = list(<your linked list>)
   def sizeKey(countour):
      <get size from contour>
   contourList.sort(key = sizeKey)

If everything is not being stored in memory already you can also look at external sorting algorithms.

Kathy Van Stone
It seems that the list(my list) thing isn't working. I assume python doesn't know to handle the wrapped seq struct from openCV. But I will convert the sequence to a python list
Janusz
Sorry my fault :) It is working. If you take contours.hrange you get a python version of the list.
Janusz