53 lines
1.7 KiB
Python
53 lines
1.7 KiB
Python
# Based on https://web.archive.org/web/20231116015653/http://nrl.northumbria.ac.uk/id/eprint/29339/1/Paper_accepted.pdf
|
|
|
|
from PIL import Image
|
|
from statistics import mean, median
|
|
|
|
# What about other color channels?
|
|
image = Image.open('9f04e2005fddb9d5512e2f42a3b826b019755717.jpg').convert('L')
|
|
r = image.load()
|
|
I = image.copy().load()
|
|
DEFAULT_COLOR = 255
|
|
# This threshold is debatable.
|
|
THRESHOLD = 20
|
|
# How to manage the border of the image?
|
|
# Equation (10)
|
|
for m in range(1, image.size[0] - 1):
|
|
for n in range(1, image.size[1] - 1):
|
|
e = I[m, n + 1]
|
|
se = I[m + 1, n + 1]
|
|
s = I[m + 1, n]
|
|
sw = I[m + 1, n - 1]
|
|
w = I[m, n - 1]
|
|
nw = I[m - 1, n - 1]
|
|
no = I[m - 1, n]
|
|
ne = I[m - 1, n + 1]
|
|
A = [e, se, s, sw, w, nw, no, ne]
|
|
if max(A) - min(A) <= THRESHOLD:
|
|
newPixel = I[m, n] - mean(A)
|
|
elif abs(e - w) - abs(no - s) > THRESHOLD:
|
|
newPixel = I[m, n] - (s + no) / 2
|
|
elif abs(s - no) - abs(e - w) > THRESHOLD:
|
|
newPixel = I[m, n] - (e + w) / 2
|
|
elif abs(sw - ne) - abs(se - nw) > THRESHOLD:
|
|
newPixel = I[m, n] - (se + nw) / 2
|
|
elif abs(se - nw) - abs(sw - ne) > THRESHOLD:
|
|
newPixel = I[m, n] - (sw + ne) / 2
|
|
else:
|
|
newPixel = I[m, n] - median(A)
|
|
r[m, n] = round(newPixel)
|
|
|
|
Q = 3
|
|
|
|
def wienerFilter():
|
|
# Equation (7)
|
|
hw[i, j] = h[i, j] * sigma(i, j) / (sigma(i, j) + sigma_0 ** 2)
|
|
|
|
def sigma(i, j):
|
|
# Equation (9)
|
|
return sigma_q(i, j, Q)
|
|
|
|
def sigma_q(i, j, q):
|
|
# Equation
|
|
|
|
image.rotate(-90).show() |