Spaces:
Running
Running
| import cv2 | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| 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 | |