Пример #1
0
	def generateData(self):
		print self.frame
		inpt = self.getInput(0).getData()
		
		# if we are on the first frame
		if self.frame == 0:
			self.prevInpt = inpt
			
			hc = self.getInput(1).getData()
			# Create the features vector for t_0 storing (x,y,confidence, active)
			fts = numpy.ones((hc.shape[0],4), dtype=numpy.float32)
			fts[:,:2]=hc[:,:2] #Copy the x-y positions of features detected by hc to the features vector
			self.prevFeatures = fts # store it as the first entry in the features array which stores features over time
			self.getOutput(0).setData(fts)
			
			
			r = numpy.floor(self.sigmaD * 5.0/2)
			self.patches = numpy.zeros((hc.shape[0], (2*r+1)**2), dtype=numpy.float32)
			for i, (x,y, _, _) in enumerate(fts):
			    self.patches[i,...] = inpt[y-r:y+r+1, x-r:x+r+1].flatten()
		else:
			newFeatures = numpy.copy(self.prevFeatures)
			Ix  = self.getInput(2).getData()
			Iy  = self.getInput(3).getData()
			Ixx = self.getInput(4).getData()
			Iyy = self.getInput(5).getData()
			Ixy = self.getInput(6).getData()
			
			h, w = inpt.shape
			velocity = numpy.array([0.0,0.0]) # intial velocity values
			
			for i, (x,y, s, a) in enumerate(newFeatures):
				iterations = self.iterations
				
				# throw away inactive points
				if a==0:
					continue
					
				# CALCULATE ATA
				pIxx = Ixx[y,x]
				pIyy = Iyy[y,x]
				pIxy = Ixy[y,x]
				ATA = numpy.array([[pIxx, pIxy],[pIxy, pIyy]])                
				
				#change in velocity
				dv = numpy.array([100.0,100.0])
				
				g = imgutil.gaussian(self.sigmaD)[0]
				g = g[:,None]
				gg = numpy.dot(g, g.transpose()).flatten()
				r = g.size/2
				iyy, ixx = numpy.mgrid[-r:r+1, -r:r+1]
				ryy, rxx = y+iyy, x+ixx
				
				patchIx = interpolation.map_coordinates(Ix, numpy.array([ryy.flatten(), rxx.flatten()]))
				patchIy = interpolation.map_coordinates(Iy, numpy.array([ryy.flatten(), rxx.flatten()]))

				# ITERATE AND CALCULATE ATb
				while iterations > 0 and numpy.dot(dv, dv)>self.epsilon: 
					patch1 = interpolation.map_coordinates(inpt, numpy.array([ryy.flatten(), rxx.flatten()]))
					patch0 = interpolation.map_coordinates(self.prevInpt, numpy.array([(ryy-velocity[1]).flatten(), (rxx-velocity[0]).flatten()])) 

					#imgutil.imageShow(patch0.reshape((g.size,g.size)), "p0")
					#imgutil.imageShow(patch1.reshape((g.size,g.size)),"p1")
		
					patchIt = patch1-patch0
					pIxt = (patchIt*patchIx*gg).sum()
					pIyt = (patchIt*patchIy*gg).sum()
					
					# solve ATAv = -ATb
					ATb = -numpy.array([pIxt, pIyt])
					dv = numpy.linalg.lstsq(ATA, ATb)[0]
					# update velocity and iterations
					velocity += dv
					iterations -= 1
				
				#print "stopped after", (self.iterations-iterations), "with norm", numpy.dot(dv,dv)**.5
				
				# calculate new feature positions
				newFeatures[i][:2]+= velocity
				
				#Compute the similarity between our new feature location and the original feature location
				
				#imgutil.imageShow(patch1.reshape(g.size,g.size), "p0")
				#imgutil.imageShow(self.patches[i], "p1")
				newFeatures[i][2] = imgutil.ncc(patch1, self.patches[i,...])
				
				# set feature status (active or inactive)
				if newFeatures[i][0] > w or newFeatures[i][1] > h or newFeatures[i][0] < 0 or newFeatures[i][1] < 0:
					newFeatures[i][3] = 0
			
			self.prevFeatures = newFeatures
			self.prevInpt = inpt
			self.getOutput(0).setData(newFeatures)
			
		self.frame += 1
Пример #2
0
    def generateData(self):
        print self.frame
        inpt = self.getInput(0).getData()

        # if we are on the first frame
        if self.frame == 0:
            self.prevInpt = inpt

            hc = self.getInput(1).getData()
            # Create the features vector for t_0 storing (x,y,confidence, active)
            fts = numpy.ones((hc.shape[0], 4), dtype=numpy.float32)
            fts[:, :
                2] = hc[:, :
                        2]  #Copy the x-y positions of features detected by hc to the features vector
            self.prevFeatures = fts  # store it as the first entry in the features array which stores features over time
            self.getOutput(0).setData(fts)

            r = numpy.floor(self.sigmaD * 5.0 / 2)
            self.patches = numpy.zeros((hc.shape[0], (2 * r + 1)**2),
                                       dtype=numpy.float32)
            for i, (x, y, _, _) in enumerate(fts):
                self.patches[i, ...] = inpt[y - r:y + r + 1,
                                            x - r:x + r + 1].flatten()
        else:
            newFeatures = numpy.copy(self.prevFeatures)
            Ix = self.getInput(2).getData()
            Iy = self.getInput(3).getData()
            Ixx = self.getInput(4).getData()
            Iyy = self.getInput(5).getData()
            Ixy = self.getInput(6).getData()

            h, w = inpt.shape
            velocity = numpy.array([0.0, 0.0])  # intial velocity values

            for i, (x, y, s, a) in enumerate(newFeatures):
                iterations = self.iterations

                # throw away inactive points
                if a == 0:
                    continue

                # CALCULATE ATA
                pIxx = Ixx[y, x]
                pIyy = Iyy[y, x]
                pIxy = Ixy[y, x]
                ATA = numpy.array([[pIxx, pIxy], [pIxy, pIyy]])

                #change in velocity
                dv = numpy.array([100.0, 100.0])

                g = imgutil.gaussian(self.sigmaD)[0]
                g = g[:, None]
                gg = numpy.dot(g, g.transpose()).flatten()
                r = g.size / 2
                iyy, ixx = numpy.mgrid[-r:r + 1, -r:r + 1]
                ryy, rxx = y + iyy, x + ixx

                patchIx = interpolation.map_coordinates(
                    Ix, numpy.array([ryy.flatten(),
                                     rxx.flatten()]))
                patchIy = interpolation.map_coordinates(
                    Iy, numpy.array([ryy.flatten(),
                                     rxx.flatten()]))

                # ITERATE AND CALCULATE ATb
                while iterations > 0 and numpy.dot(dv, dv) > self.epsilon:
                    patch1 = interpolation.map_coordinates(
                        inpt, numpy.array([ryy.flatten(),
                                           rxx.flatten()]))
                    patch0 = interpolation.map_coordinates(
                        self.prevInpt,
                        numpy.array([(ryy - velocity[1]).flatten(),
                                     (rxx - velocity[0]).flatten()]))

                    #imgutil.imageShow(patch0.reshape((g.size,g.size)), "p0")
                    #imgutil.imageShow(patch1.reshape((g.size,g.size)),"p1")

                    patchIt = patch1 - patch0
                    pIxt = (patchIt * patchIx * gg).sum()
                    pIyt = (patchIt * patchIy * gg).sum()

                    # solve ATAv = -ATb
                    ATb = -numpy.array([pIxt, pIyt])
                    dv = numpy.linalg.lstsq(ATA, ATb)[0]
                    # update velocity and iterations
                    velocity += dv
                    iterations -= 1

                #print "stopped after", (self.iterations-iterations), "with norm", numpy.dot(dv,dv)**.5

                # calculate new feature positions
                newFeatures[i][:2] += velocity

                #Compute the similarity between our new feature location and the original feature location

                #imgutil.imageShow(patch1.reshape(g.size,g.size), "p0")
                #imgutil.imageShow(self.patches[i], "p1")
                newFeatures[i][2] = imgutil.ncc(patch1, self.patches[i, ...])

                # set feature status (active or inactive)
                if newFeatures[i][0] > w or newFeatures[i][
                        1] > h or newFeatures[i][0] < 0 or newFeatures[i][
                            1] < 0:
                    newFeatures[i][3] = 0

            self.prevFeatures = newFeatures
            self.prevInpt = inpt
            self.getOutput(0).setData(newFeatures)

        self.frame += 1
Пример #3
0
 def generateData(self):
     print "Frame: %d" % (self.frame_number)
     
     # if on the first frame
     if self.frame_number == 0:
         img = self.getInput(0).getData()
         self.last_img = img
         
         harris_corners = self.getInput(1).getData()
         #NumFeaturesx4 array containing the position, strength & active flag
         features = numpy.ones((harris_corners.shape[0],4), dtype=numpy.float32)
         features[:,:2]=harris_corners[:,:2] #->initialize features to harris corners
         self.last_features = features # 
         self.getOutput(0).setData(features)
         self.feature_list.append(features)
         
         
         #make a bank of patches
         r = numpy.floor(self.sigma_d * 5.0/2)
         self.patches = numpy.zeros((harris_corners.shape[0], (2*r+1)**2), dtype=numpy.float32)
         for i, (x,y, _, _) in enumerate(features):
             self.patches[i,...] = img[y-r:y+r+1, x-r:x+r+1].flatten()
     else:
         features = numpy.copy(self.last_features)
         img = self.getInput(0).getData()
         Ixx, Iyy, Ixy = self.getInput(2).getData()
         Ix, Iy = self.getInput(3).getData()
         
         #initialize features to be stationary
         v = numpy.array([0.0, 0.0]) 
         
         #loop through all features
         for i in range(features.shape[0]):
             
            #skip if not active 
            if features[i][3] != 0:
                 x,y = features[i][:2]
                 
                 #compute A^T*A
                 A = numpy.array([[Ixx[y,x],Ixy[y,x]],
                                   [Ixy[y,x],Iyy[y,x]]])            
                 
                 #velocity difference
                 delta_v = numpy.array([100.0,100.0])
                 
                 g = imgutil.gaussian(self.sigma_d)[0]
                 g = g[:,None]
                 gg = numpy.dot(g, g.transpose()).flatten()
                 r = g.size/2
                 iyy, ixx = numpy.mgrid[-r:r+1, -r:r+1]
                 ryy = y+iyy
                 rxx = x+ixx
                 
                 patchIx = interpolation.map_coordinates(Ix, numpy.array([ryy.flatten(), rxx.flatten()]))
                 patchIy = interpolation.map_coordinates(Iy, numpy.array([ryy.flatten(), rxx.flatten()]))
                 
                 #loop through to calculate velocity
                 iterations = 10
                 eps = float('1.0e-3')**2
                 while iterations > 0 and numpy.dot(delta_v, delta_v)> eps: 
                 
                     curr_patch = interpolation.map_coordinates(img, numpy.array([ryy.flatten(), rxx.flatten()]))
                     prev_patch = interpolation.map_coordinates(self.last_img,
                         numpy.array([(ryy-v[1]).flatten(), (rxx-v[0]).flatten()]))
                     
                     #find temporal derivative
                     patch_tderiv = curr_patch-prev_patch
                     pIxt = (patch_tderiv*patchIx*gg).sum()
                     pIyt = (patch_tderiv*patchIy*gg).sum()
                     
                     #ATA = -ATb
                     ATb = -numpy.array([pIxt, pIyt])
                     delta_v = numpy.linalg.lstsq(A, ATb)[0]
                     
                     #update velocities
                     v += delta_v
                     iterations -= 1 
                 
                 
                 # find positions of new features
                 features[i][:2]+= v
                 
                 # weight for similarity to initial feature patch
                 features[i][2] = imgutil.ncc(curr_patch, self.patches[i,...])
                 
                 # make inactive if out of frame
                 h,w = img.shape
                 if features[i][0] > w or features[i][1] > h or features[i][0] < 0 or features[i][1] < 0:
                     features[i][3] = 0
             
         self.last_features = features
         self.last_img = img
         self.getOutput(0).setData(features)
         self.feature_list.append(features)
         
     self.frame_number += 1
Пример #4
0
    def generateData(self):
        
        input = self.getInput(0).getData()
        mask = self.getInput(1).getData()
        r = self.r
        
        #if there is no histogram for the initial object to be tracked, grab one
        if self.hist == None:
            self.hist = self.make_histogram(input, self.x, self.r)
            x,y = self.x[0][:]
            self.feature_patch = input[x-r:x+r,y-r:y+r].flatten()
            self.getOutput().setData(self.pos)
            
            
        else:
        
            #perturb particles, clip for size, make histograms for each particle
            self.x += numpy.random.uniform(-self.stepsize, self.stepsize, self.x.shape)

            #clip to exclude the wall
            self.x = self.x.clip(numpy.array([0,103]), numpy.array(input.shape)-1).astype(int)

            # List of (x,y) positions fish is present (already excludes wall)
            nonzero_positions = numpy.argwhere(mask != 0)

            # Ensure that all particles are on the fish
            # If a particle is off the fish, randomly choose one on its body
            for i in range(self.x.shape[0]):
                y,x = self.x[i,:]
                if mask[y,x] == 0.0:
                    self.x[i,:] = random.choice(nonzero_positions)

                    #offset = numpy.random.randint(-self.stepsize, self.stepsize, (2))
                    #temp = numpy.array([y,x]).clip(numpy.array([0,103]), numpy.array(input.shape)-1)
                    #y,x = temp
            
            new_hist = self.make_histogram(input, self.x, self.stepsize)
            
            #calculate weights (as battacharyya distances)
            w = self.get_weights(new_hist, self.hist)
            w /= numpy.sum(w) #normalize
            
            if self.best:
                #picks the location of the best particle
                new_pos = self.x[numpy.argmax(w),:]
            
            else:
                #sums the weighted particle positions
                new_pos = numpy.sum(self.x.T*w, axis = 1)
            
            
            
            self.getOutput(0).setData(new_pos)
            #self.setOutput(True,1)
            x, y = new_pos
            self.new_patch = input[x-r:x+r,y-r:y+r].flatten()
            ncc = imgutil.ncc(self.feature_patch, self.new_patch)
            print "NCC = %d" % (ncc)
            #if ncc value < threshold, eye is not in frame
            if ncc < self.threshold:
            	#self.setOutput(1, False)
            	pass
            	
            self.x = numpy.ones((self.n,2), int) * new_pos
            self.pos = new_pos
            
            
            if 1./sum(w**2) < self.n/2.:
                self.x = self.x[self.resample(w),:]