我编写了代码,可以用我的网络摄像头检测拼图区域,并想将其转换为拼图区域的方形图像以供进一步分析:
所以我想把它映射到一个 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 (请原谅,做了很多实验)。叠加在原始图像上:
最佳答案
关于algorithm - 是否有将多边形图像转换为方形图像的算法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56910424/