小趴菜_001 2022-09-23 19:23 采纳率: 81%
浏览 12
已结题

关于#c++#和#opencv#拟合圆的问题,如何解决?

问题遇到的现象和发生背景
用代码块功能插

```c++

//最小二乘法你和圆
static bool CircleInfo2(std::vector<cv::Point2f>& pts, cv::Point2f& center, float& radius) {
    center = cv::Point2d(0, 0);
    radius = 0.0;
    if (pts.size() < 3) return false;;

    double sumX = 0.0;
    double sumY = 0.0;
    double sumX2 = 0.0;
    double sumY2 = 0.0;
    double sumX3 = 0.0;
    double sumY3 = 0.0;
    double sumXY = 0.0;
    double sumX1Y2 = 0.0;
    double sumX2Y1 = 0.0;
    const double N = (double)pts.size();
    for (int i = 0; i < pts.size(); ++i) {
        double x = pts.at(i).x;
        double y = pts.at(i).y;
        double x2 = x * x;
        double y2 = y * y;
        double x3 = x2 * x;
        double y3 = y2 * y;
        double xy = x * y;
        double x1y2 = x * y2;
        double x2y1 = x2 * y;

        sumX += x;
        sumY += y;
        sumX2 += x2;
        sumY2 += y2;
        sumX3 += x3;
        sumY3 += y3;
        sumXY += xy;
        sumX1Y2 += x1y2;
        sumX2Y1 += x2y1;
        
    }
    double C = N * sumX2 - sumX * sumX;
    double D = N * sumXY - sumX * sumY;
    double E = N * sumX3 + N * sumX1Y2 - (sumX2 + sumY2) * sumX;
    double G = N * sumY2 - sumY * sumY;
    double H = N * sumX2Y1 + N * sumY3 - (sumX2 + sumY2) * sumY;

    double denominator = C * G - D * D;
    if (std::abs(denominator) < DBL_EPSILON) return false;
    double a = (H * D - E * G) / (denominator);
    denominator = D * D - G * C;
    if (std::abs(denominator) < DBL_EPSILON) return false;
    double b = (H * C - E * D) / (denominator);
    double c = -(a * sumX + b * sumY + sumX2 + sumY2) / N;

    center.x = a / (-2);
    center.y = b / (-2);
    radius = std::sqrt(a * a + b * b - 4 * c) / 2;
    return true;
}
//下面是应用

//画拟合圆
    Mat drawcircle = Mat(img.rows, img.cols, CV_8UC3, Scalar(0, 0, 0));
                   circle(drawcircle, centerP, 1, Scalar(0, 0, 255), 1);//centerP为得到的识别目标的中心
                            
                        
                            //拟合圆,三十个点拟合
                        if (cirV.size() < 30) {
                            cirV.push_back( centerP );
                        }
                        else {
                            float R;
                            //得到拟合的圆心
                            CircleInfo2(cirV, cc, R);
                            circle(drawcircle, cc, 1, Scalar(255, 0, 0),2);

                            cout << endl << "center" << cc.x << "," << cc.y << endl;
                            cirV.erase(cirV.begin());
                        }
                        if (cc.x!=0&&cc.y!=0) {
                            Mat rot_mat = getRotationMatrix2D(cc, 30, 1);
                            cout << endl << "center1" << cc.x << "," << cc.y << endl;

                            float sinA = rot_mat.at<double>(0,1);//sin(60)
                            float cosA = rot_mat.at<double>(0,0);//cso(60)
                            float xx = -(cc.x - centerP.x);
                            float yy = -(cc.y - centerP.y);
                            Point2f resPoint = Point2f(cc.x + cosA * xx - sinA * yy, cc.y + sinA * xx + cosA * yy);
                            circle(img, resPoint, 1, Scalar(0, 255, 0), 10);
                            
                            
                        }
                        
                        }
                                                                                                            
                    }

                    
                }
                namedWindow("circle", WINDOW_FREERATIO);
                imshow("circle", drawcircle);

运行结果及报错内容

img

为什么我的圆不能显示出来,运行的时候也没有报错
  • 写回答

1条回答 默认 最新

  • CSDN-Ada助手 CSDN-AI 官方账号 2022-09-23 20:08
    关注
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 2月24日
  • 已采纳回答 2月16日
  • 创建了问题 9月23日

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料