Repository navigation
Expand file tree
/
Copy pathfrequency.py
More file actions
56 lines (42 loc) · 1.54 KB
/
Copy pathfrequency.py
File metadata and controls
56 lines (42 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import numpy as np
import cv2
def compute_fft_score(image):
"""
Computes high-frequency artifact score using FFT.
Standardized to fixed resolution for consistency.
"""
# -----------------------------
# 1. Force Standard Resolution
# -----------------------------
image = image.resize((512, 512))
# Convert to grayscale
img = np.array(image.convert("L"), dtype=np.float32)
# -----------------------------
# 2. Compute FFT
# -----------------------------
f = np.fft.fft2(img)
fshift = np.fft.fftshift(f)
magnitude = np.abs(fshift)
# Log scaling (stable)
magnitude_spectrum = np.log1p(magnitude)
# -----------------------------
# 3. High Frequency Ring Mask
# -----------------------------
h, w = magnitude_spectrum.shape
center_h, center_w = h // 2, w // 2
y, x = np.ogrid[:h, :w]
distance = np.sqrt((x - center_w) ** 2 + (y - center_h) ** 2)
# Define ring: ignore low frequencies (center),
# capture outer high-frequency band
low_radius = 40
high_radius = 200
ring_mask = (distance > low_radius) & (distance < high_radius)
high_freq_energy = np.mean(magnitude_spectrum[ring_mask])
total_energy = np.mean(magnitude_spectrum)
# -----------------------------
# 4. Normalized Score
# -----------------------------
score = high_freq_energy / (total_energy + 1e-8)
# Soft normalization
normalized_score = float(min(score / 3.0, 1.0))
return normalized_score