import cv2 import numpy as np import os def clean_pipe_stream_gentle(): src_path = '/Users/davidkotnik/.gemini/antigravity/brain/07019d04-a214-43ab-9565-86f4e8f17e5b/uploaded_media_1769607894587.jpg' print(f"Loading {src_path}") img = cv2.imread(src_path) if img is None: return # 1. Standard Separation (GrabCut) - keeps everything (Pipe, Stream, Walls, Banks) mask = np.zeros(img.shape[:2], np.uint8) bgdModel = np.zeros((1,65),np.float64) fgdModel = np.zeros((1,65),np.float64) h, w = img.shape[:2] cv2.grabCut(img, mask, (10, 10, w-20, h-20), bgdModel, fgdModel, 5, cv2.GC_INIT_WITH_RECT) mask_final = np.where((mask==2)|(mask==0),0,1).astype('uint8') # Not doing any aggressive "Wall Removal" logic now, because it ate the banks. # Instead, we rely on the fact that GrabCut should just remove the GRAY background. # The walls will remain. To fit them better, we can manually crop the BOTTOM few pixels if needed. # 2. Fix Drain Hole (Make Black if it's transparent) # Actually, let's just make the hole Area Opaque regardless. # Assuming drain is separate dark region? No, it's connected. # Let's clean up small holes in the mask so the drain grate isn't see-through. kernel = np.ones((5,5),np.uint8) mask_final = cv2.morphologyEx(mask_final, cv2.MORPH_CLOSE, kernel) # 3. Create Alpha b, g, r = cv2.split(img) alpha = mask_final * 255 img_rgba = cv2.merge([b, g, r, alpha]) # Crop coords = cv2.findNonZero(alpha) if coords is not None: x, y, cw, ch = cv2.boundingRect(coords) img_rgba = img_rgba[y:y+ch, x:x+cw] # Save targets = [ '/Users/davidkotnik/repos/novafarma/main/assets/stream_pipe.png', '/Users/davidkotnik/repos/novafarma/assets/DEMO_FAZA1/Environment/stream_pipe.png' ] for t in targets: cv2.imwrite(t, img_rgba) print(f"Saved {t}") if __name__ == "__main__": clean_pipe_stream_gentle()