File size: 2,355 Bytes
098c98c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
from typing import *
from pathlib import Path

import cv2
import numpy as np
from scipy.stats import norm

def read_image(
        path: Union[str, Path],
        image_size: Union[int, Tuple[int, int], None]=None
        ) -> np.ndarray:
    """Read image from file path in RGB format.

    Args:
        path (str | Path): Path to image file.
        image_size (int | tuple[int, int] | None, optional): Resize image.

    Returns:
        image (np.ndarray): The image array.

    Example:
        >>> read_image("/path/to/image.jpg", 256)
    """
    if isinstance(path, Path):
        path = str(path)
    image = cv2.imread(path)
    image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    if image_size:
        if isinstance(image_size, int):
            image_size = (image_size, image_size)
        image = cv2.resize(image, image_size, interpolation=cv2.INTER_AREA)

    return image

def standardize(
        targets: np.ndarray,
        mean: float,
        std: float,
        center_at: float | None = None
    ) -> np.ndarray:
    """Standardize the targets to the z-domain."""
    targets = np.log(targets)
    standardized = (targets - mean) / std
    if center_at:
        standardized -= (center_at - mean) / std
    return standardized

def normalize_cdf(targets: np.ndarray, threshold: float) -> np.ndarray:
    return norm.cdf(targets - threshold)

def normalize_min_max(
        targets: np.ndarray | np.float32,
        threshold: float | np.ndarray,
        min_val: float | np.ndarray,
        max_val: float | np.ndarray
    ) -> np.ndarray:
    normalized = ((targets - threshold) / (max_val - min_val)) + 0.5
    normalized = np.minimum(normalized, 1)
    normalized = np.maximum(normalized, 0)
    return normalized

def get_boxes(mask: np.ndarray) -> np.ndarray:
    """Get bounding boxes from masks.
    
    Args:
        masks (np.ndarray): Input mask of shape (H, W).

    Returns:
        boxes (np.ndarray): Array of shape (N, 4) containing bounding boxes in xyxy format.
    """
    _, comps = cv2.connectedComponents(mask)
    labels = comps = np.unique(comps)
    boxes = []
    for label in labels[labels != 0]:
        y_loc, x_loc = np.where(comps == label)
        boxes.append((np.min(x_loc), np.min(y_loc), np.max(x_loc), np.ma(y_loc)))
    boxes = np.stack(boxes) if boxes else np.empty((0, 4))
    return boxes