diff options
author | Florian Jung <flo@windfisch.org> | 2014-08-25 21:50:09 +0200 |
---|---|---|
committer | Florian Jung <flo@windfisch.org> | 2014-08-25 21:50:09 +0200 |
commit | bb02fa2fa3ca4abc1f508c0d33024957c06991c7 (patch) | |
tree | 9920be9e504b809f2a3ea2595b256a21ef8e0e46 | |
parent | f25f794ce71695782aac15b72fc491a33965351c (diff) |
cleanup, doku, dinge
-rw-r--r-- | test.py | 51 |
1 files changed, 17 insertions, 34 deletions
@@ -4,7 +4,9 @@ import numpy from math import sin,cos cap = cv2.VideoCapture("../vid.mp4") -writer = cv2.VideoWriter("../outvid.avi",cv2.cv.CV_FOURCC(*'MP42'),25,(1600,900),1) +writer = cv2.VideoWriter("../outvid.avi",cv2.cv.CV_FOURCC(*'MP42'),25,(1600,600),1) + +###### CONFIG ###### feature_params = dict( maxCorners = 100, qualityLevel = 0.3, @@ -22,6 +24,10 @@ lk_params = dict( winSize = (15,15), criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) + + +###### INITALISATION ###### + ret,oldframe=cap.read() oldgray=cv2.cvtColor(oldframe,cv2.COLOR_BGR2GRAY) @@ -36,32 +42,10 @@ total_y=-300 while(cap.isOpened()): ret, frame = cap.read() - gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY) - p0 = cv2.goodFeaturesToTrack(oldgray, mask = None, **feature_params) - - # calculate optical flow - p1, st, err = cv2.calcOpticalFlowPyrLK(oldgray, gray, p0, None, **lk_params) - - # Select good points - good_new = p1[st==1] - good_old = p0[st==1] - - for i in p0: - x,y=i.ravel() - cv2.line(frame, (x,y),(x,y), (0,0,255),5) - - one=[] - two=[] - for i,(new,old) in enumerate(zip(good_new, good_old)): - x1,y1=new.ravel() - x2,y2=old.ravel() - cv2.line(frame, (x1,y1),(x2,y2), (0,255,0),5) - one=one+[(x1,y1)] - two=two+[(x2,y2)] - + # calculate movement, rotation and stretch. (we will ignore the stretch factor.) mat = cv2.estimateRigidTransform(gray,oldgray,False) angle = math.atan2(mat[0,1],mat[0,0]) stretch = int((math.sqrt(mat[0,1]**2+mat[0,0]**2)-1)*100) @@ -70,29 +54,28 @@ while(cap.isOpened()): shift_x = mat[0,2] - width/2 + ( mat[0,0]*width/2 + mat[0,1]*height/2 ) shift_y = mat[1,2] - height/2 + ( mat[1,0]*width/2 + mat[1,1]*height/2 ) + + # accumulate values total_x = total_x + shift_x total_y = total_y + shift_y total_angle=total_angle+angle print angle/3.141592654*180,'deg\t',stretch,"%\t", shift_x,'\t',shift_y + # rotate and move current frame into a global context mat2=cv2.getRotationMatrix2D((width/2,height/2), total_angle/3.141593654*180, scale_factor) mat2[0,2] = mat2[0,2]+total_x*scale_factor mat2[1,2] = mat2[1,2]+total_y*scale_factor - print mat2.__repr__() -# mat2=numpy.array([[cos(total_angle), sin(total_angle), total_x], [-sin(total_angle),cos(total_angle),total_y]]) frame2= cv2.warpAffine(frame, mat2, (scr_width,scr_height) ) mask2 = cv2.warpAffine(mask, mat2, (scr_width,scr_height) ) ret, mask2 = cv2.threshold(mask2, 254, 255, cv2.THRESH_BINARY) - mask3 = cv2.erode(mask2, numpy.ones((2,2),numpy.uint8)) # strip off the potentially-badlooking edges - mask4 = cv2.dilate(mask2, numpy.ones((5,5),numpy.uint8)) - #screencontent = frame2 - screencontent = cv2.bitwise_and(screencontent,screencontent, mask=cv2.bitwise_not(mask3)) - screencontent = cv2.add(screencontent, cv2.bitwise_and(frame2,frame2,mask=mask3)) - #screencontent = frame2 - #screencontent = mask2 + mask3 = cv2.erode(mask2, numpy.ones((20,200),numpy.uint8)) # strip off the potentially-badlooking edges. left/right borders are darkened, strip them off + mask4 = cv2.erode(mask2, numpy.ones((1,1),numpy.uint8)) + + screencontent = cv2.bitwise_and(screencontent,screencontent, mask=cv2.bitwise_not(mask3)) # blank out + screencontent = cv2.add(screencontent, cv2.bitwise_and(frame2,frame2,mask=mask3)) # and redraw screencontent2=screencontent.copy() screencontent2=cv2.bitwise_and(screencontent2,screencontent2, mask=cv2.bitwise_not(mask4)) @@ -107,5 +90,5 @@ while(cap.isOpened()): oldframe=frame oldgray=gray - if cv2.waitKey(20) & 0xFF == ord("q"): + if cv2.waitKey(1) & 0xFF == ord("q"): break |