Fleczer
Fleczer

Reputation: 63

GetPerspectiveTransform function error | opencv | java

I am trying to make an automatic perspective correction of quadrangle objects.

I am getting error when I am using getPerspectiveTransform function:

OpenCV Error: Assertion failed (src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4) in cv::getPerspectiveTransform

Here is my code:

        Mat originalMat = new Mat();
        originalMat=Imgcodecs.imread("photo.jpg");
        Mat binaryMat = new Mat();

        Imgproc.cvtColor(originalMat, binaryMat, Imgproc.COLOR_BGR2GRAY);
        Imgproc.threshold(binaryMat, binaryMat, 0 , 255, Imgproc.THRESH_OTSU | Imgproc.THRESH_BINARY);

        List<MatOfPoint> contours = new ArrayList<>();
        Imgproc.findContours(binaryMat.clone(), contours, new Mat(), Imgproc.RETR_LIST, Imgproc.CHAIN_APPROX_SIMPLE);

        int largestContour=0, tempContour, largestContourIndex=0;

        if(contours.size()>1)
        for (int i = 0; i < contours.size(); i++) {

            MatOfPoint2f mop2f = new MatOfPoint2f();
            contours.get(i).convertTo(mop2f, CvType.CV_32F);         
            RotatedRect rect = Imgproc.minAreaRect(mop2f);           

            tempContour = rect.boundingRect().width * rect.boundingRect().height;
            if(largestContour < tempContour){
                largestContour = tempContour;
                largestContourIndex = i;
            }
        }


        Rect rectangle = Imgproc.boundingRect(contours.get(largestContourIndex));
        Point[] boundingRectPoints = new Point[4];                              
        boundingRectPoints[0] = new Point(rectangle.x,rectangle.y);
        boundingRectPoints[1] = new Point((rectangle.x+rectangle.width),rectangle.y);
        boundingRectPoints[2] = new Point(rectangle.x+rectangle.width,rectangle.y+rectangle.height);
        boundingRectPoints[3] = new Point(rectangle.x,rectangle.y+rectangle.height);
        MatOfPoint boundingRTMatOfPoint = new MatOfPoint(boundingRectPoints);


        Mat beforeCorrectionMat = new Mat();
        Mat afterCorrectionMat = new Mat();
        contours.get(largestContourIndex).convertTo(beforeCorrectionMat, CvType.CV_32FC2);
        boundingRTMatOfPoint.convertTo(afterCorrectionMat, CvType.CV_32FC2);



        Mat transmtx = Imgproc.getPerspectiveTransform( beforeCorrectionMat, afterCorrectionMat);
        Mat transformed = Mat.zeros(originalMat.height(), originalMat.width(), CvType.CV_8UC1);
        Imgproc.warpPerspective(originalMat, transformed, transmtx, originalMat.size());

I found similar question here: Assertion failed when I'm trying to use getPerspectiveTransform on Android-NDK to transform a perspective image

But it doesn't help me. I would be very grateful for any help.

Upvotes: 1

Views: 673

Answers (1)

OWNER_2PLUS_AI
OWNER_2PLUS_AI

Reputation: 1

This means that you need to type float in the code and wherever you want to use float, you must convert it to int. This error indicates that you must use the value of float in your code.

u should use float in getPerspectiveTransform

Upvotes: 0

Related Questions