Reputation: 365
I'd like to dewarp
an image using python
(opencv/PIL
etc.). I have 4 points of which I know that they should form a rectangular on a plane.
In gimp I can manually dewarp
the picture usingbackwards-correction
, but I'd like to write a program, that doesn't rely on gimp
.
All the functions I have found rely on a transformation matrix
, so I guess it would be sufficient to give me some pointers on how to calculate the correct matrix
.
Greets & thanks for the help
Upvotes: 4
Views: 2447
Reputation: 5422
I don't use python but here is your answer in c++ it should be the same:
transformationMatrix= cv::getPerspectiveTransform(source , dst_pnt); // getting the transformation matrix
cv::warpPerspective(src, quad, transformationMatrix,perspectiveSize,1); // warping
.........................
unwarping
cv::Matx33f unwarp = transformMatrix;
cv::Point3f homogeneous = unwarp.inv() *pTmp; // pTmp is a point in your transformaed frame
cv::Mat unwarpFrame = unwarp.inv() * srcFrame; // in case of a frame
enter code here
Upvotes: 1
Reputation: 1545
To calculate this matrix, use :
cv2.getPerspectiveTransform(src, dst)
With src a list containing your 4 points and dst a list containing the corners of the new image. Be careful, they must be in the right order
Upvotes: 3