All the while, the example of coding is to calculate the biggest of the smallest area of contour in an image. Is there any idea on how to get the second largest contour area from opencv C++?

This forum is disabled, please visit https://forum.opencv.org

1 | initial version |

All the while, the example of coding is to calculate the biggest of the smallest area of contour in an image. Is there any idea on how to get the second largest contour area from opencv C++?

All the while, the example of coding is to calculate the biggest of the smallest area of contour in an image.
Is there any idea on how to get the second largest contour area from opencv ~~C++?~~C++? I believe i can find the second largest area, but the index .. My code is quite messy sorry for that.

here is to find the biggest one.

```
// iterate through each contour.
for( int i = 0; i< contours.size(); i++ )
{
// Find the area of contour
double a=contourArea( contours[i],false);
arrayarea[i]=a;
if(a>largest_area)
{
largest_area=a;cout<<i<<" area "<<a<<endl;
// Store the index of largest contour
largest_contour_index=i;
cerr << largest_contour_index << "largest_contour_index" << endl;
// Find the bounding rectangle for biggest contour
//bounding_rect=boundingRect(contours[i]);
}
cerr << arrayarea[i] << "arrayarea[i]" << endl;
}
waitKey(0);
first = second = INT_MIN;
for (i = 0; i < arrayarea[i] ; i ++)
{
/* If current element is smaller than first
then update both first and second */
if (arrayarea[i] > first)
{
second = first;
first = arrayarea[i];
}
/* If arr[i] is in between first and
second then update second */
else if (arrayarea[i] > second && arrayarea[i] != first)
second = arrayarea[i];
}
if (second == INT_MIN)
printf("There is no second largest element\n");
else
printf("The second largest element is %dn", second);
```

Copyright OpenCV foundation, 2012-2018. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.