[英]How to judge the contour is line or curve by opencv?
首先假設你有一條線,然后應用一些基本的代數。
首先找到直線的斜率和 y 軸截距。 直線的斜率定義為 y 的變化除以 x 的變化。 所以給定兩個點 (x0,y0), (x1,y1):
slope = (y0-y1) / (x0-x1)
使用斜率截距方程(y=mx+b)
並求解 b 可以找到 y 截距:
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
條件中考慮到這一點 - 您不能直接比較浮點數是否相等,因此您需要將它們四舍五入為一些可接受的准確度。 為了保持偽代碼的簡單性,我省略了它。
遲到的答案,但為了記錄:
比較第一個和最后一個點之間的距離與輪廓長度。 如果它們很接近,則輪廓為線狀,否則為曲線。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.