2

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.

enter image description here

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()
ohsnapy
  • 423
  • 2
  • 6
  • 8
  • Possible duplicate of [How to show the whole image when using OpenCV warpPerspective](http://stackoverflow.com/questions/13063201/how-to-show-the-whole-image-when-using-opencv-warpperspective) – beaker May 01 '16 at 22:06

2 Answers2

1

You should use transform with the same matrix applied on the 4 corners of your original image. So you will know where the corners are going.

mathieu.letombe
  • 343
  • 1
  • 6
0

In motion_stabilizing.cpp there is a function called estimateOptimalTrimRatio. This solves the problem!

ohsnapy
  • 423
  • 2
  • 6
  • 8