Skip to content
Snippets Groups Projects
Commit 3f21fb08 authored by Yuriy Maksymets's avatar Yuriy Maksymets
Browse files

Angle based line checking

parent 3912ba19
No related branches found
No related tags found
1 merge request!65Simple shape recognition
Pipeline #105480 failed
......@@ -73,12 +73,48 @@ function boundingCoords(points) {
}
}
const LINE_Q = 10
function vectorLength([x, y]) {
return Math.hypot(x, y)
}
function diffVector([x0, y0], [x1, y1]) {
return [x0 - x1, y0 - y1]
}
function angleBetweenVectors(p1, p2) {
const [[x0, y0], [x1, y1]] = [p1, p2]
return Math.acos((x0 * x1 + y0 * y1) / (vectorLength(p1) * vectorLength(p2)))
}
const LINE_ANGLE_THRESHOLD = Math.PI / 4
const VECTOR_LEN_THRESHOLD = 400
function couldBeLine(points) {
const { maxX, minX, maxY, minY } = boundingCoords(points)
const [dx, dy] = [maxX - minX, maxY - minY].map((x) => x + 0.00001)
return dy / dx > LINE_Q || dx / dy > LINE_Q
if (points.length < 2) return false
const pivot = points[0]
let cumulativeThreshold = 0
for (let i = 2; i < points.length; i++) {
const p1 = points[i - 1]
const p2 = points[i]
const d1 = diffVector(pivot, p1)
const d2 = diffVector(p1, p2)
const d2Len = vectorLength(d2)
if (
cumulativeThreshold < VECTOR_LEN_THRESHOLD &&
d2Len < VECTOR_LEN_THRESHOLD
) {
cumulativeThreshold += d2Len
continue
}
const angle = angleBetweenVectors(d1, d2)
if (Math.abs(angle) > LINE_ANGLE_THRESHOLD) {
return false
}
}
return true
}
const MATRIX_SIZE = 3
......@@ -186,7 +222,7 @@ function recognizeLine(points) {
if (!(points && points.length)) return {}
const accum = Array(numAngleCells)
const houghConfig = {
rhoStep: points.length > 50 ? 50 : 5,
rhoStep: points.length > 30 ? 50 : 5,
}
points.forEach((x) => constructHoughAccumulator(houghConfig, accum, ...x))
const angle = findMaxInHough(accum, points.length - 1)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment