如何找到最大的轮廓
How to find the largest contour?
我已经在 python 中写了一个脚本,该使用 max> max> 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>
类型,代表轮廓。
我假设该集合是根据openCV的 findContours
函数的嵌套向量。
在这种情况下,您可以看到解决方案的示例:使用C
查找最大轮廓基本上,您致电contourArea
获取轮廓所取的实际面积,或者boundingRect
或minAreaRect
以确定可以包含轮廓的区域。取决于您实际需要的。
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 如何找出GDB的SIGTRAP核心转储的根本原因
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 函数何时会在c++中包含stack_Unwind_Resume调用
- 找不到QtResource文件中的文件
- Python中的for循环与C++有何不同
- VC++本机单元测试,找不到调试符号
- RegGetValue在当前用户下找不到名称
- 带有 -stdlib=libc++ 的 clang++ 9.0.1 找不到<optional>
- c++找不到具有相同哈希的无序集合元素
- 找不到以下加速库:boost_fiber
- 找不到 FLTK(缺少:FLTK_INCLUDE_DIR)
- 设置 Visual Studio for MPI: 找不到标识符错误
- "assimp/config.h"找不到,但它在文件夹中
- 链接器找不到在虚拟类 c++ 中访问的静态字段的符号
- C++系统找不到指定的文件错误
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",