Ejemplo n.º 1
0
	def gray_frame(self, backsub = True):
		if backsub:
			(mask, ctrs, fd) = findObjectThreshold(self.frame, threshold = self.threshold)
			frame = np.multiply(mask, self.grayframe)
		else:
			frame = self.grayframe 
		return frame 
Ejemplo n.º 2
0
	def gray_frame(self, backsub = True):
		if backsub:
			(mask, ctrs, fd) = findObjectThreshold(self.frame, threshold = self.threshold)
			frame = np.multiply(mask, self.grayframe)
		else:
			frame = self.grayframe 
		return frame 
Ejemplo n.º 3
0
	def current_frame(self, backsub = True):
		if backsub:
			(mask, ctrs, fd) = findObjectThreshold(self.frame, threshold = self.threshold)
			frame = np.multiply(np.dstack((mask, mask, mask)), self.frame)
		else:
			frame = self.frame 
		return frame 
Ejemplo n.º 4
0
	def current_frame(self, backsub = True):
		if backsub:
			(mask, ctrs, fd) = findObjectThreshold(self.frame, threshold = self.threshold)
			frame = np.multiply(np.dstack((mask, mask, mask)), self.frame)
		else:
			frame = self.frame 
		return frame 
Ejemplo n.º 5
0
	def backsub(self, im = None):
		(mask, ctrs, fd) = findObjectThreshold(self.frame, threshold = self.threshold)
		if im is None:
			return mask, ctrs, fd
		else:
			if len(im.shape) == 2:
				return np.multiply(mask, im)
			#Probably a better way to do this...
			elif im.shape[2] == 2:
				return np.multiply(np.dstack((mask, mask)), im)
			elif im.shape[2] == 3:
				return np.multiply(np.dstack((mask, mask, mask)), im)
Ejemplo n.º 6
0
	def backsub(self, im = None):
		(mask, ctrs, fd) = findObjectThreshold(self.frame, threshold = self.threshold)
		if im is None:
			return mask, ctrs, fd
		else:
			if len(im.shape) == 2:
				return np.multiply(mask, im)
			#Probably a better way to do this...
			elif im.shape[2] == 2:
				return np.multiply(np.dstack((mask, mask)), im)
			elif im.shape[2] == 3:
				return np.multiply(np.dstack((mask, mask, mask)), im)
Ejemplo n.º 7
0
	def read(self, backsub = True):
		mask = None 
		try:
			ret = True
			self.frame = self.gen.next()
		except:
			ret = False
			self.frame = None
			return ret, self.frame, mask

		if not backsub:
			return ret, self.frame, mask
		else:
			(mask, ctrs, fd) = findObjectThreshold(self.frame, threshold = self.threshold)
			#Apply mask
			backframe = np.multiply(mask, self.frame)
			return ret, backframe, mask
Ejemplo n.º 8
0
	def projectmask(self, y_m):
		print '-- projecting outliers onto contour'
		#Project vertices onto boundary 
		ddeps = 1e-1
		(mask2, ctrs, fd) = findObjectThreshold(y_m, threshold = 0.5)
		p = self.state.vertices()
		p_orig = p.copy()
		d = fd(p)
		ix = d>1
		for idx in range(10):
			if ix.any():
				dgradx = (fd(p[ix]+[ddeps,0])-d[ix])/ddeps # Numerical
				dgrady = (fd(p[ix]+[0,ddeps])-d[ix])/ddeps # gradient
				dgrad2 = dgradx**2 + dgrady**2
				p[ix] -= (d[ix]*np.vstack((dgradx, dgrady))/dgrad2).T # Project
		self.state.X[0:(2*self.N)] = np.reshape(p, (-1,1))

		#Update velocities also... or else it might crash...
		self.state.X[(2*self.N):] += np.reshape(p - p_orig, (-1,1))
Ejemplo n.º 9
0
	def read(self, backsub = True):
		try:
			ret, frame = self.cap.read()
			self.frame = frame 
			self.grayframe = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
		except:
			ret = False
			frame = None
			grayframe = None 
			mask = None 
			return ret, frame, grayframe, mask

		if not backsub:
			return ret, self.frame, self.grayframe
		else:
			(mask, ctrs, fd) = findObjectThreshold(self.frame, threshold = self.threshold)
			#Apply mask
			backframe = np.multiply(np.dstack((mask, mask, mask)), self.frame)
			backgrayframe = np.multiply(mask, self.grayframe)		
			return ret, backframe, backgrayframe, mask
Ejemplo n.º 10
0
	def read(self, backsub = True):
		try:
			ret, frame = self.cap.read()
			self.frame = frame 
			self.grayframe = cv2.cvtColor(self.frame, cv2.COLOR_BGR2GRAY)
		except:
			ret = False
			frame = None
			grayframe = None 
			mask = None 
			return ret, frame, grayframe, mask

		if not backsub:
			return ret, self.frame, self.grayframe
		else:
			(mask, ctrs, fd) = findObjectThreshold(self.frame, threshold = self.threshold)
			#Apply mask
			backframe = np.multiply(np.dstack((mask, mask, mask)), self.frame)
			backgrayframe = np.multiply(mask, self.grayframe)		
			return ret, backframe, backgrayframe, mask
Ejemplo n.º 11
0
cuda = True
gridsize = 30
threshold = 9
name = 'test_data'

#Grab test data
video, flow = test_data(680, 680)
#video, flow = test_data_texture(680, 680)
#video, flow = test_data_image()
flowframe = flow[:,:,:,0]
frame = video[:,:,0]

#Make mesh
distmesh = DistMesh(frame, h0 = gridsize)
mask, ctrs, h = findObjectThreshold(frame, threshold = threshold)
distmesh.createMesh(ctrs, h, frame, plot=False)

nx = 680
start = nx//3
end = 2*nx//3

kf = IteratedMSKalmanFilter(distmesh, frame, flowframe, cuda)

#Now perturb the positions a bit...
kf.state.X = kf.state.X*1.2
kf.state.refresh()

rend = kf.state.renderer
cuda = kf.state.renderer.cudagl 
kf.state.render()
Ejemplo n.º 12
0
nx = capture.nx
nF = capture.nframes

vid = np.zeros((nx, nx, nF))
masks = np.zeros((nx, nx, nF))
for idx in range(nF):
	print 'Loading frame', idx
	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)
Ejemplo n.º 13
0
    def projectmask(self, y_m):
        print '-- projecting outliers onto contour'
        logging.info("--KF projection")
        #Project vertices onto boundary
        ddeps = 1e-1
        (mask2, ctrs, fd) = findObjectThreshold(y_m, threshold=0.5)
        p = self.state.vertices()
        p_orig = p.copy()

        #Find vertices outside of mask and project these onto contour
        for idx in range(10):
            d = fd(p)
            ix = d > 1
            if ix.any():
                #First order differencing
                #dgradx = (fd(p[ix]+[ddeps,0])-d[ix])/(ddeps) # Numerical
                #dgrady = (fd(p[ix]+[0,ddeps])-d[ix])/(ddeps) # gradient
                #Central differencing
                dgradx = (fd(p[ix] + [ddeps, 0]) - fd(p[ix] - [ddeps, 0])) / (
                    2 * ddeps)  # Numerical
                dgrady = (fd(p[ix] + [0, ddeps]) - fd(p[ix] - [0, ddeps])) / (
                    2 * ddeps)  # gradient

                dgrad2 = dgradx**2 + dgrady**2
                p[ix] -= (d[ix] * np.vstack(
                    (dgradx, dgrady)) / dgrad2).T  # Project

        #Find _outer_ vertices inside of mask and project these onto contour
        #Get rendered mask

        rend_mask = self.state.renderer.rendermask()[:, :, 2]
        #rend_mask = np.flipud(rend_mask)

        #For each vertex, find if its on the border of the mask...
        border = np.zeros(self.N, dtype=bool)
        for idx, pi in enumerate(p):
            pii = pi.astype(int)
            i = np.zeros(8, dtype=bool)
            i[0] = rend_mask[pii[0, 1] + 2, pii[0, 0] + 2]
            i[1] = rend_mask[pii[0, 1] + 2, pii[0, 0] - 2]
            i[2] = rend_mask[pii[0, 1] - 2, pii[0, 0] + 2]
            i[3] = rend_mask[pii[0, 1] - 2, pii[0, 0] - 2]
            i[4] = rend_mask[pii[0, 1], pii[0, 0] + 2]
            i[5] = rend_mask[pii[0, 1], pii[0, 0] - 2]
            i[6] = rend_mask[pii[0, 1] + 2, pii[0, 0]]
            i[7] = rend_mask[pii[0, 1] - 2, pii[0, 0]]
            if (np.sum(i) < 8) and (np.sum(i) > 0):
                border[idx] = 1

        #Plot the mask...
        #prend_mask = rend_mask.copy()
        #for pi in p:
        #	pii = pi.astype(int)
        #	prend_mask[pii[0,1],   pii[0,0]] = 300
        #	prend_mask[pii[0,1]+1, pii[0,0]] = 300
        #	prend_mask[pii[0,1],   pii[0,0]+1] = 300
        #	prend_mask[pii[0,1]+1, pii[0,0]+1] = 300
        #plt.imshow(prend_mask)
        #plt.colorbar()
        #plt.show()

        d = fd(p)
        ix = (d < -1) * border
        for idx in range(1):
            if ix.any():
                #First order differencing
                #dgradx = (fd(p[ix]+[ddeps,0])-d[ix])/(ddeps) # Numerical
                #dgrady = (fd(p[ix]+[0,ddeps])-d[ix])/(ddeps) # gradient
                #Central differencing
                dgradx = (fd(p[ix] + [ddeps, 0]) - fd(p[ix] - [ddeps, 0])) / (
                    2 * ddeps)  # Numerical
                dgrady = (fd(p[ix] + [0, ddeps]) - fd(p[ix] - [0, ddeps])) / (
                    2 * ddeps)  # gradient
                dgrad2 = dgradx**2 + dgrady**2
                p[ix] -= (d[ix] * np.vstack(
                    (dgradx, dgrady)) / dgrad2).T  # Project

        #Write changes
        self.state.X[0:(2 * self.N)] = np.reshape(p, (-1, 1))

        #Update velocities also... or else it might crash...
        self.state.X[(2 * self.N):] += np.reshape(p - p_orig, (-1, 1))
Ejemplo n.º 14
0
	def backsub(self):
		(mask, ctrs, fd) = findObjectThreshold(self.img, threshold = self.threshold)
		return mask, ctrs, fd