Spaces:
Sleeping
Sleeping
File size: 1,451 Bytes
ad8cacf | 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 | """
Simple threshold-based background removal functionality.
"""
import cv2
import numpy as np
import os
def remove_background_simple(image_path, output_path=None, threshold=240):
"""
Remove background using simple threshold-based method.
Args:
image_path (str): Path to input image
output_path (str, optional): Path for output image. If None, generates new path
threshold (int): Threshold value for background detection (0-255)
Returns:
str: Path to processed image
"""
# Load image
img = cv2.imread(image_path)
if img is None:
print(f"Error: Could not load image: {image_path}")
return None
# Convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Threshold to separate background
_, mask = cv2.threshold(gray, threshold, 255, cv2.THRESH_BINARY_INV)
# Create alpha channel
alpha = np.zeros_like(gray)
alpha[mask > 0] = 255
# Create BGRA image
bgra = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
bgra[:, :, 3] = alpha
# Generate output path if not specified
if output_path is None:
filename, ext = os.path.splitext(image_path)
output_path = f"{filename}_nobg.png"
# Save image
cv2.imwrite(output_path, bgra)
print(f"Saved background-removed image: {output_path}")
return output_path
|