Cannot use mat.put(int, int, double..), what am I doing wrong?

227 views
Skip to first unread message

Johan Swanberg

unread,
Sep 22, 2018, 6:36:10 PM9/22/18
to javacv
I'm trying to do a warp perspective on an image, but I'm failing. 
I have the following code snippet:


Mat src = new Mat(4, 1, CV_32FC2); //Points describing the edges of the source image
Mat dst = new Mat(4, 1, CV_32FC2); //Points describing where I want the src edges to be moved to

//Below two rows aren't working
src
.put(p1.y, p1.x, p2.y, p2.x, p4.y, p4.x, p3.y, p3.x);
dst
.put(0, 0, 0, originalImgWidth, originalImgHeight, originalImgWidth, originalImgHeight, 0);

Mat perspectiveTransform = getPerspectiveTransform(src, dst);
warpPerspective
(img2, img2, perspectiveTransform, new Size((int) originalImgWidth, (int) originalImgHeight));

The problem I'm encountering with the above code is that src.put, dst.put complains about "cannot resolve method".
If I type src.put, the auto complete only suggests MatExpr, Mat, Scalar and Pointer as valid input parameters. However openCV documentation suggests
there should be a method signature like:

put(int row, int col, double... data)
for the Mat object. Should I avoid using openCV documentation when using javaCV?

Am I going about trying to do a perspective warp the wrong way? Ive noticed that I can access perspectiveTransform through
JavaCV.getPerspectiveTransform, however that gives me a Mat object that's
org.opencv.core.Mat, and incompatible with the other methods.

Here are my imports:


import static org.bytedeco.javacpp.opencv_core.*;
import static org.bytedeco.javacpp.opencv_imgproc.*;
import static org.bytedeco.javacpp.opencv_imgcodecs.*;

I'm thankful for any help.

/Johan

Samuel Audet

unread,
Sep 22, 2018, 7:28:57 PM9/22/18
to jav...@googlegroups.com, Johan Swanberg
We need to create indexers explicitly for this because the operation is a big costly:
    http://bytedeco.org/news/2014/12/23/third-release/

In your case that would look like this:
    FloatIndexer srcIdx = src.createIndexer();
    FloatIndexer dstIdx = dst.createIndexer();
    srcIdx.put(0,  p1.y, p1.x, p2.y, p2.x, p4.y, p4.x, p3.y, p3.x);
    dstIdx.put(0,  0, 0, 0, originalImgWidth, originalImgHeight, originalImgWidth, originalImgHeight, 0);
That should get you on the right track!

Johan Swanberg

unread,
Sep 23, 2018, 2:07:18 PM9/23/18
to javacv
Thank you! That does indeed seem to work.

Now I'm running into another issue with unexpected behavior using the warpPerspective method, but it's most likely due to something I'm doing wrong.
Reply all
Reply to author
Forward
0 new messages