#Output img_out = './synthetictests/' + name + '/' + ff + '_' + notes + '_pred/' if not os.path.isdir(img_out): os.makedirs(img_out) gridsize = 18 threshold = 8 #Create KF print 'Loading synthetic data streams' capture = VideoStream(v_in, threshold) frame = capture.current_frame() mask, ctrs, fd = capture.backsub() distmesh = DistMesh(frame, h0 = gridsize) distmesh.load(dm_in) #Load true data f_mesh = open(m_in, 'r') lines = f_mesh.readlines() nF = len(lines)-1 nX = int(lines[0].split(',')[1]) truestates = np.zeros((nF, nX*4), dtype = np.float32) predstates = np.zeros((nF, nX*4), dtype = np.float32) for i in range(1,nF+1): line = lines[i] truestates[i-1,:] = [float(x) for x in line.split(',')[1:]] predstates[0,:] = truestates[0,:] rms_vel = np.zeros(nF)
refframe = frame vid[:, :, idx] = grayframe masks[:, :, idx] = mask tracking_mask = cv2.imread(mask_in) tracking_mask = cv2.cvtColor(tracking_mask, cv2.COLOR_BGR2GRAY) (mask, ctrs, fd) = findObjectThreshold(tracking_mask, threshold=threshold) #distmesh = DistMesh(refframe, h0 = gridsize) #distmesh.createMesh(ctrs, fd, refframe, plot = True) #Save this distmesh and reload it for quicker testing #distmesh.save(mfsf_in + dm_out) distmesh = DistMesh(refframe, h0=gridsize) distmesh.load(mfsf_in + dm_out) refpositions = distmesh.p #Create dummy input for flow frame flowframe = np.zeros((nx, nx, 2)) #Create Kalman Filter object to store mesh and make use of plotting functions kf = IteratedMSKalmanFilter(distmesh, refframe, flowframe, cuda=cuda) #Perturb mesh points according to MFSF flow field and save screenshot output nF = u.shape[2] N = kf.size() / 4 #Load estimated neuron tracks truepositions = np.zeros((nF, nC, 2))
ret, frame, mask = capture.read() if idx == nref: refframe = frame.copy() vid[:,:,idx] = frame masks[:,:,idx] = mask #Generate ctrs and fd (mask, ctrs, fd) = findObjectThreshold(tracking_mask, threshold = threshold) #distmesh = DistMesh(refframe, h0 = gridsize) #distmesh.createMesh(ctrs, fd, refframe, plot = True) #Save this distmesh and reload it for quicker testing #distmesh.save(dm_out) distmesh = DistMesh(refframe, h0 = gridsize) distmesh.load(dm_out) refpositions = distmesh.p #Create dummy input for flow frame flowframe = np.zeros((nx, nx, 2)) #Create Kalman Filter object to store mesh and make use of plotting functions kf = IteratedMSKalmanFilter(distmesh, refframe, flowframe, cuda = cuda) N = kf.size()/4 totalframes = 0 for vidx in range(nV): #Load MFSF data a = loadmat(mfsf_matfiles[vidx]) params = a['parmsOF']
def runIEKF(eps_Z, eps_J, eps_M, eps_F): print 'Running with:' print '- eps_Z:', eps_Z print '- eps_J:', eps_J print '- eps_M:', eps_M print '- eps_F:', eps_F if mask_flow: notes = 'masked_iekf' else: notes = 'iekf' notes += '_eps_Z_%f'%eps_Z notes += '_eps_J_%f'%eps_J notes += '_eps_M_%f'%eps_M notes += '_eps_F_%f'%eps_F img_out = './synthetictests/' + name + '/' + ff + '_' + notes + '_pred/' if not os.path.isdir(img_out): os.makedirs(img_out) print 'Loading synthetic data streams' capture = VideoStream(v_in, threshold) frame = capture.current_frame() mask, ctrs, fd = capture.backsub() distmesh = DistMesh(frame, h0 = gridsize) distmesh.load(dm_in) #Load true data f_mesh = open(m_in, 'r') lines = f_mesh.readlines() nF = len(lines)-1 nX = int(lines[0].split(',')[1]) truestates = np.zeros((nF, nX*4), dtype = np.float32) predstates = np.zeros((nF, nX*4), dtype = np.float32) for i in range(1,nF+1): line = lines[i] truestates[i-1,:] = [float(x) for x in line.split(',')[1:]] predstates[0,:] = truestates[0,:] rms_vel = np.zeros(nF) rms_pos = np.zeros(nF) flowstream = FlowStream(flow_in) ret_flow, flowframe = flowstream.read() kf = IteratedKalmanFilter(distmesh, frame, flowframe, cuda = cuda, sparse = sparse,\ eps_F = eps_F, eps_Z = eps_Z, eps_J = eps_J, eps_M = eps_M, nI = nI) count = 0 print 'Tracking with Kalman filter' while(capture.isOpened() and count < max_frames): #for idx in range(1): count += 1 ret, frame, grayframe, mask = capture.read() ret_flow, flowframe = flowstream.read() if ret is False or ret_flow is False: break print 'Frame %d' % count kf.compute(grayframe, flowframe, mask, maskflow = mask_flow, imageoutput = img_out+'solution_frame_%03d'%count) #kf.compute(grayframe, flowframe, mask, maskflow = mask_flow) predstates[count,:] = np.squeeze(kf.state.X) r_pos = truestates[count,0:(2*nX)]-predstates[count,0:(2*nX)] r_vel = truestates[count,(2*nX):]-predstates[count,(2*nX):] rms_pos[count] = np.sqrt(np.mean(np.multiply(r_pos, r_pos))) rms_vel[count] = np.sqrt(np.mean(np.multiply(r_vel, r_vel))) print 'RMS_pos:', rms_pos[count], 'RMS_vel:', rms_vel[count] print 'Saving' np.savez('./synthetictests/' + name + '/' + ff + '_' + notes + '_pred.npz', predstates, truestates, rms_pos, rms_vel) print 'Done... how\'d we do?' #Make plot of tracking error plt.plot(range(nF), rms_pos, label='RMS position') plt.plot(range(nF), rms_vel, label='RMS velocity') plt.legend(loc='upper left') plt.ylabel('RMS') plt.xlabel('Frame') plt.savefig('./synthetictests/' + name + '/' + ff + '_' + notes + '_pred_rms.eps')