lukaszkurantdev / react-native-fast-opencv

A powerful port of OpenCV for React Native.
https://lukaszkurantdev.github.io/react-native-fast-opencv/
MIT License
66 stars 7 forks source link

how to use "perspectiveTransform" API in Camera "frameProcessor" Event? #13

Closed i7soft closed 3 weeks ago

lukaszkurantdev commented 1 month ago

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 use getPerspectiveTransform.

It was introduced here #12 and in v0.2.7

martinbooth commented 1 month 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/

lukaszkurantdev commented 3 weeks ago

As Martin has added an example, I'm closing this ticket. Feel free to open it if necessary.