0

I have written a script in python that calls the cv2.minEnclosingCircle method, I am trying to recreate a similar program in c++ but I am not able to pass radius and center by reference.

I have tried to use alternative answers regarding invalid initialisation and to follow the openCV documentation regarding the function but it is not clear, I am able to pass radius and center without the '&' however I would prefer not to.

Here is the docs regarding the method http://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#minenclosingcircle

Here is my code:

        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
        }

I have declared radius and center as a float and cv::Point2f types respectively.

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>*}

Here is how I do it in 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)
randy sandy
  • 89
  • 1
  • 14
  • Apologies for the typo Miki! – randy sandy Nov 22 '16 at 17:17
  • 2
    Why are you passing with `&`? Just call `cv::minEnclosingCircle(c, center, radius);`. `center` and `radius` are passed by reference, you don't need the `&` – Miki Nov 22 '16 at 17:17
  • 1
    Possible duplicate of [C++: Argument Passing "passed by reference"](http://stackoverflow.com/questions/19827119/c-argument-passing-passed-by-reference) – Miki Nov 22 '16 at 17:20
  • Purely due to the fact it is represented as such in the docs: C++: void minEnclosingCircle(InputArray points, Point2f& center, float& radius) – randy sandy Nov 22 '16 at 17:23
  • 1
    exactly, they are already captured by reference. You don't need the "&" – Miki Nov 22 '16 at 17:25
  • :O Apologies yet again Miki! That was not clear to me unfortunately -_- – randy sandy Nov 22 '16 at 17:26

1 Answers1

1

You may not need to pass the variables explicitly using & operator in C++, this can be simply done as:

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;
ZdaR
  • 22,343
  • 7
  • 66
  • 87