我在 python 中编写了一个脚本,该脚本利用了 max() 方法,我试图在 c++ 中重新创建一个类似的程序,但我无法获取值掩码中最大的轮廓。
我曾尝试使用 C++ 算法库中的 max_element() 函数,但无济于事。我也尝试取消引用迭代器但收到一系列错误,这是我的代码:
if (contours.size() > 0)
{
c = *max_element(contours.begin(), contours.end());
//not compiling
}
这里是错误:
no match for 'operator=' (operand types are 'std::vector<std::vector<cv::Point_<int> > >' and 'std::vector<cv::Point_<int> >')
这是我在 Python 中的做法:
if len(contours) > 0;
#find largest contour in mask, use to compute minEnCircle
c = max(contours, key = cv2.contourArea)
(x,y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
最佳答案
在您的 Python 示例中,您传递一个比较器作为 key
争论
c = max(contours, key = cv2.contourArea)
这样做的等价物是将比较器传递给 std::max_element
还有
auto c = *std::max_element(contours.begin(),
contours.end(),
[](std::vector<cv::Point> const& lhs, std::vector<cv::Point> const& rhs)
{
return contourArea(lhs, false) < contourArea(rhs, false);
});
在这种情况下 c
将是 std::vector<cv::Point>
类型代表一个轮廓。
关于python - 如何找到最大的轮廓?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40741398/