def process_masks_and_save_with_visualization(mask_dir, bump_dir, no_bump_dir, skip=12, threshold=3):
"""
Przetwarza maski, wykrywa obecność garbu, zapisuje je do odpowiednich folderów
i tworzy ich wizualizacje.
"""
if not os.path.exists(bump_dir):
os.makedirs(bump_dir)
if not os.path.exists(no_bump_dir):
os.makedirs(no_bump_dir)
for filename in os.listdir(mask_dir):
file_path = os.path.join(mask_dir, filename)
if os.path.isfile(file_path) and filename.endswith('.png'):
mask = Image.open(file_path).convert('L') # Konwertuj do odcieni szarości
mask_np = np.array(mask)
# Wykrycie garbu
bump_present = detect_bump(mask_np, skip, threshold)
# Obliczanie max_diff
label_bony_roof = 5
binary_mask = (mask_np == label_bony_roof).astype(np.uint8)
height, width = binary_mask.shape
upper_contour = []
for x in range(width):
column = binary_mask[:, x]
if np.any(column):
y = np.where(column)[0][0] # Najwyższy piksel w danej kolumnie
upper_contour.append(y)
else:
upper_contour.append(height)
upper_contour = np.array(upper_contour)
min_y = np.min(upper_contour)
distances = min_y - upper_contour
differences = pd.Series(distances).diff(periods=skip).fillna(0).abs()
max_diff = differences.max()
# Wizualizacja maski
visualized_image = np.zeros((height, width, 3), dtype=np.uint8)
for label, color_info in CLASS_COLORS.items():
color = color_info['color_rgb']
visualized_image[mask_np == label] = color
# Zapisanie do odpowiedniego folderu
if bump_present:
save_path = os.path.join(bump_dir, filename)
else:
save_path = os.path.join(no_bump_dir, filename)
Image.fromarray(visualized_image).save(save_path)
print(f'Zapisano zwizualizowaną maskę do: {save_path} - max_diff: {max_diff}')
process_masks_and_save_with_visualization('./Angles/dane/masks_from_txt', './Angles/dane/garb_5_5/garb_obecny','./Angles/dane/garb_5_5/garb_nieobecny')