OpenCV Assertion Failed for Perspective Transform.
For this part of code from this application code
Mat hg = Calib3d.findHomography(obj, scene, 8, 10);
Mat obj_corners = new Mat(4,1,CvType.CV_32FC1);
Mat scene_corners = new Mat(4,1,CvType.CV_32FC1);
Core.perspectiveTransform(obj_corners, scene_corners, hg);
This is the error, also tried changing the type to CV_8UC1, CV_32F and CV_32FC1
08-06 05:37:35.219: E/cv::error()(17414): OpenCV Error: Assertion failed (scn + 1 == m.cols && (depth == CV_32F || depth == CV_64F)) in void cv::perspectiveTransform(cv::InputArray, cv::OutputArray, cv::InputArray), file /home/reports/ci/slave_desktop/50-SDK/opencv/modules/core/src/matmul.cpp, line 1926
08-06 05:37:35.229: W/dalvikvm(17414): threadid=11: thread exiting with uncaught exception (group=0x40ad59f0)
08-06 05:37:35.229: E/AndroidRuntime(17414): FATAL EXCEPTION: Thread-5874
08-06 05:37:35.229: E/AndroidRuntime(17414): CvException [org.opencv.core.CvException: /home/reports/ci/slave_desktop/50-SDK/opencv/modules/core/src/matmul.cpp:1926: error: (-215) scn + 1 == m.cols && (depth == CV_32F || depth == CV_64F) in function void cv::perspectiveTransform(cv::InputArray, cv::OutputArray, cv::InputArray)
08-06 05:37:35.229: E/AndroidRuntime(17414): ]
08-06 05:37:35.229: E/AndroidRuntime(17414): at org.opencv.core.Core.perspectiveTransform_0(Native Method)
08-06 05:37:35.229: E/AndroidRuntime(17414): at org.opencv.core.Core.perspectiveTransform(Core.java:5869)
08-06 05:37:35.229: E/AndroidRuntime(17414): at com.example.objectrecognition.MainActivity.onCameraFrame(MainActivity.java:279)
08-06 05:37:35.229: E/AndroidRuntime(17414): at org.opencv.android.CameraBridgeViewBase.deliverAndDrawFrame(CameraBridgeViewBase.java:376)
08-06 05:37:35.229: E/AndroidRuntime(17414): at org.opencv.android.JavaCameraView$CameraWorker.run(JavaCameraView.java:294)
08-06 05:37:35.229: E/AndroidRuntime(17414): at java.lang.Thread.run(Thread.java:856)
The MainActivity.java:279 referes to the line
Core.perspectiveTransform(obj_corners, scene_corners, hg);
When I use log for the two I get
obj_corners : Mat [ 4*1*CV_32FC1, isCont=true, isSubmat=false, nativeObj=0x677d18, dataAddr=0x7de5a0 ]
scene_corners : Mat [ 4*1*CV_32FC1, isCont=true, isSubmat=false, nativeObj=0x677d58, dataAddr=0x5d9c80 ]
What am I missing here?
The problem is that the homography matrix is a 3x3 Matrix and you are trying perspective transform on a 4x1 Matrix(4 dimensional vector) with a 3x3 matrix. Perspective transform requires a (n-1)x1 source Matrix/vector (when n is the number of columns in the transformation matrix). So it does not fail at the Matrix type, but here: (scn + 1 == m.cols). Your m.cols is 3 and snc +1 = 5.
That's also why the opencv example uses a point2f vector (point2f = point with 2 floats x and y) std::vector<Point2f> obj_corners(4);
Thanks 2 you