2

我想从图像中提取网格,以便能够测量从同一相机拍摄的其他照片上的“事物”。每个方格之间的距离为 1 米。我可以检测线(不完美)我需要帮助以获得更清晰的线,并且还需要以像素为单位获取交点坐标,作为数组。

这里是处理后的图像https://ibb.co/Hn6WYz5 和结果的屏幕截图 https://ibb.co/bgpmDpm

这是我的代码

 const cv2 = require('opencv4nodejs');
    let image1 = cv2.imread('./mypicture.jpg')
    let gray=image1.bgrToGray()
    let dst = gray.canny(500, 800,3)
    let lines= dst.houghLines(2, Math.PI/180.0, 150);
    let points = [];
    let isLine=(pt1,pt2,maxX=2851,maxY=10)=>{
        return Math.abs(pt1.x - pt2.x )<maxX || Math.abs(pt1.y - pt2.y )<maxY
    };

    let pointContain=(pt,maxX=2851,maxY=10)=>{
        return points.some((point)=>{
            return Math.abs(point.x-pt.x)<maxX && Math.abs(point.y-pt.y)<maxY
        });
    };
    for(let i in lines) {
        let rho = lines[i].x;
        let theta = lines[i].y;
        let a = Math.cos(theta)
        let b = Math.sin(theta)
        let x0=a * rho;
        let y0 =  b * rho;
        let max=1600;
        let x1=parseInt(x0 + max * (-b));
        let y1=parseInt(y0 + max * (a));
        let pt1 = new cv2.Point2(x1, y1)
        let pt2 = new cv2.Point2(parseInt(x0 - max * (-b)), parseInt(y0 - max * (a)))

        if(isLine(pt1,pt2) && !pointContain(pt1) && !pointContain(pt2)){//hack to clean lines
            points.push(pt1)
            points.push(pt2)
            console.log(pt1,pt2)
            image1.drawLine(pt1, pt2, new cv2.Vec3(0, 0, 255), 2)

        }
    }
    cv2.imshow('image1',image1)
    cv2.waitKey(0)

4

0 回答 0