Spaces:
Sleeping
Sleeping
Update apply_mask.py
Browse files- apply_mask.py +72 -72
apply_mask.py
CHANGED
|
@@ -1,72 +1,72 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import os
|
| 3 |
-
import glob
|
| 4 |
-
import numpy as np
|
| 5 |
-
|
| 6 |
-
def apply_mask_and_crop(input_folder, mask_path, output_folder):
|
| 7 |
-
"""
|
| 8 |
-
Apply binary mask to all images, crop to masked region, and save to output folder
|
| 9 |
-
|
| 10 |
-
Args:
|
| 11 |
-
input_folder (str): Path to folder containing input images
|
| 12 |
-
mask_path (str): Path to binary mask image
|
| 13 |
-
output_folder (str): Path to save cropped masked images
|
| 14 |
-
"""
|
| 15 |
-
# Load and prepare mask
|
| 16 |
-
mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
|
| 17 |
-
if mask is None:
|
| 18 |
-
raise ValueError(f"Could not load mask from {mask_path}")
|
| 19 |
-
|
| 20 |
-
_, binary_mask = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)
|
| 21 |
-
|
| 22 |
-
# Create output directory if it doesn't exist
|
| 23 |
-
os.makedirs(output_folder, exist_ok=True)
|
| 24 |
-
|
| 25 |
-
# Get list of image files
|
| 26 |
-
image_files = glob.glob(os.path.join(input_folder, "*.jpg")) + \
|
| 27 |
-
glob.glob(os.path.join(input_folder, "*.png")) + \
|
| 28 |
-
glob.glob(os.path.join(input_folder, "*.bmp"))
|
| 29 |
-
|
| 30 |
-
if not image_files:
|
| 31 |
-
print(f"No images found in {input_folder}")
|
| 32 |
-
return
|
| 33 |
-
|
| 34 |
-
print(f"Found {len(image_files)} images to process")
|
| 35 |
-
|
| 36 |
-
for img_path in image_files:
|
| 37 |
-
# Load image
|
| 38 |
-
img = cv2.imread(img_path)
|
| 39 |
-
if img is None:
|
| 40 |
-
print(f"Warning: Could not read image {img_path}")
|
| 41 |
-
continue
|
| 42 |
-
|
| 43 |
-
# Resize mask if dimensions don't match
|
| 44 |
-
if img.shape[:2] != binary_mask.shape[:2]:
|
| 45 |
-
resized_mask = cv2.resize(binary_mask, (img.shape[1], img.shape[0]))
|
| 46 |
-
else:
|
| 47 |
-
resized_mask = binary_mask
|
| 48 |
-
|
| 49 |
-
# Apply mask
|
| 50 |
-
masked_img = cv2.bitwise_and(img, img, mask=resized_mask)
|
| 51 |
-
|
| 52 |
-
# Find contours to get bounding box of mask
|
| 53 |
-
contours, _ = cv2.findContours(resized_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 54 |
-
if not contours:
|
| 55 |
-
print(f"No mask area found in {img_path} - skipping")
|
| 56 |
-
continue
|
| 57 |
-
|
| 58 |
-
# Get bounding rectangle of largest contour
|
| 59 |
-
x, y, w, h = cv2.boundingRect(max(contours, key=cv2.contourArea))
|
| 60 |
-
|
| 61 |
-
# Crop to masked region
|
| 62 |
-
cropped_img = masked_img[y:y+h, x:x+w]
|
| 63 |
-
|
| 64 |
-
# Create output path (preserve original filename)
|
| 65 |
-
filename = os.path.basename(img_path)
|
| 66 |
-
output_path = os.path.join(output_folder, filename)
|
| 67 |
-
|
| 68 |
-
# Save cropped image
|
| 69 |
-
cv2.imwrite(output_path, cropped_img)
|
| 70 |
-
# print(f"Processed and saved: {output_path}")
|
| 71 |
-
|
| 72 |
-
print(f"\nProcessing complete! Saved {len(image_files)} cropped images to {output_folder}")
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import os
|
| 3 |
+
import glob
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
def apply_mask_and_crop(input_folder, mask_path, output_folder):
|
| 7 |
+
"""
|
| 8 |
+
Apply binary mask to all images, crop to masked region, and save to output folder
|
| 9 |
+
|
| 10 |
+
Args:
|
| 11 |
+
input_folder (str): Path to folder containing input images
|
| 12 |
+
mask_path (str): Path to binary mask image
|
| 13 |
+
output_folder (str): Path to save cropped masked images
|
| 14 |
+
"""
|
| 15 |
+
# Load and prepare mask
|
| 16 |
+
# mask = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
|
| 17 |
+
if mask is None:
|
| 18 |
+
raise ValueError(f"Could not load mask from {mask_path}")
|
| 19 |
+
|
| 20 |
+
_, binary_mask = cv2.threshold(mask, 127, 255, cv2.THRESH_BINARY)
|
| 21 |
+
|
| 22 |
+
# Create output directory if it doesn't exist
|
| 23 |
+
os.makedirs(output_folder, exist_ok=True)
|
| 24 |
+
|
| 25 |
+
# Get list of image files
|
| 26 |
+
image_files = glob.glob(os.path.join(input_folder, "*.jpg")) + \
|
| 27 |
+
glob.glob(os.path.join(input_folder, "*.png")) + \
|
| 28 |
+
glob.glob(os.path.join(input_folder, "*.bmp"))
|
| 29 |
+
|
| 30 |
+
if not image_files:
|
| 31 |
+
print(f"No images found in {input_folder}")
|
| 32 |
+
return
|
| 33 |
+
|
| 34 |
+
print(f"Found {len(image_files)} images to process")
|
| 35 |
+
|
| 36 |
+
for img_path in image_files:
|
| 37 |
+
# Load image
|
| 38 |
+
img = cv2.imread(img_path)
|
| 39 |
+
if img is None:
|
| 40 |
+
print(f"Warning: Could not read image {img_path}")
|
| 41 |
+
continue
|
| 42 |
+
|
| 43 |
+
# Resize mask if dimensions don't match
|
| 44 |
+
if img.shape[:2] != binary_mask.shape[:2]:
|
| 45 |
+
resized_mask = cv2.resize(binary_mask, (img.shape[1], img.shape[0]))
|
| 46 |
+
else:
|
| 47 |
+
resized_mask = binary_mask
|
| 48 |
+
|
| 49 |
+
# Apply mask
|
| 50 |
+
masked_img = cv2.bitwise_and(img, img, mask=resized_mask)
|
| 51 |
+
|
| 52 |
+
# Find contours to get bounding box of mask
|
| 53 |
+
contours, _ = cv2.findContours(resized_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 54 |
+
if not contours:
|
| 55 |
+
print(f"No mask area found in {img_path} - skipping")
|
| 56 |
+
continue
|
| 57 |
+
|
| 58 |
+
# Get bounding rectangle of largest contour
|
| 59 |
+
x, y, w, h = cv2.boundingRect(max(contours, key=cv2.contourArea))
|
| 60 |
+
|
| 61 |
+
# Crop to masked region
|
| 62 |
+
cropped_img = masked_img[y:y+h, x:x+w]
|
| 63 |
+
|
| 64 |
+
# Create output path (preserve original filename)
|
| 65 |
+
filename = os.path.basename(img_path)
|
| 66 |
+
output_path = os.path.join(output_folder, filename)
|
| 67 |
+
|
| 68 |
+
# Save cropped image
|
| 69 |
+
cv2.imwrite(output_path, cropped_img)
|
| 70 |
+
# print(f"Processed and saved: {output_path}")
|
| 71 |
+
|
| 72 |
+
print(f"\nProcessing complete! Saved {len(image_files)} cropped images to {output_folder}")
|