Skip to content

JavaScript OpenCV Hough Transform 直线检测

Published: at 11:01 AMSuggest Changes

解释

示例

使用

let src = cv.imread('canvasInput');
let dst = cv.Mat.zeros(src.rows, src.cols, cv.CV_8UC3);
let lines = new cv.Mat();
let color = new cv.Scalar(255, 0, 0);
// 图像转成灰度
cv.cvtColor(src, src, cv.COLOR_RGBA2GRAY, 0);
// 边缘检测
cv.Canny(src, src, 50, 200, 3);
// You can try more different parameters
// 使用霍夫直线检测

cv.HoughLinesP(src, lines, 1, Math.PI / 180, 2, 0, 0);
// draw lines
for (let i = 0; i < lines.rows; ++i) {
  let startPoint = new cv.Point(lines.data32S[i * 4], lines.data32S[i * 4 + 1]);
  let endPoint = new cv.Point(
    lines.data32S[i * 4 + 2],
    lines.data32S[i * 4 + 3]
  );
  cv.line(dst, startPoint, endPoint, color);
}
cv.imshow('canvasOutput', dst);
src.delete();
dst.delete();
lines.delete();

参考文章


Previous Post
JavaScript OpenCV 边缘检测
Next Post
JavaScript OpenCV inRange 颜色过滤