Implement grouping score, customizable export weights, and fix color selection bug

This commit is contained in:
lm
2026-03-23 21:00:16 +01:00
parent 1c48a53c19
commit ef648cc676
5 changed files with 255 additions and 22 deletions
+36 -2
View File
@@ -19,7 +19,8 @@ def test_stats_summary():
return key
return f"{kwargs['with_pct']:.1f} {kwargs['without_pct']:.1f} {kwargs['excluded_pct']:.1f}"
res = s.summary(mock_t)
weights = {"match_all": 30, "match_keep": 50, "brightness": 10, "grouping": 10}
res = s.summary(mock_t, weights)
# with_pct: 40/80 = 50.0
# without_pct: 50/100 = 50.0
# excluded_pct: 20/100 = 20.0
@@ -27,7 +28,8 @@ def test_stats_summary():
def test_stats_empty():
s = Stats()
assert s.summary(lambda k, **kw: "Empty") == "Empty"
weights = {"match_all": 30, "match_keep": 50, "brightness": 10, "grouping": 10}
assert s.summary(lambda k, **kw: "Empty", weights) == "Empty"
def test_rgb_to_hsv_numpy():
@@ -121,3 +123,35 @@ def test_coordinate_scaling():
assert s_large.total_all == 1000000
assert s_large.total_keep == 500000
assert s_large.total_excl == 500000
def test_calculate_grouping_score():
proc = QtImageProcessor()
# 1. Empty mask
mask_empty = np.zeros((20, 20), dtype=bool)
assert proc._calculate_grouping_score(mask_empty) == 0.0
# 2. Single mask pixel (0 neighbors)
mask_single = np.zeros((20, 20), dtype=bool)
mask_single[10, 10] = True
assert proc._calculate_grouping_score(mask_single) == 0.0
# 3. 2x2 block
# each pixel in 2x2 has 3 neighbors in 3x3, 3 neighbors in 5x5, 3 neighbors in 9x9.
# score = ((3/80)^2) * 100
expected_2x2 = ((3/80.0)**2) * 100.0
mask_block = np.zeros((20, 20), dtype=bool)
mask_block[10:12, 10:12] = True
assert pytest.approx(proc._calculate_grouping_score(mask_block)) == expected_2x2
# 4. 9x9 block
# center pixel has 80 neighbors (100% density).
# many pixels have high density.
mask_9x9 = np.zeros((20, 20), dtype=bool)
mask_9x9[5:14, 5:14] = True
res_9x9 = proc._calculate_grouping_score(mask_9x9)
assert res_9x9 > expected_2x2
# For a 9x9 block, the center pixel is 100%. Boundary pixels are less.
# 1 center pixel = 80/80 = 1.0.
# Overall it should be a healthy percentage.
assert res_9x9 > 10.0 # significant grouping