feat(extract): Pixel-Klassifizierung mit Lab-Palette

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
knsoandClaude Sonnet 5 committed 2026-09-12 14:13:35 +02:00
1 parent d40d96a800
commit 593ccf217f
2 files changed
+79

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
import numpy as np
from leonida.classify import CLASSES, PALETTE, class_shares, classify
def block(color, size=8):
return np.full((size, size, 3), color, dtype=np.uint8)
def test_pure_colors_hit_their_class():
for name, color in PALETTE.items():
labels = classify(block(color))
assert (labels == CLASSES[name]).all(), name
def test_jpeg_noise_still_classifies():
rng = np.random.default_rng(0)
noisy = block(PALETTE["strasse"], 32).astype(np.int16)
noisy += rng.integers(-10, 11, noisy.shape) # JPEG-Rauschen
labels = classify(np.clip(noisy, 0, 255).astype(np.uint8))
assert (labels == CLASSES["strasse"]).mean() > 0.98
def test_unknown_pixels_are_filled_from_neighbours():
img = block(PALETTE["stadt"], 16)
img[7:9, 7:9] = (255, 0, 255) # Beschriftung, in keiner Klasse
labels = classify(img)
assert (labels == CLASSES["stadt"]).all()
def test_reports_the_share_of_unknown_pixels():
img = block(PALETTE["stadt"], 16)
img[7:9, 7:9] = (255, 0, 255) # 4 von 256 Pixeln passen zu keiner Farbe
labels, share = classify(img, report_unknown=True)
assert (labels == CLASSES["stadt"]).all()
assert abs(share - 4 / 256) < 1e-9
def test_shares_sum_to_one():
img = np.concatenate([block(PALETTE["wasser"], 8), block(PALETTE["land"], 8)], axis=1)
shares = class_shares(classify(img))
assert abs(sum(shares.values()) - 1.0) < 1e-9
assert abs(shares["wasser"] - 0.5) < 1e-9