类型为 'cv::Point2f&' 的非常量引用的初始化无效
invalid initialisation of non-const reference of type 'cv::Point2f&'
我已经在python中写了一个脚本,该脚本称为 cv2.minenclosingcircle 方法,我正在尝试在C 中重新创建类似的程序,但我无法通过半径和中心参考。
我尝试使用有关无效初始化的替代答案,并遵循有关该功能的OpenCV文档,但尚不清楚, i Am able 通过半径和中心没有"&"但是我宁愿不喜欢。
这是有关方法的文档http://docs.opencv.org/2.4/modules/imgproc/doc/sstructur_analsisy_and_and_shape_descriptors.html#minenclosingscircle
这是我的代码:
if (contours.size() > 0)
{
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); });
cv::minEnclosingCircle(c, ¢er, &radius); // not compiling
}
我分别将半径和中心称为 float 和 cv :: Point2f 分别。
Error: invalid initialization of non-const reference of type 'cv::Point2f& {aka cv::Point_<float>& }' from an rvalue of type 'cv::Point2f* {aka cv::Point_<float>*}
这是我在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) #not compiling in c++
M = cv2.moments(c)
您可能不需要使用C 中的&
操作员明确传递变量,这可以简单地完成为:
std::vector<std::vector<cv::Point> > contours;
std::vector<cv::Vec4i> hierarchy;
// Here contours and hierarchy are implicitly passed by reference.
cv::findContours(img, contours, hierarchy, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_NONE);
cv::Point2f center;
float radius;
if (contours.size() > 0) {
// center and radius implicitly passed by reference.
cv::minEnclosingCircle(contours[0], center, radius);
}
std::cout << "Center : " << center << std::endl;
std::cout << "Radius : " << radius << std::endl;
相关文章:
- C++-模板嵌套类的引用初始化无效
- 在引用初始化中使用已删除的复制构造函数进行复制初始化
- 模板流运算符重载错误:引用初始化无效,与basic_istream和basic_ifstream之间的差异有关
- 具有字符串文本构造函数的类不适用于 const 引用初始化
- 类型为"短整型 (&)"的引用初始化无效
- 引用初始化和常量表达式
- 在Visual Studio 2013中为rvalue引用初始化捕获
- 从大括号括起来的初始值设定项列表进行的Lvalue引用初始化无法编译
- Visual C++ 2015 中右值的非常量引用初始化无效
- 是否可以使用对派生类实例的基类引用初始化派生类引用
- 类型'int&'的引用初始化无效,传递参数 1 时出错
- 引用初始化表单
- 从类型为"int*"的临时引用初始化类型为"int&"的非常量引用无效
- 简单的C++日志记录类-ostream引用初始化
- 错误:类型为"cv::Mat&"的非常量引用初始化无效
- 为什么我收到类型为"const vec&"的引用初始化无效
- 从类型为"char*"的临时引用初始化类型为"char*&"的非常量引用
- 为什么这会发送一个关于引用初始化无效的错误
- 为什么此处不对引用初始化执行复制初始化?
- std::vector不会为多个向量条目创建cv::Mat的新引用——初始化矩阵时,数据会被覆盖