首页 文章

如何通过opencv判断轮廓是线还是曲线?

提问于
浏览
1

Test image

伙计们 . 此图像有两个轮廓 . 我可以用opencv findcontour函数找到它们 . 我想知道的是如何判断哪个轮廓是线,哪个轮廓是曲线?有人可以告诉你怎么做吗?任何建议将被认真考虑 . 提前致谢 .

1 回答

  • 1

    首先假设你有一条线,并应用一些基本的代数 .

    首先找到直线的斜率和y轴截距 . 线的斜率定义为y的变化除以x的变化 . 给定两点(x0,y0),(x1,y1):

    slope = (y0-y1) / (x0-x1)
    

    使用斜率截距方程 (y=mx+b) 找到y截距并求解b:

    y = mx + b
    b = y - mx
    

    所以

    y_intercept = y0 - slope * x0
    

    一旦得到斜率和y轴截距,您只需要遍历轮廓的点并查看是否所有点都落在同一条线上 . 如果他们这样做,你就有了一条线;如果他们不这样做,你就会有一条曲线 .

    由于我不知道你正在使用什么语言,这里是伪代码的整个过程:

    // First assume you have a line - find the slope and y-intercept
    slope = (point[0].y - point[1].y) / (point[0].x - point[1].x);
    y_intercept = point[0].y - (slope * point[0].x);
    
    // Using slope-intercept (y = mx + b), see if the other points are on the same line
    for (n = 0 to numPoints)
    {
        if ((slope * point[n].x + y_intercept) != point[n].y)
        {
            // You've found a point that's not on the line - as soon as you
            // find a point that's not on the line, you know that the contour
            // is not a straight line
        }
    }
    

    请注意,您必须在 if 条件中考虑到这一点 - 您可以't directly compare floating point numbers for equality, so you' ll需要将它们四舍五入到一定程度的准确度 . 我把它留下来保持伪代码简单 .

相关问题