The short question is, how do I convert a shape like the one below to a rectangle using OpenCV?

Notes:
- The contours of the shape are not necessarily straight lines β they may be slightly curved.
- The content inside the form should be stretched along x and y, respectively.
Long story :
I am trying to correct image distortions representing a sheet of paper with text, logos, etc. After the sheet was folded 2 or 4 times and made it almost perfectly rectangular.
The input image is already in black and white with a fixed perspective. So everything is fine, except that it has black areas around the distorted edges, and the text is a little wavy.
My thought is that the sequence should look something like this:
- Find the outline of the sheet
- Calculate the correction matrix to display the shape area in the correct rectangular area
- Image Conversion Using a Computed Matrix
those. something like what we use to perform perspective correction using Imgproc.findContours , then find the corners of the sheet, then Imgproc.getPerspectiveTransform and eventually use Imgproc.warpPerspective to perform the correction.
But I can not find the right algorithms for this case.
Can anyone indicate how this is achieved in image processing?
source share