summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Jung <flo@windfisch.org>2014-08-27 02:08:51 +0200
committerFlorian Jung <flo@windfisch.org>2014-08-27 02:08:51 +0200
commitec85614111a3b8e4df01ad290eae7a1182d4885f (patch)
treeb4d9e5c2272a818dbdf7be787e7c429358059fed
parent28b48db87f8900e351a90ec6e9d815af8b5e8101 (diff)
parent1a9cf0e213af0de7c447c7085a04f4c7b2c3e4b7 (diff)
Merge branch 'warp_stabilizer'
-rw-r--r--stabilize.py55
-rw-r--r--warp.py76
2 files changed, 89 insertions, 42 deletions
diff --git a/stabilize.py b/stabilize.py
index 3692b2b..ab11b51 100644
--- a/stabilize.py
+++ b/stabilize.py
@@ -28,12 +28,60 @@ lk_params = dict( winSize = (15,15),
###### INITALISATION ######
-ret,oldframe=cap.read()
+
+def get_maps(xres,yres):
+ xlist=[]
+ ylist=[]
+
+ constant = 30./41.5*3
+
+ # calculated from official spec: maximum angle (i.e., over the diagonals)
+ # is 92 deg. -> angle over half a diagonal's length is 92/2 deg
+ constant2= 1./(math.sqrt((1280/2)**2+(720/2)**2))*(92/2)/180*math.pi
+
+ for y in xrange(0,yres):
+ xtmp=[]
+ ytmp=[]
+
+ yy=(y-yres/2.)/xres # yes, xres.
+ for x in xrange(0,xres):
+ xx=math.tan((x-xres/2.)/xres)
+
+ dist = math.sqrt(xx**2 + yy**2)
+
+ if (dist != 0):
+ xtmp=xtmp+[ 1280/2+ xx/dist/constant2*math.atan(constant*dist) ]
+ ytmp=ytmp+[ 720/2 + yy/dist/constant2*math.atan(constant*dist) ]
+ else:
+ xtmp=xtmp+[0+1280/2]
+ ytmp=ytmp+[0+720/2]
+
+ xlist=xlist+[xtmp]
+ ylist=ylist+[ytmp]
+
+ if y % 10 == 0:
+ print y
+
+ xmap=numpy.array(xlist).astype('float32')
+ ymap=numpy.array(ylist).astype('float32')
+
+ return xmap,ymap
+
+xmap,ymap = get_maps(1280,720)
+
+
+
+ret,oldframe_=cap.read()
+rawheight, rawwidth, bpp = oldframe_.shape
+
+oldframe = cv2.remap(oldframe_, xmap, ymap, cv2.INTER_CUBIC)
oldgray=cv2.cvtColor(oldframe,cv2.COLOR_BGR2GRAY)
height, width, bpp = oldframe.shape
-mask = numpy.ones((height,width, 1), numpy.uint8) * 255
+mask_ = numpy.ones((rawheight,rawwidth, 1), numpy.uint8) * 255
+mask = cv2.remap(mask_, xmap, ymap, cv2.INTER_CUBIC)
+
screencontent = numpy.zeros((scr_height, scr_width,3), numpy.uint8)
total_angle=0.
@@ -41,7 +89,8 @@ total_x=1500
total_y=-300
while(cap.isOpened()):
- ret, frame = cap.read()
+ ret, frame_ = cap.read()
+ frame = cv2.remap(frame_, xmap, ymap, cv2.INTER_CUBIC)
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
diff --git a/warp.py b/warp.py
index 7cda232..f6407e9 100644
--- a/warp.py
+++ b/warp.py
@@ -2,48 +2,46 @@ import math
import cv2
import numpy
-xlist=[]
-ylist=[]
-
-xres=1600/2
-yres=900/2
-
-#constant = 1./math.sqrt((xres/2)**2 + (yres/2)**2)
-constant = 30./41.5*3
-# found out experimentally
-constant2= 1./(1280/2.)*math.atan(30./40)
-print constant2
-
-# calculated from official spec: maximum angle (i.e., over the diagonals)
-# is 92 deg. -> angle over half a diagonal's length is 92/2 deg
-constant2= 1./(math.sqrt((1280/2)**2+(720/2)**2))*(92/2)/180*math.pi
-print constant2
+def get_maps(xres,yres):
+ xlist=[]
+ ylist=[]
+
+ constant = 30./41.5*3
+
+ # calculated from official spec: maximum angle (i.e., over the diagonals)
+ # is 92 deg. -> angle over half a diagonal's length is 92/2 deg
+ constant2= 1./(math.sqrt((1280/2)**2+(720/2)**2))*(92/2)/180*math.pi
+
+ for y in xrange(0,yres):
+ xtmp=[]
+ ytmp=[]
+
+ yy=(y-yres/2.)/xres # yes, xres.
+ for x in xrange(0,xres):
+ xx_=(x-xres/2.)/xres
+ xx=math.tan(xx_)
+
+ dist = math.sqrt(xx**2 + yy**2)
+
+ if (dist != 0):
+ xtmp=xtmp+[ 1280/2+ xx/dist/constant2*math.atan(constant*dist) ]
+ ytmp=ytmp+[ 720/2 + yy/dist/constant2*math.atan(constant*dist) ]
+ else:
+ xtmp=xtmp+[0+1280/2]
+ ytmp=ytmp+[0+720/2]
+
+ xlist=xlist+[xtmp]
+ ylist=ylist+[ytmp]
+
+ if y % 10 == 0:
+ print y
-for y in xrange(0,yres):
- xtmp=[]
- ytmp=[]
+ xmap=numpy.array(xlist).astype('float32')
+ ymap=numpy.array(ylist).astype('float32')
- yy=(y-yres/2.)/xres # yes, xres.
- for x in xrange(0,xres):
- xx=(x-xres/2.)/xres
+ return xmap,ymap
- dist = math.sqrt(xx**2 + yy**2)
-
- if (dist != 0):
- xtmp=xtmp+[ 1280/2+ xx/dist/constant2*math.atan(constant*dist) ]
- ytmp=ytmp+[ 720/2 + yy/dist/constant2*math.atan(constant*dist) ]
- else:
- xtmp=xtmp+[0+1280/2]
- ytmp=ytmp+[0+720/2]
-
- xlist=xlist+[xtmp]
- ylist=ylist+[ytmp]
-
- if y % 10 == 0:
- print y
-
-xmap=numpy.array(xlist).astype('float32')
-ymap=numpy.array(ylist).astype('float32')
+xmap,ymap = get_maps(1600/2,900/2)
img=cv2.imread("cam42.png")
img2=cv2.remap(img, xmap, ymap, cv2.INTER_CUBIC)