from typing import Union import cv2 import numpy as np from PIL import Image from cv_analysis.utils import copy_and_normalize_channels def draw_contours(image, contours): image = copy_and_normalize_channels(image) for contour in contours: cv2.drawContours(image, contour, -1, (0, 255, 0), 4) return image def draw_rectangles(image: Union[np.ndarray, Image.Image], rectangles, color=None, annotate=False, filled=False): def annotate_rect(x, y, w, h): cv2.putText( image, "+", (x + (w // 2) - 12, y + (h // 2) + 9), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0, 100), 2, ) image = copy_and_normalize_channels(image) if not color: color = (0, 255, 0) for rect in rectangles: x, y, w, h = rect cv2.rectangle(image, (x, y), (x + w, y + h), color, -1 if filled else 2) if annotate: annotate_rect(x, y, w, h) return image