homography-transform
Version:
A robust TypeScript implementation of homography-based transformation between 2D planes, ideal for computer vision and image mapping
37 lines (30 loc) • 1.45 kB
text/typescript
import { Plane, Point } from './Plane';
import { PlaneTransformer, PointPair } from './PlaneTransformer';
// Create two planes: a mobile screen and a camera image
const mobileScreen = new Plane(1080, 1920);
const cameraImage = new Plane(640, 480);
// Define corresponding points between the two planes
const pointPairs: PointPair[] = [
// Corner points
{ source: { x: 0, y: 0 }, target: { x: 0, y: 0 } },
{ source: { x: 1080, y: 0 }, target: { x: 640, y: 0 } },
{ source: { x: 0, y: 1920 }, target: { x: 0, y: 480 } },
{ source: { x: 1080, y: 1920 }, target: { x: 640, y: 480 } },
// Optional: Additional points for better accuracy
{ source: { x: 540, y: 960 }, target: { x: 320, y: 240 } }
];
// Create the transformer
const transformer = new PlaneTransformer(mobileScreen, cameraImage, pointPairs);
// Example: Transform a point from mobile screen to camera image
const sourcePoint: Point = { x: 100, y: 200 };
const transformedPoint = transformer.transform(sourcePoint);
console.log('Source point:', sourcePoint);
console.log('Transformed point:', transformedPoint);
// Check the average transformation error
const error = transformer.getTransformationError();
console.log('Average transformation error:', error);
if (error < 1.0) {
console.log('Test Passed: Transformation error is within acceptable range');
} else {
console.log('Test Failed: Transformation error is too high');
}