c++ - 如何从圆心拟合直线

标签 c++ opencv image-processing

我想像这幅图一样从圆心做拟合线,而不是像 p7、p8、p3、p3、p1 那样找到圆的相交坐标: enter image description here

所以我写了这段代码

     vector<Moments> mu(contours.size() );
  for( int i = 0; i < contours.size(); i++ )
     { mu[i] = moments( contours[i] ,false); }

 vector<Point2f> mc( contours.size() );
  for( int i = 0; i < contours.size(); i++ )
     { mc[i] = Point2f( mu[i].m10/mu[i].m00 , mu[i].m01/mu[i].m00 ); }
      for( int i = 0; i< contours.size(); i++ )
      {
        cout<<" "<<mc[i]<<endl;
      //    circle( contourImage, mc[i], 4,Scalar( 0, 0, 255 ) , 1, 1, 0 );
    fitLine(mc);
    int lefty = (-lines[2]*lines[1]/lines[0])+lines[3];
    int righty = ((image.cols-lines[2])*lines[1]/lines[0])+lines[3];

    line(contourImage,mc[i],Point(309,165),Scalar(255,0,0),2);
  // line(contourImage,mc[i].x,m.c[i].y,Scalar(0,0,125),1,8,0);
   imshow("fitline",contourImage);

但是我得到了这个结果: enter image description here

在此之后我想找到 p7 和 p8 坐标、p5 和 p6 坐标、p4 和 p1 坐标以及 p3 和 p2 坐标 另外我的函数 fitLine 在这里:

 vector<float> fitLine(vector<Point2f>  points){ 
    vector<float> line; 
    cv::fitLine(points,line,CV_DIST_L2,0,0.001,0.001); 
    return line; 
}  

你能帮帮我吗?

最佳答案

我假设你知道半径和中心。你必须画 8 条线,与中心的角度差为 (360/8)。

x2=中心(x)+半径(x)+余弦(θ);

y2 = 中心(y)+ 半径(y)+ sin(θ);

theta = 3.14 *角度/180; 试试这个……

关于c++ - 如何从圆心拟合直线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38135715/

相关文章:

c++ - 如何在不同的内核上运行不同的线程?

c++ - Libevent 仅在第二次 buffer_write 之后写入套接字

c++ - OpenCV GAPI 性能不如预期

python - 实时视频稳定 OpenCV

iphone - cvGetSize 给出错误参数错误

c++ - 错误 : invalid operands of types 'float' and 'int' to binary 'operator%' in c++

c++ - 从类中返回可 move 的成员变量

c++ - 使用图像传输压缩和 boost 库 C++

image-processing - 如何训练tesseract识别低DPI中的小数字?

c - 如何旋转 cvMat 中的值?