def draw_box( out_img: np.ndarray, box: Box, labels: Iterable[Tuple[str, Point]], color: Color = Color.red(), line_thickness: int = 2, ) -> np.ndarray: cv.rectangle( img=out_img, pt1=box.top_left, pt2=box.bottom_right, color=color.to_bgr(), thickness=line_thickness, ) for text, translation in labels: text_loc: Point = translate_point( Point(box.top_left_x, box.bottom_right_y), translation) cv.putText( img=out_img, text=text, org=text_loc, fontFace=cv.FONT_HERSHEY_SIMPLEX, fontScale=0.5, color=Color.orange().to_bgr(), thickness=2, ) return out_img
def detect( self, img: np.ndarray, out_img: np.ndarray = None, color: Color = Color.yellow(), line_thickness: int = 2, ) -> np.ndarray: """ Detect self.template in img and draw a box around it. :param img: The image to detect the template in :param out_img: The image to draw on (should be at least as large as img) :param color: The color of the bounding box :param line_thickness: The thickness of he bounding box line :return: out_img """ # TODO: Make this scale invariant. # see https://www.pyimagesearch.com/2015/01/26/multi-scale-template-matching-using-python-opencv/ if out_img is None: out_img = img.copy() else: assert all( img_dim <= out_img_dim for img_dim, out_img_dim in zip(img.shape, out_img.shape)) similarity_map: np.ndarray = self._compute_similarity_map(img) match_xy_indices: Iterable[Tuple[ int, int]] = self._get_match_xy_indices(similarity_map) # For each match, draw the bounding box for x, y in match_xy_indices: top_left = x, y bottom_right = x + self.temp_w, y + self.temp_h cv.rectangle( img=out_img, pt1=top_left, pt2=bottom_right, color=color.to_bgr(), thickness=line_thickness, ) return out_img