我写了一些代码,可以用我的摄像头检测难题的区域,并希望将其转换为难题区域的正方形图像以进行进一步分析:
所以我想将其映射到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(原谅混乱,做了很多实验) 。覆盖原始图像: