File size: 1,140 Bytes
144afae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import numpy as np
import cv2

def visualize_mask(image, mask, alpha=0.5):
    """
    Overlay mask on image for visualization.
    Automatically resizes mask to match image dimensions.
    """
    image = np.array(image)
    mask = np.array(mask)
    
    h, w = image.shape[:2]
    mh, mw = mask.shape[:2]

    if (h, w) != (mh, mw):
        mask = cv2.resize(mask, (w, h), interpolation=cv2.INTER_NEAREST)

    overlay = image.copy()
    overlay[mask > 0] = [255, 0, 0]
    
    return cv2.addWeighted(image, 1-alpha, overlay, alpha, 0)

def combine_masks(masks):
    """Combine multiple masks into one"""
    if not masks:
        return None
    combined = np.zeros_like(masks[0])
    for mask in masks:
        combined = np.logical_or(combined, mask).astype(np.uint8)
    return combined

def resize_image(image, max_size=1024):
    """Resize image while maintaining aspect ratio"""
    h, w = image.shape[:2]
    if max(h, w) > max_size:
        scale = max_size / max(h, w)
        new_w = int(w * scale)
        new_h = int(h * scale)
        return cv2.resize(image, (new_w, new_h), interpolation=cv2.INTER_AREA)
    return image