spancmp.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. #!/usr/bin/env python3
  2. """spancmp.py <we dir> <ours dir> [WxH+X+Y ...] - compares we_span.sh and ours_span.sh grabs of the same layout per
  3. output: mean difference, share of pixels off by more than 24 levels, and the shift that lines the grids up
  4. (phase correlation); writes <ours dir>/cmp_<n>.png (WE | ours | diff) per grab pair"""
  5. import glob
  6. import sys
  7. import numpy as np
  8. from PIL import Image
  9. we_dir, ours_dir = sys.argv[1], sys.argv[2]
  10. specs = sys.argv[3:] or ['1920x1080+0+0', '1280x1024+2120+200']
  11. rects = []
  12. for spec in specs:
  13. size, x, y = spec.split('+')
  14. w, h = size.split('x')
  15. rects.append((int(x), int(y), int(w), int(h)))
  16. def shift(a, b):
  17. fa, fb = np.fft.fft2(a.mean(2)), np.fft.fft2(b.mean(2))
  18. r = np.fft.ifft2(fa * np.conj(fb) / (np.abs(fa * np.conj(fb)) + 1e-9)).real
  19. dy, dx = np.unravel_index(np.argmax(r), r.shape)
  20. return (dx if dx < a.shape[1] // 2 else dx - a.shape[1]), (dy if dy < a.shape[0] // 2 else dy - a.shape[0])
  21. for n, (wf, of) in enumerate(zip(sorted(glob.glob(we_dir + '/f*.png')), sorted(glob.glob(ours_dir + '/f*.png')))):
  22. we = np.asarray(Image.open(wf).convert('RGB')).astype(np.int16)
  23. ours = np.asarray(Image.open(of).convert('RGB')).astype(np.int16)
  24. if we.shape != ours.shape:
  25. print(n, 'size differs', we.shape, ours.shape)
  26. continue
  27. diff = np.abs(we - ours).max(2)
  28. for i, (x, y, w, h) in enumerate(rects):
  29. d = diff[y:y + h, x:x + w]
  30. print('grab %d output %d: mean %.2f, >24: %.3f%%, shift %s' % (
  31. n, i + 1, d.mean(), (d > 24).mean() * 100, shift(we[y:y + h, x:x + w], ours[y:y + h, x:x + w])))
  32. out = np.concatenate([we, ours, np.repeat(np.minimum(diff * 4, 255)[..., None], 3, 2)], 0).astype(np.uint8)
  33. Image.fromarray(out).resize((out.shape[1] // 3, out.shape[0] // 3)).save('%s/cmp_%d.png' % (ours_dir, n))