1

I am trying to write some code to find the contour with the maximum size in a std::vector of contour.

I have the following error

error: conversion from ‘__gnu_cxx::__normal_iterator<std::vector<cv::Point_<int> >*, std::vector<std::vector<cv::Point_<int> > > >’ to non-scalar type
‘std::vector<cv::Point_<int> >::iterator {aka __gnu_cxx::__normal_iterator<cv::Point_<int>*, std::vector<cv::Point_<int> > >}’ requested
    std::vector<cv::Point2i>::iterator it = std::max_element(contours.begin(), contours.end()

Below is my code

std::vector<std::vector <cv::Point2i>> contours;
std::vector<cv::Vec4i> hierarchy;

cv::findContours(rImg, contours, hierarchy,CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE, cv::Point(0, 0));
cv::Mat blank = cv::Mat::zeros(frame.size(), CV_8UC3);
cv::RNG rng;

std::vector<cv::Point2i>::iterator it = std::max_element(contours.begin(), contours.end(),
                                                        [](const std::vector<cv::Point2i>& p1, 
                                                        const std::vector<cv::Point2i>& p2)
                                                        { return p1.size()< p2.size(); });


std::vector<std::vector<cv::Point2i> > contourV;
contourV.push_back(it);

Would like to know what is wrong and how to correct them

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
user1538798
  • 1,075
  • 3
  • 17
  • 42

1 Answers1

2

You are using an object of the type

std::vector<std::vector <cv::Point2i>> contours;

in the std::max_element algorithm

So the iterator shall correspond to that container. That is

std::vector<std::vector <cv::Point2i>>::iterator it = std::max_element( /*...*/ );

Or it would be even simpler to write

auto it = std::max_element(contours.begin(), contours.end(),
                           [](const std::vector<cv::Point2i>& p1, 
                              const std::vector<cv::Point2i>& p2)
                              { return p1.size()< p2.size(); });

as @melpomene pointed out in a comment.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335