Ricardo Alves
Ricardo Alves

Reputation: 1121

Opencv Warp perspective whole image

I'm struggling with this problem:

I have the an image and I want to apply a warp perspective to it (I already have the transformation matrix) but instead of the output only having the transformation area (like the example below) I want to be able to see the whole image instead.

EXAMPLE http://docs.opencv.org/trunk/_images/perspective.jpg

Instead of having the transformation region, like this example, I want to transform the whole original image.

How can I achieve this?

Thanks!

Upvotes: 0

Views: 5073

Answers (1)

BConic
BConic

Reputation: 8980

It seems that you are computing the perspective transform by selecting the corners of the sudoku grid in the input image and requesting them to be warped at fixed location in the output image. In your example, it seems that you are requesting the top-left corner to be warped at coordinates (0,0), the top-right corner at (300,0), the bottom-left at (0,300) and the bottom-right at (300,300).

This will always result in the cropping of the image area on the left of the two left corners and above the two top corners (i.e. the image area where x<0 or y<0 in the output image). Also, if you specify an output image size of 300x300, this results in the cropping of the image area on the right to the right corners and below the bottom corners.

If you want to keep the whole image, you need to use different output coordinates for the corners. For example warp TLC to (100, 100), TRC to (400,100), BLC to (100,400) and BRC to (400,400), and specify an output image size of 600x600 for instance.

You can also calculate the optimal coordinates as follows:

  1. Compute the default perspective transform H0 (as you are doing now)
  2. Transform the corners of the input image using H0, and compute the minimum and maximum values for the x and y coordinates of these corners. Let's denote them xmin, xmax, ymin, ymax.
  3. Compute the translation necessary to map the point (xmin,ymin) to (0,0). The matrix of this translation is T = [1, 0, -xmin; 0, 1, -ymin; 0, 0, 1].
  4. Compute the optimised perspective transform H1 = T*H0 and specify an output image size of (xmax-xmin) x (ymax-ymin).

This way, you are guaranteed that:

  • the four corners of your input sudoku grid will form a true square
  • the output image will be translated so that no useful image data is cropped above or to the left of the grid corners
  • the output image will be have sized so that no useful image data is cropped below or to the right of the grid corners

However, this will generate black areas since the ouput image is no longer a perfect rectangle, hence some pixels in the output image won't have any correspondence in the input image.

Edit 1: If you want to replace the black areas with something else, you can initialize the destination matrix as you wish and then set the borderMode parameter of the warpPerspective function to BORDER_TRANSPARENT.

Upvotes: 1

Related Questions