File size: 611 Bytes
6add590
 
 
 
9f9ed53
6add590
9f9ed53
6add590
 
 
 
9f9ed53
 
 
 
 
 
 
6add590
 
9f9ed53
6add590
 
9f9ed53
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
import cv2
import numpy as np


def extract_bubble_mask(image):
    """
    Extrai a máscara da bolha e seu contorno principal.
    """
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    _, thresh = cv2.threshold(gray, 240, 255, cv2.THRESH_BINARY)

    contours, _ = cv2.findContours(
        thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
    )

    if not contours:
        return None, None

    largest_contour = max(contours, key=cv2.contourArea)

    mask = np.zeros(gray.shape, dtype=np.uint8)
    cv2.drawContours(mask, [largest_contour], -1, 255, cv2.FILLED)

    return mask, largest_contour