by Anonymous » 27 Mar 2025, 15:41
Skriptausgabe < /p>
Graustufe Rivermask < /p>
Mein Hauptproblem ist, dass mein Skript immer noch Teile des Flusses mehr als 1px breit macht, insbesondere Junctions und das, was ich denke, Kurven des 1px -Radius. Hier ist das Skript: < /p>
Code: Select all
from PIL import Image
import numpy as np
from skimage import morphology
from scipy.ndimage import label
# File paths (update these to match your files)
RIVER_MASK_PATH = "river_mask.png" # e.g., "river_mask.png"
OUTPUT_PATH = "river_map_output.png" # e.g., "river_map_output.png"
# Load river mask as grayscale
river_img = Image.open(RIVER_MASK_PATH).convert("I") # 16-bit grayscale (0-65535)
river_array = np.array(river_img, dtype=np.uint16)
# For 8-bit grayscale, use this instead:
# river_img = Image.open(RIVER_MASK_PATH).convert("L") # 8-bit grayscale (0-255)
# river_array = np.array(river_img, dtype=np.uint8)
# Threshold to create a binary mask (river = True, background = False)
river_binary = river_array > 0
# Skeletonize to make rivers 1px wide
river_skeleton = morphology.skeletonize(river_binary)
# Function to convert to 4-directional connectivity
def convert_to_4connected(skeleton):
height, width = skeleton.shape
river_4dir = skeleton.copy()
for y in range(height - 1):
for x in range(width - 1):
if skeleton[y, x]:
# Check for diagonal connections
if skeleton[y + 1, x + 1] and not (skeleton[y + 1, x] or skeleton[y, x + 1]):
# Break diagonal by adding an intermediate pixel
river_4dir[y + 1, x] = True # Add vertical connection
if skeleton[y + 1, x - 1] and not (skeleton[y + 1, x] or skeleton[y, x - 1]):
river_4dir[y + 1, x] = True
return river_4dir
# Apply 4-directional connectivity
river_4dir = convert_to_4connected(river_skeleton)
# Remove small isolated rivers (≤5 pixels)
labeled_rivers, _ = label(river_4dir)
river_sizes = np.bincount(labeled_rivers.ravel())
small_rivers = np.where((river_sizes 0))[0]
for label_id in small_rivers:
river_4dir[labeled_rivers == label_id] = False
# Define CK3 river colors and width thresholds
def get_river_color(value, max_value=255):
if max_value == 255: # 8-bit
if value
Skriptausgabe < /p>
Graustufe Rivermask < /p>
Mein Hauptproblem ist, dass mein Skript immer noch Teile des Flusses mehr als 1px breit macht, insbesondere Junctions und das, was ich denke, Kurven des 1px -Radius. Hier ist das Skript: < /p>
[code]from PIL import Image
import numpy as np
from skimage import morphology
from scipy.ndimage import label
# File paths (update these to match your files)
RIVER_MASK_PATH = "river_mask.png" # e.g., "river_mask.png"
OUTPUT_PATH = "river_map_output.png" # e.g., "river_map_output.png"
# Load river mask as grayscale
river_img = Image.open(RIVER_MASK_PATH).convert("I") # 16-bit grayscale (0-65535)
river_array = np.array(river_img, dtype=np.uint16)
# For 8-bit grayscale, use this instead:
# river_img = Image.open(RIVER_MASK_PATH).convert("L") # 8-bit grayscale (0-255)
# river_array = np.array(river_img, dtype=np.uint8)
# Threshold to create a binary mask (river = True, background = False)
river_binary = river_array > 0
# Skeletonize to make rivers 1px wide
river_skeleton = morphology.skeletonize(river_binary)
# Function to convert to 4-directional connectivity
def convert_to_4connected(skeleton):
height, width = skeleton.shape
river_4dir = skeleton.copy()
for y in range(height - 1):
for x in range(width - 1):
if skeleton[y, x]:
# Check for diagonal connections
if skeleton[y + 1, x + 1] and not (skeleton[y + 1, x] or skeleton[y, x + 1]):
# Break diagonal by adding an intermediate pixel
river_4dir[y + 1, x] = True # Add vertical connection
if skeleton[y + 1, x - 1] and not (skeleton[y + 1, x] or skeleton[y, x - 1]):
river_4dir[y + 1, x] = True
return river_4dir
# Apply 4-directional connectivity
river_4dir = convert_to_4connected(river_skeleton)
# Remove small isolated rivers (≤5 pixels)
labeled_rivers, _ = label(river_4dir)
river_sizes = np.bincount(labeled_rivers.ravel())
small_rivers = np.where((river_sizes 0))[0]
for label_id in small_rivers:
river_4dir[labeled_rivers == label_id] = False
# Define CK3 river colors and width thresholds
def get_river_color(value, max_value=255):
if max_value == 255: # 8-bit
if value