【问题标题】:Is there an algorithm for converting a polygon to a square image?是否有将多边形转换为正方形图像的算法?
【发布时间】:2019-11-16 12:32:50
【问题描述】:

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

所以我想把它映射到一个 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(请原谅,我做了很多实验)。叠加在原始图像上:

【问题讨论】:

  • 我很惊讶你可以做这么复杂的事情,但不能做像倾斜变换这样简单的事情。无论如何,你需要什么语言的?
  • 一个简单的解决方案可能是:使用两点 A、B 找到角度,然后旋转图像
  • @AlbertRenshaw 我正在 typescript/javascript 中执行此操作,因为我想在一个简单的网页中执行此操作,但可以从其他语言中找出算法,所以我不希望它添加标签.

标签: algorithm image-processing


【解决方案1】:

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

希望这会有所帮助!

【讨论】:

  • 感谢您提供的链接,它们帮助我找到了我正在寻找的术语,“透视转换”似乎是一个不错的术语。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-24
  • 1970-01-01
  • 2011-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多