def doTask(self, image): """Compute difference between given image and accumulation, then accumulate and return the difference. Initialize accumulation if needed (if opacity is 100%.)""" # Compute the alpha value. alpha, self.tstamp_prev = util.getAlpha(self.tstamp_prev) # Initalize accumulation if so indicated. if self.image_acc is None: self.image_acc = np.empty(np.shape(image)) # Compute difference. image_diff = cv2.absdiff( self.image_acc.astype(image.dtype), image, ) # Accumulate. hello = cv2.accumulateWeighted( image, self.image_acc, alpha, ) return image_diff
def doTask(self, tstamp): """Compute difference between given image and accumulation, then accumulate and set result with the difference. Initialize accumulation if needed (if opacity is 100%.)""" # Compute the alpha value. alpha, self.tstamp_prev = util.getAlpha(self.tstamp_prev) image = common[tstamp]['image_in'] # Initalize accumulation if so indicated. if self.image_acc is None: self.image_acc = np.empty(np.shape(image)) # Allocate shared memory for the diff image. shape = np.shape(image) dtype = image.dtype image_diff = sharedmem.empty(shape, dtype) # Compute difference. cv2.absdiff( self.image_acc.astype(image.dtype), image, image_diff, ) # Write the framerate on top of the image. fps_text = '{:.2f}, {:.2f}, {:.2f} fps process'.format(*self.framerate.tick()) util.writeOSD(image_diff, ('', fps_text,)) # First line is blank (written to later.) # Write diff image (actually, reference thereof) to process-shared table. hello = common[tstamp] hello['image_diff'] = image_diff common[tstamp] = hello # Propagate result to the next stage. self.putResult(tstamp) # Accumulate. hello = cv2.accumulateWeighted( image, self.image_acc, alpha, )
def doTask(self, tstamp): """Compute difference between given image and accumulation, then accumulate and return the difference. Initialize accumulation if needed (if opacity is 100%.)""" # Compute the alpha value. alpha, self.tstamp_prev = util.getAlpha(self.tstamp_prev) image = common[tstamp]['image_in'] # Initalize accumulation if so indicated. if self.image_acc is None: self.image_acc = np.empty(np.shape(image)) # Allocate shared memory for the diff image. image_diff = sharedmem.empty(np.shape(image), image.dtype) # Compute difference. cv2.absdiff( self.image_acc.astype(image.dtype), image, image_diff, ) # Write diff image (actually, reference thereof) to process-shared table. hello = common[tstamp] hello['image_diff'] = image_diff common[tstamp] = hello # Propagate result to the next stage. self.putResult(tstamp) # Accumulate. hello = cv2.accumulateWeighted( image, self.image_acc, alpha, )
# Keep track of previous iteration's timestamp. tstamp_prev = None # Monitor framerates for the given seconds past. framerate = coils.RateTicker((1, 5, 10)) # Run the loop for designated amount of time. end = datetime.datetime.now() + datetime.timedelta(seconds=DURATION) while end > datetime.datetime.now(): # Take a snapshot and mark the snapshot time. hello, image = cap.read() # Compute alpha value. alpha, tstamp_prev = util.getAlpha(tstamp_prev) # Initalize accumulation if so indicated. if image_acc is None: image_acc = np.empty(np.shape(image)) # Compute difference. image_diff = cv2.absdiff( image_acc.astype(image.dtype), image, ) # Accumulate. hello = cv2.accumulateWeighted( image, image_acc,
def __call__(self): tau = util.tau # Positions des billes après tau up = [] for i in range(len(self.billes)): temp = self.billes[i].testUpdate(tau) up.append([i, temp[0], temp[1], temp[2], temp[3]]) # On regarde s'il y a des conflits conflits = [] # 1. Chocs entre billes tests = [] for elem in up: for j in range(len(up)): if elem[0] != j and [elem[0], j] not in tests and [j, elem[0]] not in tests: tests.append([elem[0], j]) if not self.billes[up[j][0]].rentre: if util.choc([[up[elem[0]][1], up[elem[0]][2]], [up[j][1], up[j][2]]]): conflits.append(["choc", elem[0], j]) # 2. Bille qui s'arrête for elem in up: if not self.billes[elem[0]].rentre: if elem[3] <= 0 and self.billes[elem[0]].v[0] > 0: conflits.append(["v0", elem[0]]) elif elem[4] <= 0 and self.billes[elem[0]].v[1] > 0: conflits.append(["v1", elem[0]]) # 3. Bords du billard for elem in up: if not self.billes[elem[0]].rentre: if util.oob(elem[1:3]): conflits.append(["oob", elem[0]]) if len(conflits) > 0: # On résout le conflit et on récupère le tau correspondant solver(self.billes, conflits) tau = solver.solved[0] # MAJ des billes for b in self.billes: b.update(tau) if len(conflits) > 0: solution = solver.solved[1] # Problème résolu : annulation de la vitesse de glissement if solution[0] == self.V0: self.billes[solution[1]].v[0] = 0 self.billes[solution[1]].glissement = False # Problème résolu : annulation de la vitesse de roulement elif solution[0] == self.V1: self.billes[solution[1]].v[1] = 0 if self.billes[solution[1]].v[0] == 0: self.billes[solution[1]].arret = True # Problème résolu : choc avec les bords elif solution[0] == self.OOB: pos = self.billes[solution[1]].pos # La bille ne tombe pas dans un trou pR = util.t_m * 0.85 gR = util.t_b * 1.5 if not (util.b_d / 2 - pR <= pos[0] <= util.b_d / 2 + pR) \ and not (pos[0] <= util.holes[0][0] + gR and pos[1] <= util.holes[0][1] + gR) \ and not (pos[0] >= util.holes[1][0] - gR and pos[1] <= util.holes[1][1] + gR) \ and not (pos[0] <= util.holes[2][0] + gR and pos[1] >= util.holes[2][1] - gR) \ and not (pos[0] >= util.holes[3][0] - gR and pos[1] >= util.holes[3][1] - gR): dists = [[0, (pos[0] - util.b_g) ** 2], [1, (pos[1] - util.b_h) ** 2], [2, (pos[0] - util.b_d) ** 2], [3, (pos[1] - util.b_b) ** 2]] dists.sort(key=lambda x: x[1]) if dists[0][0] % 2 == 0: self.billes[solution[1]].alpha = np.pi - \ self.billes[solution[1]].alpha else: self.billes[solution[1]].alpha = - \ self.billes[solution[1]].alpha self.billes[solution[1]].v /= np.sqrt(2) # La bille tombe dans un trou else: self.billes[solution[1]].arret = True self.billes[solution[1]].rentre = True # Problème résolu : choc entre bille elif solution[0] == self.CHOC: v1o, v2o = util.getOrth( self.billes[solution[1]], self.billes[solution[2]]) v1p, v2p = util.getPar( self.billes[solution[1]], self.billes[solution[2]]) # Choc v1of = v2o v2of = v1o v1pf = v1p v2pf = v2p alpha = util.getAlpha( self.billes[solution[1]], self.billes[solution[2]]) self.billes[solution[1]].alpha = np.pi - \ alpha - np.arctan2(v1pf, v1of) self.billes[solution[2]].alpha = alpha - \ np.pi / 2 + np.arctan2(v2of, v2pf) self.billes[solution[1]].v[0] = 0 self.billes[solution[2]].v[0] = 0 self.billes[solution[1]].glissement = False self.billes[solution[1]].glissement = False self.billes[solution[1]].v[1] = np.sqrt(v1of ** 2 + v1pf ** 2) self.billes[solution[2]].v[1] = np.sqrt(v2of ** 2 + v2pf ** 2) if self.billes[solution[1]].v[1] <= 0: self.billes[solution[1]].arret = True else: self.billes[solution[1]].arret = False if self.billes[solution[2]].v[1] <= 0: self.billes[solution[2]].arret = True else: self.billes[solution[2]].arret = False self.lastTau = tau