def search_sequence_cv2(arr, seq): """ Find sequence in an array using cv2. Copied from Source: https://stackoverflow.com/a/36535397 Inputs ------ * arr (list or np.array): The array to search the sequence in. * seq (list or np.array): The sequence to be searched. Returns ------- * (list): Index(s) where the sequence match was found. Returns empty list if not found any. """ # Run a template match with input sequence as the template across # the entire length of the input array and get scores. arr = np.array(arr) seq = np.array(seq) S = cv2m(arr.astype('uint8'),seq.astype('uint8'),cv2.TM_SQDIFF) # Now, with floating point array cases, the matching scores might not be # exactly zeros, but would be very small numbers as compared to others. # So, for that use a very small to be used to threshold the scorees # against and decide for matches. thresh = 1e-5 # Would depend on elements in seq. So, be careful setting this. # Find the matching indices idx = np.where(S.ravel() < thresh)[0] if len(idx) > 0: return idx else: return []
def get_legal_actions(self, joint_states): assert len(joint_states) == 2 board = joint_states[0] + joint_states[1] legal_moves = [] for k in range(board.shape[1]): match = cv2m(self.legal_move_pattern.astype(self.datatype), board[:, k].astype(self.datatype), cv2.TM_SQDIFF) i, j = np.where(match == 0) if len(i) != 0: legal_moves.append(np.max(i) * board.shape[1] + k) return legal_moves
def search_sequence_cv2(arr,seq): """ Find sequence in an array using cv2. """ # Run a template match with input sequence as the template across # the entire length of the input array and get scores. S = cv2m(arr.astype('uint8'),seq.astype('uint8'),cv2.TM_SQDIFF) # Now, with floating point array cases, the matching scores might not be # exactly zeros, but would be very small numbers as compared to others. # So, for that use a very small to be used to threshold the scorees # against and decide for matches. thresh = 1e-5 # Would depend on elements in seq. So, be careful setting this. # Find the matching indices idx = np.where(S.ravel() < thresh)[0] # Get the range of those indices as final output if len(idx)>0: return np.unique((idx[:,None] + np.arange(seq.size)).ravel()) else: return [] # No match found
def cv2_based(field_array, match_array): M = cv2m(field_array, match_array, cv2.TM_SQDIFF_NORMED) return np.where(1e-6 >= M)
def findSuper8Sprocket3(image, filename): matches = cv2m(image, arrToFind, cv2.TM_SQDIFF) (minVal, maxVal, minLoc, maxLoc) = cv2.minMaxLoc(matches) return minLoc