How to use warpPerspective metod on image and show it on camera preview with Android openCV?

203 Views Asked by At

I've been building an Android app with openCV that finds an intended paper pattern using Homography. And on camera preview show an image over the paper, like where the rectangle is on this picture: found paper

I have the 4 points that I need from the perspectiveTransform and I have managed to show a picture over it with an rectangle ROI but I need to do it with warp perspective since the points don't always make a rectangle sometimes it's a different quadrilateral. The way I tried doing it with warpPerspective only gets the camera view stuck in one frame.

this is my code:

   protected void draw(final Mat src, final Mat dst) {

        if (dst != src) {
            src.copyTo(dst);
        }


        // Outline the found target in green.
        Core.line(dst, new Point(mSceneCorners.get(0, 0)),
                new Point(mSceneCorners.get(1, 0)), mLineColor, 4);
        Core.line(dst, new Point(mSceneCorners.get(1, 0)),
                new Point(mSceneCorners.get(2, 0)), mLineColor, 4);
        Core.line(dst, new Point(mSceneCorners.get(2, 0)),
                new Point(mSceneCorners.get(3, 0)), mLineColor, 4);
        Core.line(dst, new Point(mSceneCorners.get(3,0)),
                new Point(mSceneCorners.get(0, 0)), mLineColor, 4);

        Core.rectangle(dst, new Point(mSceneCorners.get(0, 0)),
                new Point(mSceneCorners.get(2, 0)), mLineColorII, 3);


        Mat a = new Mat(mSketchImage.height(), mSketchImage.width(), mSketchImage.type());
        Imgproc.warpPerspective(mSketchImage, a, mReferenceCorners, a.size());
}

I have the points of the corners in the mReferenceCorners field. I calculated the with this line:

Core.perspectiveTransform(mReferenceCorners, mCandidateSceneCorners, homography);

This is my desired outcome: desired outcome

Can somebody please help me? What am I doing wrong? Thank you.

0

There are 0 best solutions below