def main(): fourcc = cv2.VideoWriter_fourcc(*'XVID') # outGrid = cv2.VideoWriter('sep/0_0.grid.avi', fourcc, 20.0, (1280, 720)) outFull = cv2.VideoWriter('sep/full.avi', fourcc, 20.0, (1280, 720)) cap = cv2.VideoCapture('sep/0_0.avi') # outGrid = cv2.VideoWriter('sep/25_68351.grid.avi',fourcc, 20.0, (1280,720)) # outFull = cv2.VideoWriter('sep/25_68351.full.avi',fourcc, 20.0, (1280,720)) # cap = cv2.VideoCapture('sep/25_68351.avi') fn = 0 ret, iframe = cap.read() H, W, _ = iframe.shape M = np.dot(initM(), np.linalg.inv(visM())) M = np.array( [[4., 0, -2000.], [0, 4., - 1200.], [0, 0, 1]]) #M = np.eye(3) bg = np.zeros_like(iframe) prev = cv2.warpPerspective(iframe, M, (W, H), flags=cv2.WARP_INVERSE_MAP, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0)) # prev = cv2.Canny(iframe, 100, 255, apertureSize=3) # prev[565:650, 240:950] = 0 # cv2.circle(prev, (1042, 620), 29, 0, -1) # params = np.array([ [1.1, .9, 1.2], [.9, 1.1, 1.2], [.9, .9, 1] ]) blank = np.zeros((H, W)).astype(np.uint8) bgMask = cv2.warpPerspective(blank, M, (W, H), flags=cv2.WARP_INVERSE_MAP, borderMode=cv2.BORDER_CONSTANT, borderValue=255) bgMask = cv2.bitwise_not(bgMask) while(cap.isOpened()): ret, frame = cap.read() if not ret: break fn += 1 if fn % 30 != 0: continue img, dm = siftFeature(prev, frame, bgMask) if dm is None: continue M = np.dot(dm, M) M = M / M[2, 2] print M proj = cv2.warpPerspective(frame, dm, (W, H), flags=cv2.WARP_INVERSE_MAP, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0)) mask = cv2.warpPerspective(blank, dm, (W, H), flags=cv2.WARP_INVERSE_MAP, borderMode=cv2.BORDER_CONSTANT, borderValue=255) fmask = cv2.bitwise_not(mask) bgMask = cv2.bitwise_or(bgMask, fmask) if fn == 1: bg = proj.copy() else: T = cv2.bitwise_or(bg, proj, mask=mask) bg = cv2.bitwise_and(proj, proj, mask=fmask) bg = cv2.bitwise_or(bg, T) prev = bg while False: cv2.imshow('img', img) cv2.imshow('proj', proj) cv2.imshow('mask', bgMask) key = cv2.waitKey(5) & 0xFF if key == ord('q'): return if key == ord('a'): break if key == ord('c'): break cv2.imshow('proj', proj) cv2.imshow('mask', bgMask) cv2.imshow('bg', bg) outFull.write(bg) key = cv2.waitKey(5) & 0xFF if key == ord('q'): return
def main(): fourcc = cv2.VideoWriter_fourcc(*'XVID') #outGrid = cv2.VideoWriter('sep/0_0.grid.avi', fourcc, 20.0, (1280, 720)) #outFull = cv2.VideoWriter('sep/0_0.full.avi', fourcc, 20.0, (1280, 720)) #cap = cv2.VideoCapture('sep/0_0.avi') #outGrid = cv2.VideoWriter('sep/25_68351.grid.avi',fourcc, 20.0, (1280,720)) #outFull = cv2.VideoWriter('sep/25_68351.full.avi',fourcc, 20.0, (1280,720)) cap = cv2.VideoCapture('sep/25_68351.avi') fn = 0 ret, iframe = cap.read() H, W, _ = iframe.shape tpl = template() M = initM() visTpl = cv2.warpPerspective(tpl, visM(), (1280, 720)) cRot = cv2.warpPerspective(tpl, M, (1280, 720)) fullCourt = [] fullImg = np.zeros_like(iframe) m = np.eye(3) tic = time.clock() MS = [M] Theta = 10 #params = np.array([ [1.1, .9, 1.2], [.9, 1.1, 1.2], [.9, .9, 1] ]) while(cap.isOpened()): ret, frame = cap.read() if not ret: break fn += 1 # if fn%2 == 0: continue # if fn % 6 == 0: continue frameHSV = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) threshColor = cv2.inRange( frameHSV, np.array([0, 47, 151]), np.array([16, 255, 255])) threshColor = cv2.morphologyEx( threshColor, cv2.MORPH_OPEN, np.ones((3, 3), np.uint8)) edges = cv2.Canny(threshColor, 200, 255, apertureSize=3) edges[565:650, 240:950] = 0 #frame[565:650, 240:950] = 0 cv2.circle(edges, (1042, 620), 29, 0, -1) dstIdx = cv2.findNonZero(edges).reshape(-1, 2) if len(dstIdx) < 5000: MS.append(MS[-1]) continue nbrs = NearestNeighbors( n_neighbors=1, radius=1.0, algorithm='auto').fit(dstIdx) cnt = Theta converge = 100 while cnt: img = frame.copy() cnt -= 1 #blank = np.zeros_like(frame) cv2.warpPerspective(tpl, np.dot(m, M), (W, H), dst=cRot) # if len(nKeys) < 8000: break if fn == 1 : oKeys, nKeys = neighbors(cRot, dstIdx, nbrs, d=10) dm, ret = cv2.findHomography(oKeys, nKeys, method=cv2.LMEDS) else: oKeys, nKeys = neighbors(cRot, dstIdx, nbrs, d=10) dm = estimate(oKeys, nKeys) #dm = ransac(oKeys, nKeys) print dm #import ipdb; # ipdb.set_trace() if dm is None: dm = np.eye(3) #converge = np.linalg.norm(dm - np.eye(3)) # if converge < 0.45: # break #dm = 1.2 * (dm - np.eye(3)) + np.eye(3) m = np.dot(dm, m) m = m / m[2, 2] img[...,1] = cv2.bitwise_or(cRot, img[...,1]) for o, n in zip(oKeys, nKeys): cv2.line(img, (o[0], o[1]), (n[0], n[1]), (255,255,255), 1) while False: cv2.imshow('edges', edges) key = cv2.waitKey(5) & 0xFF if key == ord('q'): return if key == ord('a'): break if key == ord('c'): cnt = False break M = np.dot(m, M) M = M / M[2, 2] MS.append(M) alpha = np.sqrt(m[0, 2] * m[2, 0]) gamma = np.sqrt(-m[1, 2] * m[2, 1]) f = - m[1, 2] / gamma r = m[0, 2] / (alpha * f) # print converge # print 50 - cnt if fn > 2: m = .6 * (m - np.eye(3)) + np.eye(3) else: m = np.eye(3) img[..., 1] = cv2.bitwise_or(cRot, img[..., 1]) inv = cv2.warpPerspective(frame, np.dot(M, np.linalg.inv(visM())), (W, H), flags=cv2.WARP_INVERSE_MAP, borderMode=cv2.BORDER_CONSTANT, borderValue=(0, 0, 0)) fmask = cv2.warpPerspective(np.zeros_like(cRot), np.dot(M, np.linalg.inv(visM())), (W, H), flags=cv2.WARP_INVERSE_MAP, borderMode=cv2.BORDER_CONSTANT, borderValue=255) #inv[...,1] = cv2.bitwise_or(visTpl, inv[...,1]) fullT1 = cv2.bitwise_and(fullImg, fullImg, mask=fmask) fullImg = cv2.addWeighted(fullImg, 0.99, inv, 0.01, 0.45) #fullImg = inv.copy() fmaskI = cv2.bitwise_not(fmask) fullImg = cv2.bitwise_or(fullImg, fullT1) visImg = cv2.bitwise_and(inv, inv, mask=fmaskI) bg = cv2.bitwise_and(fullImg, fullImg, mask=fmask) visImg = cv2.add(visImg, bg) visImg[..., 1] = cv2.bitwise_or(visTpl, visImg[..., 1]) toc = time.clock() # sys.stdout.write("\rI[%s] #%s %.4f %.4f sec/frame\n" % # (Theta - cnt, fn, converge, (toc - tic) / fn)) # sys.stdout.write("\r%.4f %.4f %.4f %.4f" % (alpha, gamma, f, r)) # sys.stdout.flush() cv2.putText(img, "[%d]#f %d %.2f %.2f sec/frame" % (Theta - cnt, fn, converge, (toc - tic) / fn), (10, 30), FONT, 1, (255, 255, 255), 1, cv2.LINE_AA) cv2.imshow('frame', img) cv2.putText(visImg, "[%d]#f %d %.2f %.2f sec/frame" % (Theta - cnt, fn, converge, (toc - tic) / fn), (10, 600), FONT, 1, (255, 255, 255), 1, cv2.LINE_AA) cv2.imshow('inv', visImg) key = cv2.waitKey(1) & 0xFF if key == ord('q'): return # outGrid.write(img) # outFull.write(visImg) MS = np.array(MS)