74 lines
2.3 KiB
Python
74 lines
2.3 KiB
Python
from PIL import Image
|
|
import glob, statistics
|
|
|
|
files = sorted(glob.glob(r"C:\Users\29452\AppData\Local\Temp\opencode\seq*.png"))
|
|
imgs = [Image.open(f).convert("L") for f in files]
|
|
W, H = imgs[0].size
|
|
n = len(imgs)
|
|
pxs = [im.load() for im in imgs]
|
|
|
|
# temporal std per pixel (sample every pixel but coarse for speed? do full)
|
|
# spatial gradient on mean image
|
|
# Build mean and std
|
|
import array
|
|
step = 1
|
|
cand = Image.new("L", (W, H), 0)
|
|
cp = cand.load()
|
|
for y in range(0, H, 2):
|
|
for x in range(0, W, 2):
|
|
vals = [px[x, y] for px in pxs]
|
|
m = sum(vals) / n
|
|
var = sum((v - m) ** 2 for v in vals) / n
|
|
std = var ** 0.5
|
|
# spatial contrast from mean image
|
|
xm = x - 2 if x - 2 >= 0 else x
|
|
xp = x + 2 if x + 2 < W else x
|
|
gx = abs(pxs[0][xp, y] - pxs[0][xm, y]) # placeholder
|
|
# use mean image gradient
|
|
mean_px = [[sum(px[xx, yy] for px in pxs) / n for xx in (x,)] for yy in (y,)]
|
|
cp[x, y] = 255 - min(255, int(std))
|
|
import os
|
|
# Better: compute static mask = std < 6, then high contrast of mean
|
|
mean = Image.new("L", (W, H), 0)
|
|
mp = mean.load()
|
|
for y in range(H):
|
|
for x in range(W):
|
|
mp[x, y] = int(sum(px[x, y] for px in pxs) / n)
|
|
# gradient of mean
|
|
ed = Image.new("L", (W, H), 0)
|
|
ep = ed.load()
|
|
for y in range(1, H - 1):
|
|
for x in range(1, W - 1):
|
|
g = abs(mp[x + 1, y] - mp[x - 1, y]) + abs(mp[x, y + 1] - mp[x, y - 1])
|
|
ep[x, y] = min(255, g)
|
|
# static mask
|
|
st = Image.new("L", (W, H), 0)
|
|
sp = st.load()
|
|
for y in range(H):
|
|
for x in range(W):
|
|
vals = [px[x, y] for px in pxs]
|
|
m = sum(vals) / n
|
|
var = sum((v - m) ** 2 for v in vals) / n
|
|
sp[x, y] = 255 if var ** 0.5 < 4 else 0
|
|
# candidate = static AND edge
|
|
cand = Image.new("L", (W, H), 0)
|
|
cp = cand.load()
|
|
for y in range(H):
|
|
for x in range(W):
|
|
cp[x, y] = ep[x, y] if sp[x, y] else 0
|
|
cand.save(r"C:\Users\29452\AppData\Local\Temp\opencode\staticcand.png")
|
|
|
|
# report bounding regions: coarse grid of activity
|
|
gw, gh = 40, 24
|
|
cellw, cellh = W // gw, H // gh
|
|
print("grid activity (static-edge):")
|
|
for gy in range(gh):
|
|
line = ""
|
|
for gx in range(gw):
|
|
s = 0
|
|
for y in range(gy * cellh, min(H, (gy + 1) * cellh), 3):
|
|
for x in range(gx * cellw, min(W, (gx + 1) * cellw), 3):
|
|
s += cp[x, y]
|
|
line += " .:-=+*#%@"[min(9, s // 300)]
|
|
print(line)
|