def image_exact(haystack: Image, needle: Image) -> List[Box]: """ Find an image (exactly) within another image. Parameters ---------- haystack the image to be looked in needle the image to be found Returns ------- matches bounding boxes around any matches """ matches = [] needle = needle.copy() needle[np.where(needle.mask is True)] = [0, 0, 0] sheight, swidth, _ = needle.shape ty, tx, _ = haystack.shape # total x,y if swidth > tx or sheight > ty: raise Exception('Needle is larger than haystack.') for y in range(ty): # iterate y 1st cus text travels horizontal if y + sheight > ty: break for x in range(tx): if x + swidth > tx: break s_haystack = haystack.copy()[y:y + sheight, x:x + swidth] s_haystack[np.where(needle.mask is True)] = [0, 0, 0] needle.mask = False # remove the mask cus we dont need it no more if np.all(needle[0, 0] == s_haystack[0, 0]): if np.all(needle == s_haystack): match = Box.from_array([x, y, x + swidth, y + sheight]) matches.append(match) return matches
def get_image(self, img: Image) -> Image: img = self.get_image_slice(img) img.mask = self.mask img.alpha_mask = True return img