File size: 1,242 Bytes
c48064c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import numpy as np

def create_mask(image):
    """
    Creates a binary mask from an input image by thresholding and smoothing.
    
    Args:
        image: Input image (numpy array)
        
    Returns:
        Binary mask image (numpy array)
    """
    # Store original image for visualization
    image_org = image.copy()
    
    # Convert image to binary (0 or 255)
    for i in range(len(image)):
        for j in range(len(image[i])):
            if image[i][j] != 255:
                image[i][j] = 0
                
    # Add padding of 50 pixels on all sides
    padding = 0
    image = cv2.copyMakeBorder(image, padding, padding, padding, padding, 
                              cv2.BORDER_CONSTANT, value=255)

    # Apply Gaussian blur for smoothening
    image = cv2.GaussianBlur(image, (5,5), 50)

    # Threshold to create binary mask
    _, mask = cv2.threshold(image, 254, 255, cv2.THRESH_BINARY)
    
    # Visualization (commented out for production use)
    """
    fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 5))
    ax1.imshow(image_org, cmap="gray")
    ax1.set_title("Original Image")
    ax2.imshow(mask, cmap="gray")
    ax2.set_title("Mask Image")
    plt.show()
    """
    
    return mask