我编写了可以使用网络摄像头检测拼图区域的代码,并希望将其转换为拼图区域的正方形图像以进行进一步分析:
所以我想把它映射到一个 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 毫秒就可以在我的笔记本电脑上转换图像:代码(请原谅混乱,做了很多实验)。叠加在原始图像上:

