代码之家  ›  专栏  ›  技术社区  ›  Jason Goemaat

有没有把多边形转换成正方形的算法?

  •  0
  • Jason Goemaat  · 技术社区  · 6 年前

    我已经编写了一些代码,可以用我的网络摄像头检测谜题区域,并希望将其转换为谜题区域的方形图像以供进一步分析:

    puzzle area

    所以我想把它映射到一个640x480的区域。我的第一个想法是做一些类似伪代码的事情,但我认为可能有一个定义良好的算法,我还没有找到。。。

      // x, y is from 0,0 to 639,479 in the target image
      let topX = B.x - A.x;
      let topY = B.y - A.y;
      let bottomX = C.x - D.x;
      let bottomY = C.y - D.y;
    
      pointAt(x, y) {
        let frac = x / 639; // how far along line from A to B or D to C
    
        // point along top AB line for x position
        let top = { x: A.x + (frac * topX), y: A.y + (frac * topY) };
    
        // point along bottom CD line for x position
        let bottom = { x: D.x + (frac * bottomX), y: D.y + (frac * bottomY) };
    
        // now get point along line from top to bottom
        let dx = bottom.x - top.x;
        let dy = bottom.y - top.y;
        frac = y / 479;
        return { x: top.x + (frac * dx), y: top.y + (frac * dy) };
      }
    

    使现代化

    到目前为止,我使用了我的伪代码,它运行得很好,速度足够快,可以在我的笔记本电脑上转换图像,大约20毫秒: code (请原谅这一团糟,做了很多实验)。覆盖在原始图像上:

    enter image description here

    0 回复  |  直到 6 年前
        1
  •  2
  •   Phenomenal One    6 年前

    可以在中使用透视变换算法 OpenCV BoofCV .如果您打算为此编写自己的代码,我建议您阅读 this 博客

    希望这有帮助!