opencv c++方法

#include <opencv2/opencv.hpp>
#include <iostream>

int main() {
    cv::Point2f center(100.0f, 100.0f); // 圆的中心坐标
    float radius = 50.0f; // 圆的半径

    cv::Vec4f line(1.0f, -1.0f, -50.0f, 0.0f); // 直线的一般方程 Ax + By + C = 0

    cv::Point2f intersection1, intersection2;

    // 使用OpenCV的函数计算圆和直线的交点
    bool success = cv::fitLine(cv::Mat(line), line, cv::DIST_L2, 0, 0.01, 0.01);
    if (success) {
        cv::Point2f dir(line[0], line[1]);
        cv::Point2f point(line[2], line[3]);
        
        cv::Point2f p1 = point + 1000.0f * dir;
        cv::Point2f p2 = point - 1000.0f * dir;
        
        // 使用直线上的两个点创建线段
        cv::Vec4f lineSegment(p1.x, p1.y, p2.x, p2.y);

        // 使用cv::circle函数绘制圆
        cv::Mat image(200, 200, CV_8UC3, cv::Scalar(255, 255, 255)); // 创建白色背景的图像
        cv::circle(image, center, radius, cv::Scalar(0, 0, 0), 2); // 绘制圆
        cv::line(image, cv::Point(lineSegment[0], lineSegment[1]), cv::Point(lineSegment[2], lineSegment[3]), cv::Scalar(0, 0, 0), 2); // 绘制直线

        // 使用cv::circle函数绘制交点
        bool intersects = cv::intersection(center, radius, lineSegment, intersection1, intersection2);
        if (intersects) {
            cv::circle(image, intersection1, 4, cv::Scalar(255, 0, 0), -1); // 绘制交点1为红色
            cv::circle(image, intersection2, 4, cv::Scalar(0, 255, 0), -1); // 绘制交点2为绿色
        }

        // 显示图像
        cv::imshow("Circle and Line Intersection", image);
        cv::waitKey(0);
    } else {
        std::cout << "FitLine failed." << std::endl;
    }

    return 0;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值