2016-11-22 34 views
0

のcv2.minEnclosingCircleメソッドを呼び出すスクリプトをPythonで作成しましたが、同様のプログラムをC++で再作成しようとしていますが、参照によって半径と中心を渡します。タイプ 'cv :: Point2f&'の非const参照の無効な初期化

私はしかし、私は「&」せずに半径と中心部を通過すること ことができるよう、私は無効初期に関する選択肢の答えを使用し、機能に関するOpenCVのドキュメントに従うことを試みたが、それは明らかではない、

には好きではありません。ここで

は方法http://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html#minenclosingcircle

に関するドキュメントである。ここに私のコードです:私は種類それぞれフロートCV :: Point2fとして半径と中心部を宣言した

 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 
     } 

が。ここで

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) 
+0

ミキのためのお詫び! –

+2

なぜあなたは '&'で渡していますか?単に 'cv :: minEnclosingCircle(c、center、radius);'を呼び出してください。 'center'と' radius'が参照渡しされているので、 '&' – Miki

+1

は必要ありません。[C++:引数渡し]が重複している可能性があります "(http://stackoverflow.com/questions/19827119/c参照渡しによって渡される) – Miki

答えて

1

あなたが明示的に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; 
関連する問題