Update GrassScene: S1 Max Logic, Infinite Grass mode, new assets (visoka_trava_v2), physics enabled. Time: 15:15
This commit is contained in:
59
scripts/clean_pink_grass.py
Normal file
59
scripts/clean_pink_grass.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
import os
|
||||
|
||||
def remove_pink_bg():
|
||||
input_path = '/Users/davidkotnik/.gemini/antigravity/brain/63340bd3-91a9-439d-b1d9-5692ce5adaea/visoka_trava_v2_pink_bg_1769436757738.png'
|
||||
output_path = 'assets/DEMO_FAZA1/Vegetation/visoka_trava_v2.png'
|
||||
|
||||
# Ensure directory exists
|
||||
os.makedirs(os.path.dirname(output_path), exist_ok=True)
|
||||
|
||||
# Read image
|
||||
img = cv2.imread(input_path)
|
||||
if img is None:
|
||||
print(f"Error: Could not read {input_path}")
|
||||
return
|
||||
|
||||
# Convert to RGBA
|
||||
img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
|
||||
|
||||
# Define Magenta range (B, G, R) - standard magenta is (255, 0, 255) in BGR
|
||||
# We'll allow a small tolerance to catch anti-aliasing edges
|
||||
sensitivity = 20
|
||||
lower_magenta = np.array([255 - sensitivity, 0, 255 - sensitivity])
|
||||
upper_magenta = np.array([255, sensitivity, 255])
|
||||
|
||||
# Create mask
|
||||
# Note: OpenCV reads as BGR, so Pink #FF00FF is (255, 0, 255)
|
||||
# Using simple color thresholding
|
||||
mask = cv2.inRange(img[:,:,:3], lower_magenta, upper_magenta)
|
||||
|
||||
# Invert mask (we want to keep non-magenta)
|
||||
mask_inv = cv2.bitwise_not(mask)
|
||||
|
||||
# Apply mask to alpha channel
|
||||
# Where mask is white (magenta pixels), alpha becomes 0
|
||||
# But mask_inv is white for KEEP pixels.
|
||||
# So we want Alpha to be 0 where mask is 255.
|
||||
|
||||
# Better approach:
|
||||
# 1. Split channels
|
||||
b, g, r, a = cv2.split(img)
|
||||
|
||||
# 2. Update alpha channel using the inverted mask
|
||||
# If mask pixel is 255 (magenta), mask_inv is 0. We want alpha 0 there.
|
||||
# If mask pixel is 0 (not magenta), mask_inv is 255. We want alpha 255 there.
|
||||
# However, original alpha is 255 everywhere inside the image bounds.
|
||||
# So we can just take bitwise_and or just set alpha to mask_inv.
|
||||
|
||||
img[:, :, 3] = mask_inv
|
||||
|
||||
# Optional: Basic edge smoothing/despiking if needed, but for pixel/vector art simple cut is often better.
|
||||
|
||||
# Save
|
||||
cv2.imwrite(output_path, img)
|
||||
print(f"Successfully saved transparent image to {output_path}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
remove_pink_bg()
|
||||
71
scripts/process_hay.py
Normal file
71
scripts/process_hay.py
Normal file
@@ -0,0 +1,71 @@
|
||||
import cv2
|
||||
import numpy as np
|
||||
import os
|
||||
|
||||
def process_hay():
|
||||
# Paths
|
||||
input_path = 'assets/references/proizvodnja_sena.png'
|
||||
output_dir = 'assets/DEMO_FAZA1/Items/Hay'
|
||||
|
||||
if not os.path.exists(output_dir):
|
||||
os.makedirs(output_dir)
|
||||
|
||||
# Load image
|
||||
img = cv2.imread(input_path)
|
||||
if img is None:
|
||||
print(f"Error: Could not load {input_path}")
|
||||
return
|
||||
|
||||
# Convert to RGBA
|
||||
img = cv2.cvtColor(img, cv2.COLOR_BGR2BGRA)
|
||||
|
||||
# Define Green Key (assuming bright green background)
|
||||
# Range for Chroma Key Green in HSV is best, but BGR is fine for simple flat green
|
||||
# Pure Green in BGR is (0, 255, 0)
|
||||
|
||||
# Using HSV for better masking
|
||||
hsv = cv2.cvtColor(img[:,:,:3], cv2.COLOR_BGR2HSV)
|
||||
|
||||
# Mask for Green (Hue 35-85 approx for standard green screen)
|
||||
lower_green = np.array([35, 50, 50])
|
||||
upper_green = np.array([85, 255, 255])
|
||||
|
||||
mask = cv2.inRange(hsv, lower_green, upper_green)
|
||||
|
||||
# Invert mask (we want non-green parts)
|
||||
mask_inv = cv2.bitwise_not(mask)
|
||||
|
||||
# Set Alpha channel based on mask
|
||||
# Everything green becomes transparent
|
||||
img[:, :, 3] = mask_inv
|
||||
|
||||
# Find contours of objects
|
||||
# Use the mask_inv (where 255 is the object)
|
||||
contours, _ = cv2.findContours(mask_inv, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
||||
|
||||
print(f"Found {len(contours)} objects.")
|
||||
|
||||
count = 0
|
||||
for cnt in contours:
|
||||
x, y, w, h = cv2.boundingRect(cnt)
|
||||
|
||||
# Filter noise
|
||||
if w < 10 or h < 10:
|
||||
continue
|
||||
|
||||
# Crop
|
||||
crop = img[y:y+h, x:x+w]
|
||||
|
||||
# Determine if it's a small pile or big bale based on size
|
||||
# This is a heuristic. We'll label them by size.
|
||||
area = w * h
|
||||
label = "hay_piece"
|
||||
|
||||
# Save
|
||||
output_filename = f"{output_dir}/hay_drop_{count}.png"
|
||||
cv2.imwrite(output_filename, crop)
|
||||
print(f"Saved {output_filename} (Size: {w}x{h})")
|
||||
count += 1
|
||||
|
||||
if __name__ == "__main__":
|
||||
process_hay()
|
||||
Reference in New Issue
Block a user