1 | initial version |
If list_area is a std::vector<double> than the largest two values you just get by taking the last two from your sorted vector:
double largest_area = list_area[list_area.size()-1];
double 2nd_largest_area = list_area[list_area.size()-2];
If it is not a std::vector<double>, then either change your list to a vector or access it via the reverse-iterator:
std::list<double>::reverse_iterator rit = list_area.rbegin();
double largest_area = *rit;
double 2nd_largest_area = *(++rit);
Btw: OpenCV also offers a sort and also a sortIdx method, the latter one may be worth considiering, since I guess you also would like the corresponding contours from your largest two areas. Then you just need to convert your vector to a Matrix, then sort by index and take the first two indices. Example: Considering you have your contours as std::vector<std::vector<cv::Point> > all_contours
, and the corresponding sizes in a std::vector<double> sizes
. Then you get the two largest areas in the following way (code not tested):
cv::Mat1d size_mat(1, list_area.size(), list_area);
cv::Mat1i ind;
cv::sortIdx(size_mat, ind, CV_SORT_EVERY_ROW | CV_SORT_DESCENDING);
std::vector<cv::Point> largest_contour = all_contours[ind(0,0)];
std::vector<cv::Point> 2nd_largest_contour = all_contours[ind(0,1)];