Ask Your Question

CvType to use with Transformation

asked 2015-03-12 16:03:19 -0500

sfkc gravatar image

updated 2015-03-12 16:16:49 -0500

Hello there I am trying to warp a section of an image using OpenCV Java and am failing to do so as I am getting an assertion error. My current code is here. 'bw' is the original matrix of an image and the corners[] array holds the points which border the quadrilateral I want to warp.

Mat src_mat = new Mat(bw.rows(), bw.cols(), CvType.CV_8UC1); 
Mat dst_mat = new Mat(bw.rows(), bw.cols(), CvType.CV_8UC1);
src_mat.put(0, 0, corners[0].x, corners[0].y, corners[1].x, corners[1].y,
                corners[2].x, corners[2].y, corners[3].x, corners[3].y);
dst_mat.put(0, 0, 0.0, 0.0, bw.cols(), 0.0, 0.0, bw.rows(),
                bw.cols(), bw.rows());
Mat pTransform = Imgproc.getPerspectiveTransform(src_mat, dst_mat); //FAILING HERE
Imgproc.warpPerspective(bw, newMatrix, pTransform, new Size(bw.cols(), bw.rows()));

The error is

OpenCV Error: Assertion failed (src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4) in cv::getPerspectiveTransform, file ..\..\..\..\opencv\modules\imgproc\src\imgwarp.cpp, line 3922
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\imgproc\src\imgwarp.cpp:3922: error: (-215) src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4 in function cv::getPerspectiveTransform
at org.opencv.imgproc.Imgproc.getPerspectiveTransform_0(Native Method)
at org.opencv.imgproc.Imgproc.getPerspectiveTransform(
at judge.objectTracking.main(

Any help would be greatly appreciated thank you.

edit retag flag offensive close merge delete


...and the error is ?

berak gravatar imageberak ( 2015-03-12 16:07:24 -0500 )edit

Edited, I apologise

sfkc gravatar imagesfkc ( 2015-03-12 16:10:40 -0500 )edit

no need to apologize ;)

berak gravatar imageberak ( 2015-03-12 16:12:51 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2015-03-12 16:26:15 -0500

berak gravatar image

updated 2015-03-12 16:29:22 -0500

indeed, your Mat's have the wrong type.

if you got an array of Points called 'corners', try like this:

// both must be in the same order / winding !
MatOfPoint2f src = new MatOfPoint2f(
    corners[0], // tl
    corners[1], // tr
    corners[2], // br
    corners[3], // bl

MatOfPoint2f dst = new MatOfPoint2f(
    new org.opencv.core.Point(0,0), // awt has a Point class too, so needs canonical name here
    new org.opencv.core.Point(w,0),
    new org.opencv.core.Point(w.h),
    new org.opencv.core.Point(0,h)

Mat pTransform = Imgproc.getPerspectiveTransform(src, dst);
edit flag offensive delete link more


Thank you this works. I was unsure how to use the MatOfPointXX data types but now I got it. My points are in the wrong order but that's my problem now. Cheers!

sfkc gravatar imagesfkc ( 2015-03-12 16:47:14 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-03-12 16:03:19 -0500

Seen: 1,561 times

Last updated: Mar 12 '15