Ask Your Question

Revision history [back]

@zms As @berak proposed, you can sort your vector of contours based off of their areas and pick the second element; if sorted in ascending order. To achieve this, you would need to pass a lambda function to sort(). Keep in mind though that the sorting operation is O(N log(N)). If this method deems hard for you to implement; especially with the lambda stuff, then I would recommend following this easier tutorial then convert the logic to use contourArea instead. If done correctly, it should give you the index of the second largest contour and its O(N)

@zms As @berak and @sturkmen proposed, you can sort your vector of contours based off of their areas and pick the second element; if sorted in ascending order. To achieve this, you would need to pass a lambda function to sort(). Keep in mind though that the sorting operation is O(N log(N)). If this method deems hard for you to implement; especially with the lambda stuff, then I would recommend following this easier tutorial then convert the logic to use contourArea instead. If done correctly, it should give you the index of the second largest contour and its O(N)