简体   繁体   中英

Opencv4Android getPerspectiveTransform error

I want to use getPerspectiveTransform function, but it only accepts Mat as arguments and I have coordinate data in an array. So I convert them into points and then to Mat as follows:

List<Point> l = new ArrayList<Point>();
for (int i = 0; i < pts_1.length; i++) {
    Point pt = new Point(pts_1[0][i], pts_1[1][i]);
    l.add(i,pt);
}
Mat mat1 = Converters.vector_Point2f_to_Mat(l);
for (int i = 0; i < pts_2.length; i++) {
    Point pt = new Point(pts_2[0][i], pts_2[1][i]);
    l.add(i,pt);
    }
Mat mat2 = Converters.vector_Point2f_to_Mat(l);
Mat perspectiveTransform = Imgproc.getPerspectiveTransform(mat1,mat2);

But when I run my app, then it gives me error 'Something went wrong', and in logcat I am getting the following error:

E/cv::error(): OpenCV Error: Assertion failed (src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4) in cv::Mat cv::getPerspectiveTransform(cv::InputArray, cv::InputArray), file /build/master_pack-android/opencv/modules/imgproc/src/imgwarp.cpp, line 6748
E/org.opencv.imgproc: imgproc::getPerspectiveTransform_10() caught cv::Exception: /build/master_pack-android/opencv/modules/imgproc/src/imgwarp.cpp:6748: error: (-215) src.checkVector(2, CV_32F) == 4 && dst.checkVector(2, CV_32F) == 4 in function cv::Mat cv::getPerspectiveTransform(cv::InputArray, cv::InputArray)

I am new to OpenCV4Android so I don't understand why this is happening. How to fix it? Is there any other better way to do this?

Thanks for help!

Note: I know that a similar procedure is followed here: Can't get OpenCV's warpPerspective to work on Android but he is not getting this error, so I am posting it here.

As mentioned in the comment discussion on OP's post (all credit to them), the problem lies with the list l :

List<Point> l = new ArrayList<Point>();
for (int i = 0; i < pts_1.length; i++) {
    Point pt = new Point(pts_1[0][i], pts_1[1][i]);
    l.add(i,pt);
}
Mat mat1 = Converters.vector_Point2f_to_Mat(l);

If we take a look at the contents of the List<Point> l :

for (Point pt : l)
    System.out.println("(" + pt.x + ", " + p.ty + ")");

(x0, y0)
(x1, y1)
(x2, y2)
(x3, y3)

And moving onto the next matrix:

for (int i = 0; i < pts_2.length; i++) {
    Point pt = new Point(pts_2[0][i], pts_2[1][i]);
    l.add(i,pt);
    }
Mat mat2 = Converters.vector_Point2f_to_Mat(l);

Taking another look at the contents of the List<Point> l :

for (Point pt : l)
    System.out.println("(" + pt.x + ", " + p.ty + ")");

(x4, y4)
(x5, y5)
(x6, y6)
(x7, y7)
(x0, y0)
(x1, y1)
(x2, y2)
(x3, y3)

So this is the culprit; your second matrix will have 8 points in it.

From the Java docs for ArrayList :

Parameters: index - index at which the specified element is to be inserted

Using l.add will insert and not overwrite the old list. So the solution would be to make a new list for each transformation matrix.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM