image 您所在的位置:网站首页 是直线还是曲线 image

image

2024-06-27 09:34| 来源: 网络整理| 查看: 265

Test image

该图像有两个轮廓。我可以使用 opencv findcontour 函数找到两者。我想知道的是如何判断哪个轮廓是直线,哪个轮廓是曲线?谁能告诉我该怎么做吗?

最佳答案

首先假设您有一条线,并应用一些基本代数。

首先求直线的斜率和 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 } }

请注意,您将在这里处理 float ,因此您必须在 if 条件中考虑到这一点 - 您不能直接比较 float 是否相等,因此您需要将它们舍入到某种可接受的准确度。为了使伪代码简单,我省略了它。

关于image - opencv如何判断轮廓是直线还是曲线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40668711/



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有