opencv-从图像进行线检测(检测网格)

时间:2019-05-15 01:46:37

标签: javascript node.js opencv opencv3.0 opencv4nodejs

我想从图像中提取网格,以便能够测量同一相机拍摄的其他图像上的“物体”。 每个正方形之间的距离是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)

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)

0 个答案:

没有答案