Ask Your Question
0

Perspective transformation "not working"

asked 2017-04-24 03:31:19 -0500

balvan2 gravatar image

I am trying some perspective transformation (in Jave) ... so i did this simple example and it does not work. I get distorted result image (like lines from left top corner to right top and right-bottom). I suppose to get minification of origin 512x512 image.

What is wrong?

thanks

public void run2() {
    Mat mat = Imgcodecs.imread(getFilePath("/lena.png"));
    Mat src_mat = new Mat(4, 1, CvType.CV_32FC2);
    Mat dst_mat = new Mat(4, 1, CvType.CV_32FC2);

    src_mat.put(0,0, 0.0,0.0,     500.0,0.0,   500.0,500.0, 500.0,0.0);
    dst_mat.put(0,0, 0.0,0.0,     300.0,0.0,   300.0,300.0, 300.0,0.0);
    Mat perspectiveTransform = Imgproc.getPerspectiveTransform(src_mat, dst_mat);

    Mat dst = mat.clone();

    Imgproc.warpPerspective(mat, dst, perspectiveTransform, new Size(300, 300));
    Imgcodecs.imwrite("result.jpg", dst);
}
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-04-24 05:06:02 -0500

berak gravatar image

updated 2017-04-24 05:46:20 -0500

you simply messed up your src and dst points,

the last (4th) points should be 0.0, 500 in src_mat, and 0.0, 300 in dst_mat.

src_mat.put(0,0, 0.0,     0.0);
src_mat.put(1,0, 500.0,   0.0);
src_mat.put(2,0, 500.0, 500.0);
src_mat.put(3,0, 0.0,   500.0);

dst_mat.put(0,0, 0.0,     0.0);
dst_mat.put(1,0, 300.0,   0.0);
dst_mat.put(2,0, 300.0, 300.0);
dst_mat.put(3,0, 0.0,   300.0);
edit flag offensive delete link more

Comments

Thank you. Yes, this was my mistake. I was probably tired :|. Sorry for asking such question.

balvan2 gravatar imagebalvan2 ( 2017-04-24 09:08:03 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-04-24 03:31:19 -0500

Seen: 121 times

Last updated: Apr 24 '17