I have 2 test images here. My is question is, how to map the square in first image to the quadrilateral in the second image without cropping the image.
Image 1:

Image 2:

Here is my current code using openCV warpPerspective function.
import cv2
import numpy as np
img1_square_corners = np.float32([[253,211], [563,211], [563,519],[253,519]])
img2_quad_corners = np.float32([[234,197], [520,169], [715,483], [81,472]])
h, mask = cv2.findHomography(img1_square_corners, img2_quad_corners)
im = cv2.imread("image1.png")
out = cv2.warpPerspective(im, h, (800,800))
cv2.imwrite("result.png", out)
Result:

As you can see, because of dsize=(800,800) parameter in the warpPerspective function, I can’t get full view of image 1. If I adjust the dsize, the square won’t map properly. Is there any way to resize the output image so that I can get whole picture of image 1?
Yes, but you should realise that the output image might be very large. I quickly wrote the following Python code, but even a 3000 x 3000 image could not fit the output, it is just way too big due to the transformation. Although, here is my code, I hope it will be of use to you.
Create an output image here, I used (3000, 3000) as an example.
By using the old
cvinterface, I wrote directly to the output, and so it does not get cropped. I tried this using thecv2interface, but for some reason it did not work… Maybe someone can shed some light on that?Anyway, this gives a very large image, that contains your original image 1, warped. The entire image will be visible if you specify the output image to be large enough. (Which might be very large indeed!)
I hope that this code may help you.