类型为 'cv::Point2f&' 的非常量引用的初始化无效

invalid initialisation of non-const reference of type 'cv::Point2f&'

本文关键字:引用 初始化 无效 常量 cv 类型 Point2f 非常      更新时间:2023-10-16

我已经在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, &center, &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;