Closed i7soft closed 3 weeks ago
Assuming you have an array of 4 points that represents the 4 corners of a warped quad:
const matrixPtr = OpenCV.invoke(
'getPerspectiveTransform',
OpenCV.createObject(
ObjectType.Point2fVector,
points.value.map(p =>
OpenCV.createObject(ObjectType.Point2f, p.x, p.y),
),
),
OpenCV.createObject(
ObjectType.Point2fVector,
[
{x: 0, y: 0},
{x: warpedWidth, y: 0},
{x: warpedWidth, y: warpedHeight},
{x: 0, y: warpedHeight},
].map(p => OpenCV.createObject(ObjectType.Point2f, p.x, p.y)),
),
DecompTypes.DECOMP_LU,
);
const destPtr = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8U);
OpenCV.invoke(
'warpPerspective',
srcPtr,
destPtr,
matrixPtr,
OpenCV.createObject(ObjectType.Size, warpedWidth, warpedHeight),
InterpolationFlags.INTER_LINEAR,
BorderTypes.BORDER_CONSTANT,
OpenCV.createObject(ObjectType.Scalar, 0),
);
warpedWidth and warpedHeight is the size of a rectangle you want to transform those 4 points to.. this is just a JS implementation of something like this: https://www.geeksforgeeks.org/perspective-transformation-python-opencv/
As Martin has added an example, I'm closing this ticket. Feel free to open it if necessary.
The perspectiveTransform function is designed for transforming vectors. It can be used like here:
invoke(name: 'perspectiveTransform', src: Mat, dst: Mat, m: Mat): void;
If you want to transform an image using a perspective transformation, use
warpPerspective
. If you have the opposite problem, i.e. you want to calculate the most likely perspective transformation from several pairs of corresponding points, you can usegetPerspectiveTransform
.It was introduced here #12 and in v0.2.7