I am using warpPerspective() to stabilize video. After calling this function I am left with a a slightly rotate image that leaves empty space within the image rectangle. e.g.
I would like to calculate how much I need to trim/crop the image where it fits within the image's rectangle without empty space.. I cannot figure out how to calculate this. I am imagine this must be a common problem but I could not find any results on stack overflow (other than calculating the contour for stiched images, which I think is very different problem).
Example code:
import urllib
import numpy as np
import cv2
import matplotlib.pyplot as plt
link = "http://i68.tinypic.com/f4mhy0.jpg"
req = urllib.urlopen(link)
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
img = cv2.imdecode(arr,-1)
img=cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
(h, w) = img.shape[:2]
M=np.array([[1.00430091e+00,7.30151802e-03,-1.55476794e+01][-5.88214567e-03,1.00545504e+00,-3.72084380e+00], [4.47652947e-07,3.99215743e-06,1.00000000e+00]])
warped = cv2.warpPerspective(img, M, (w,h))
plt.imshow(warped),plt.show()