Upload image_processing.py
Browse files
processing/image_processing.py
CHANGED
|
@@ -94,7 +94,7 @@ def error_level_analysis(img, quality=90):
|
|
| 94 |
return Image.fromarray(diff)
|
| 95 |
|
| 96 |
def wavelet_decomposition(img):
|
| 97 |
-
"""Decomposes image into wavelet subbands
|
| 98 |
if not PYWT_AVAILABLE:
|
| 99 |
# Fallback: return grayscale
|
| 100 |
arr = np.array(img)
|
|
@@ -116,18 +116,34 @@ def wavelet_decomposition(img):
|
|
| 116 |
coeffs = pywt.dwt2(gray, 'haar')
|
| 117 |
LL, (LH, HL, HH) = coeffs
|
| 118 |
|
| 119 |
-
#
|
| 120 |
-
def
|
| 121 |
-
|
| 122 |
if np.max(band) > 0:
|
| 123 |
band = (band / np.max(band)) * 255.0
|
| 124 |
return np.clip(band, 0, 255).astype(np.uint8)
|
| 125 |
return np.zeros_like(band, dtype=np.uint8)
|
| 126 |
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 131 |
|
| 132 |
# Combine into single image (2x2 grid)
|
| 133 |
top = np.hstack([LL_norm, LH_norm])
|
|
|
|
| 94 |
return Image.fromarray(diff)
|
| 95 |
|
| 96 |
def wavelet_decomposition(img):
|
| 97 |
+
"""Decomposes image into wavelet subbands with proper visualization"""
|
| 98 |
if not PYWT_AVAILABLE:
|
| 99 |
# Fallback: return grayscale
|
| 100 |
arr = np.array(img)
|
|
|
|
| 116 |
coeffs = pywt.dwt2(gray, 'haar')
|
| 117 |
LL, (LH, HL, HH) = coeffs
|
| 118 |
|
| 119 |
+
# Different normalization for LL vs high-frequency bands
|
| 120 |
+
def normalize_lowfreq(band):
|
| 121 |
+
"""Standard normalization for LL (approximation)"""
|
| 122 |
if np.max(band) > 0:
|
| 123 |
band = (band / np.max(band)) * 255.0
|
| 124 |
return np.clip(band, 0, 255).astype(np.uint8)
|
| 125 |
return np.zeros_like(band, dtype=np.uint8)
|
| 126 |
|
| 127 |
+
def normalize_highfreq(band, amplification=30):
|
| 128 |
+
"""Amplified normalization for high-frequency details"""
|
| 129 |
+
band = np.abs(band)
|
| 130 |
+
|
| 131 |
+
# Amplify before normalization
|
| 132 |
+
band = band * amplification
|
| 133 |
+
|
| 134 |
+
# Clip to prevent overflow
|
| 135 |
+
band = np.clip(band, 0, 255)
|
| 136 |
+
|
| 137 |
+
# Normalize to full range
|
| 138 |
+
if np.max(band) > 0:
|
| 139 |
+
band = (band / np.max(band)) * 255.0
|
| 140 |
+
return band.astype(np.uint8)
|
| 141 |
+
return np.zeros_like(band, dtype=np.uint8)
|
| 142 |
+
|
| 143 |
+
LL_norm = normalize_lowfreq(LL)
|
| 144 |
+
LH_norm = normalize_highfreq(LH) # Horizontal edges
|
| 145 |
+
HL_norm = normalize_highfreq(HL) # Vertical edges
|
| 146 |
+
HH_norm = normalize_highfreq(HH) # Diagonal edges/noise
|
| 147 |
|
| 148 |
# Combine into single image (2x2 grid)
|
| 149 |
top = np.hstack([LL_norm, LH_norm])
|