Skip to content Skip to sidebar Skip to footer

Can't Get Opencv's Warpperspective To Work On Android

I've been struggling to implement a quad to quad system in my Android application. The aim is to let the user take a picture, add 4 cornerpoints and have that quad extracted from t

Solution 1:

Found it, the problem was in these lines:

MatperspectiveTransform=newMat(3, 3, CvType.CV_32FC1);
Core.perspectiveTransform(startM, endM, perspectiveTransform);

Which should be replaced by this:

MatperspectiveTransform= Imgproc.getPerspectiveTransform(startM, endM);

Solution 2:

I had some problems with your code, so I made changes until I get a working version, here is my code if someone had problems with the question code:

Bitmap warp(Bitmap image, Point topLeft, Point topRight, Point bottomLeft, Point bottomRight) {
    intresultWidth= (int)(topRight.x - topLeft.x);
    intbottomWidth= (int)(bottomRight.x - bottomLeft.x);
    if(bottomWidth > resultWidth)
        resultWidth = bottomWidth;

    intresultHeight= (int)(bottomLeft.y - topLeft.y);
    intbottomHeight= (int)(bottomRight.y - topRight.y);
    if(bottomHeight > resultHeight)
        resultHeight = bottomHeight;

    MatinputMat=newMat(image.getHeight(), image.getHeight(), CvType.CV_8UC1);
    Utils.bitmapToMat(image, inputMat);
    MatoutputMat=newMat(resultWidth, resultHeight, CvType.CV_8UC1);

    List<Point> source = newArrayList<>();
    source.add(topLeft);
    source.add(topRight);
    source.add(bottomLeft);
    source.add(bottomRight);
    MatstartM= Converters.vector_Point2f_to_Mat(source);

    PointocvPOut1=newPoint(0, 0);
    PointocvPOut2=newPoint(resultWidth, 0);
    PointocvPOut3=newPoint(0, resultHeight);
    PointocvPOut4=newPoint(resultWidth, resultHeight);
    List<Point> dest = newArrayList<>();
    dest.add(ocvPOut1);
    dest.add(ocvPOut2);
    dest.add(ocvPOut3);
    dest.add(ocvPOut4);
    MatendM= Converters.vector_Point2f_to_Mat(dest);

    MatperspectiveTransform= Imgproc.getPerspectiveTransform(startM, endM);

    Imgproc.warpPerspective(inputMat, outputMat, perspectiveTransform, newSize(resultWidth, resultHeight));

    Bitmapoutput= Bitmap.createBitmap(resultWidth, resultHeight, Bitmap.Config.ARGB_8888);
    Utils.matToBitmap(outputMat, output);
    return output;
}

Post a Comment for "Can't Get Opencv's Warpperspective To Work On Android"