The short question is how to transform a shape like the below one into a rectangle using OpenCV?
Notes:
- Shape contours aren't necessarily straight lines - they could be slightly curved.
- Content inside the shape must be stretched along both x and y axises accordingly.
The long story:
I'm trying to correct distortions of an image representing a sheet of paper with text, logos etc. after the sheet has been folded 2 or 4 times and make it almost perfectly rectangular.
The input image is already in black and white format with corrected perspective. So all is fine except that it has black areas around distorted edges and text is a bit wavy.
My thought is that the sequence should be something like:
- Find the sheet contours
- Calculate a correction matrix to map the shape area into the proper rectangular area
- Transform the image using the computed matrix
I.e. something like we use to perform perspective correction with Imgproc.findContours
, then finding the sheet corners, followed by Imgproc.getPerspectiveTransform
and eventually use the Imgproc.warpPerspective
to perform correction.
But I can't find the proper algorithms for this case.
Could anybody point how one achives it in image processing?