8

The short question is how to transform a shape like the below one into a rectangle using OpenCV?

enter image description here

Notes:

  1. Shape contours aren't necessarily straight lines - they could be slightly curved.
  2. 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:

  1. Find the sheet contours
  2. Calculate a correction matrix to map the shape area into the proper rectangular area
  3. 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?

Alexander Abakumov
  • 13,617
  • 16
  • 88
  • 129
  • You are not yet on the right track. The transformations you are looking into are linear transformations which can be resembled my matrices, http://docs.opencv.org/3.1.0/da/d54/group__imgproc__transform.html#gaf73673a7e8e18ec6963e3774e6a94b87&gsc.tab=0 . For your task you'll need something highly nonlinear, since you are mapping one arbitrary contour onto another. – tfv Apr 25 '16 at 17:54
  • You may want to look into this: http://stackoverflow.com/questions/32207085/shape-transformers-and-interfaces-opencv3-0 – tfv Apr 25 '16 at 17:57

1 Answers1

0

In this case you are looking to do a non-linear transformation of that image. Traditionally you would use a single transformation of the entire paper to make it into a rectangle. This can be done with only the corners. More complex correction like lens distortion you would need to pick points on the page and assert that they are supposed to be co-planar to generate your transformation matrix. Basically you need to have an understanding of the geometry in your image in order to undistort it. So the geometry you need to find is the 3 dimensional surface of the page.

In order to find the highly nonlinear geometry of your mangled piece of paper, you could take multiple images of the page and triangulate them. If you want to go this route, pick up a copy of Multiple View Geometry by Hartley and Zisserman.

esdanol
  • 356
  • 3
  • 9