perspectiveTransform in JAVA

2,271 views
Skip to first unread message

Erez Farhan

unread,
Jan 31, 2012, 2:09:50 PM1/31/12
to android-opencv
Hi all,
So we do have interface to <findHomography> in java- but can someone
explain what is the use for that if we don't have interface to
<perspectiveTransform>?
In other words, How can I use the derived matrix from <findHomography>
in the current java interface?
To be specific- Can someone suggest a way to apply this found
Homography on a certain Mask [meaning, warping the mask] -say on a
rectangular mask.
Thank you all,
Erez

Rui Marques

unread,
Feb 1, 2012, 11:03:28 AM2/1/12
to android-opencv
Opencv does have perspectiveTransform and warpPerspective.

Example:
H = homography matrix

Core.perspectiveTransform( obj_corners, scene_corners, H);

or

Imgproc.warpPerspective(src_img, dst_img, H, dst_size);

Erez Farhan

unread,
Feb 1, 2012, 11:40:12 AM2/1/12
to android-opencv
Oh,
How the hell did I miss that? silly me
Thank you again and sorry for the stupid question

Erez Farhan

unread,
Feb 2, 2012, 2:23:40 PM2/2/12
to android-opencv
Hi again,
I am having trouble using perspectiveTransform- I can't figure out how
to turn my vector of 2D points into a Mat so that perspectiveTransform
would be satisfied.
All examples I see is on C++ where you can easily cast a vector to a
Mat- can figure out how to do in Java.

To make it simple:
can someone show how to build a matrix of points to be successfully
fed to perspectiveTransform?

Thanks a lot,
Erez

Rui Marques

unread,
Feb 3, 2012, 4:57:18 AM2/3/12
to android-opencv
Yeah I also had to do that. I think this is what you need:

your_mat = Converters.vector_Point_to_Mat( list_of_points );

ster_va7

unread,
Mar 1, 2012, 2:11:34 PM3/1/12
to android-opencv
How do I create my vector of 2D points?? I can figure out how to use
Vector2f on android... how can I create a valid list_of_points?

thanks,
Message has been deleted

Rui Marques

unread,
Mar 2, 2012, 4:55:23 AM3/2/12
to android...@googlegroups.com
You do something like this:

Point pointX = new Point(x,y);

List<Point> list_of_points = new ArrayList<Point>(list_size);

list_of_points.add(pointX);

(...)

ster_va7

unread,
Mar 2, 2012, 5:44:28 AM3/2/12
to android-opencv
Thanks, that is actually what I've been trying but
Imgproc.getPerspectiveTransform doesn't seem to like it...
This is my code:

//First I declare all the variables and initialize an int[] for my src
and dst points


List<Point> src_points;
List<Point> dst_points;
Mat H;
Mat dst_mat;
Mat src_mat;
int[] s_points={120,0,240,0,0,120,360,120};
int[] d_points={0,0,360,0,0,120,360,120};

//Then I initialize all Mat and ArrayList...

H= new Mat();
dst_mat=new Mat();
src_mat=new Mat();
src_points = new ArrayList<Point>(4);
dst_points = new ArrayList<Point>(4);

//I'm adding all the points to the ArrayLists

for(int q = 0; q<8;q=q+2)
{
src_points.add(new Point(s_points[q],
s_points[q+1]));
dst_points.add(new Point(d_points[q],
d_points[q+1]));
}

//I convert the List of points to Mat as this:

dst_mat = Converters.vector_Point_to_Mat(dst_points);
src_mat = Converters.vector_Point_to_Mat(src_points);

//At this point I've got a 4*1*CV_32SC2. It looks good but the code
crashes when calling Imgproc.getPerspectiveTransform... I don't know
how exactly src_mat and dst_mat should be! I've also tried transposing
the mats... Don't know what I am missing

H= Imgproc.getPerspectiveTransform(src_mat, dst_mat);


Thanks,
Esther

Rui Marques

unread,
Mar 2, 2012, 6:10:00 AM3/2/12
to android...@googlegroups.com
What does the error say?

I think (but not 100% sure) that getPerspectiveTransform only works with Mats with type CV_32F or CV_64F so you should probably try using those.

ster_va7

unread,
Mar 2, 2012, 6:30:26 AM3/2/12
to android-opencv
Oh yes that was exactly the problem!!
Thank you Rui
Reply all
Reply to author
Forward
0 new messages