Skip to content

Commit 182f773

Browse files
fix margins on annotated pages
1 parent 9d29d26 commit 182f773

83 files changed

Lines changed: 106 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
"""
2+
Apply image registration to annotated score pages.
3+
Act1: resize by 1/1.064, crop (215, 156, 2181, 2946) → 1966×2790
4+
Act3: crop (0, 0, 1977, 2743)
5+
"""
6+
import numpy as np
7+
from PIL import Image
8+
from skimage.registration import phase_cross_correlation
9+
import os
10+
11+
ACT1_ANNOTATED = r'D:\Wozzeck\OperaVisualization\site\data\pages\Act1\annotated'
12+
ACT1_PARENT = r'D:\Wozzeck\OperaVisualization\site\data\pages\Act1'
13+
ACT3_ANNOTATED = r'D:\Wozzeck\OperaVisualization\site\data\pages\Act3\annotated'
14+
ACT3_PARENT = r'D:\Wozzeck\OperaVisualization\site\data\pages\Act3'
15+
16+
SCALE = 1.064
17+
18+
def get_act1_offset(sheet_num):
19+
"""Compute per-sheet offset via phase cross-correlation at full resolution."""
20+
pa = np.array(Image.open(f'{ACT1_PARENT}/sheet{sheet_num}.png').convert('L'), dtype=float)
21+
ann = Image.open(f'{ACT1_ANNOTATED}/sheet{sheet_num}.png').convert('L')
22+
new_w, new_h = round(2550 / SCALE), round(3300 / SCALE)
23+
ann_r = ann.resize((new_w, new_h), Image.LANCZOS)
24+
ar = np.array(ann_r, dtype=float)
25+
26+
ph, pw = pa.shape
27+
ah, aw = ar.shape
28+
pa_padded = np.zeros_like(ar)
29+
pa_padded[:ph, :pw] = pa
30+
31+
shift, error, _ = phase_cross_correlation(ar, pa_padded, upsample_factor=10)
32+
oy, ox = int(round(shift[0])), int(round(shift[1]))
33+
return ox, oy, error
34+
35+
def ncc(a, b):
36+
a = a.astype(float)
37+
b = b.astype(float)
38+
a -= a.mean(); b -= b.mean()
39+
denom = np.std(a) * np.std(b)
40+
return float(np.mean(a * b) / denom) if denom > 1e-6 else 0.0
41+
42+
# --- Verify offset on sample sheets ---
43+
FIXED_OX, FIXED_OY = 215, 156
44+
CROP_BOX = (FIXED_OX, FIXED_OY, FIXED_OX + 1966, FIXED_OY + 2790)
45+
46+
print("=== Verifying act1 offset on sample sheets ===")
47+
sample_sheets = [64, 68, 72, 76, 80, 84, 88, 92, 96, 100, 104, 105]
48+
offsets = []
49+
for s in sample_sheets:
50+
ox, oy, err = get_act1_offset(s)
51+
offsets.append((ox, oy))
52+
print(f" sheet{s}: offset=({ox},{oy}), error={err:.4f}")
53+
54+
xs = [o[0] for o in offsets]
55+
ys = [o[1] for o in offsets]
56+
print(f"\nOffset x: min={min(xs)}, max={max(xs)}, mean={np.mean(xs):.1f}")
57+
print(f"Offset y: min={min(ys)}, max={max(ys)}, mean={np.mean(ys):.1f}")
58+
59+
# Verified offset: (215, 156) is consistent across all sheets except sheet64 which is already processed.
60+
# 11 of 12 samples gave exactly (215,156); sheet64 gave garbage because it's already 1966x2790.
61+
OX, OY = 215, 156
62+
CROP_BOX_ACT1 = (OX, OY, OX + 1966, OY + 2790)
63+
print(f"\nUsing fixed crop box: {CROP_BOX_ACT1}")
64+
65+
# --- Apply transformation to all Act1 annotated images ---
66+
print("\n=== Applying Act1 transformations ===")
67+
new_w, new_h = round(2550 / SCALE), round(3300 / SCALE) # 2397x3102
68+
69+
for sheet_num in range(64, 106):
70+
src = f'{ACT1_ANNOTATED}/sheet{sheet_num}.png'
71+
if not os.path.exists(src):
72+
print(f" SKIP sheet{sheet_num} (not found)")
73+
continue
74+
ann = Image.open(src)
75+
if ann.size == (2550, 3300):
76+
ann_resized = ann.resize((new_w, new_h), Image.LANCZOS)
77+
result = ann_resized.crop(CROP_BOX_ACT1)
78+
result.save(src)
79+
print(f" sheet{sheet_num}: 2550x3300 -> {result.size[0]}x{result.size[1]} OK")
80+
elif ann.size == (1966, 2790):
81+
print(f" sheet{sheet_num}: already 1966x2790, skipping")
82+
else:
83+
print(f" sheet{sheet_num}: unexpected size {ann.size}, skipping")
84+
85+
# --- Apply crop to all Act3 annotated images ---
86+
print("\n=== Applying Act3 crops ===")
87+
act3_sheets = list(range(1, 19)) + list(range(85, 107))
88+
ACT3_CROP = (0, 0, 1977, 2743)
89+
90+
for sheet_num in act3_sheets:
91+
src = f'{ACT3_ANNOTATED}/sheet{sheet_num}.png'
92+
if not os.path.exists(src):
93+
print(f" SKIP sheet{sheet_num} (not found)")
94+
continue
95+
ann = Image.open(src)
96+
if ann.size == (1978, 2743):
97+
result = ann.crop(ACT3_CROP)
98+
result.save(src)
99+
print(f" sheet{sheet_num}: 1978x2743 -> {result.size[0]}x{result.size[1]} OK")
100+
elif ann.size == (1977, 2743):
101+
print(f" sheet{sheet_num}: already 1977x2743, no change needed")
102+
else:
103+
print(f" sheet{sheet_num}: unexpected size {ann.size}, skipping")
104+
105+
print("\nDone.")
106+
370 KB
545 KB
697 KB
613 KB
572 KB
391 KB
454 KB
429 KB
374 KB

0 commit comments

Comments
 (0)