dftba4ever
dftba4ever

Reputation: 135

OpenCV: What are the parameters for the WarpPerspective function?

I'm building an application in Java that uses OpenCV. I haven't used the library before, and the Java implementation is a bit lacking in documentation. I need to undo a perspective warped image to make it squared up. I need to transform a trapezoid to a rectangle. Basically I need to stretch the shorter of the two parallel sides to match the length of the longer parallel side. I know I need to compute homographies and use a WarpPerspective function, but I have no idea how to structure this command. I know what a homography is, but I don't know how to declare one and I don't know what to feed into warpPerspective. Let me emphasize, I understand the theory, I just need to learn the text syntax in code.

For the sake of example, let's say my source trapezoid has corners at (0,20), (0,80), (200,0), and (200,100). I then want to end up with a rectangular "stretch" to (0,0), (0,100),(200,0), and (200,200). Can anyone provide a code example? The images are in Mat form.

Upvotes: 2

Views: 2496

Answers (2)

Shivam Goel
Shivam Goel

Reputation: 31

Source Image

Destination Image

java code

corners.add(new Point(215,90));
corners.add(new Point(470,90));
corners.add(new Point(540,240));
corners.add(new Point(150,240));
double maxWidth = 540 - 150 ;//BottomRight.x - BottomLeft.x
double maxHeight = 240 - 90 ; //BottomRight.y - TopLeft.y

target.add(new Point(0,0));
target.add(new Point(maxWidth-1,0));
target.add(new Point(0,maxHeight-1));
target.add(new Point(maxWidth-1,maxHeight-1));
trans=Imgproc.getPerspectiveTransform(Converters.vector_Point2f_to_Mat(corners), Converters.vector_Point2f_to_Mat(target));

Imgproc.warpPerspective(img, proj, trans, new Size(maxWidth,maxHeight));

orignal.updateFrame(Utils.fromMatToBufferedImage(img));

proj is destination Mat

Upvotes: 3

zedv
zedv

Reputation: 1469

I don't know if I can help you in Java but in C ++ the solution is quite simple:

// Matrix for getPerspectiveTransform()
Mat H( 2, 4, CV_32FC1 );
//Input and Output Image;
Mat input, output;
input = imread( "example.jpg", 1 );

std::vector<cv::Point2f> trapezoid;
std::vector<cv::Point2f> rectangular;

trapezoid.push_back(cv::Point2f(0,20));
trapezoid.push_back(cv::Point2f(0,80));
trapezoid.push_back(cv::Point2f(200,0));
trapezoid.push_back(cv::Point2f(200,100));

rectangular.push_back(cv::Point2f(0,0));
rectangular.push_back(cv::Point2f(0,100));
rectangular.push_back(cv::Point2f(0,200));
rectangular.push_back(cv::Point2f(200,200));

H = getPerspectiveTransform( trapezoid, rectangular );

warpPerspective( input, output, H,output.size() );

In java you can see these two links that I think perfectly answers all your questions about warpPerspective function:

http://docs.oracle.com/cd/E17802_01/products/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/WarpPerspective.html

http://docs.oracle.com/cd/E17802_01/products/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/PerspectiveTransform.html

I hope this helps you!

Upvotes: 0

Related Questions