WIP: Context-Adaptive Interpolator (CAI)
This commit is contained in:
@@ -1,14 +1,17 @@
|
||||
# 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
|
||||
from statistics import mean, median
|
||||
|
||||
# What about other color channels?
|
||||
image = Image.open('9f04e2005fddb9d5512e2f42a3b826b019755717.jpg').convert('L')
|
||||
newI = image.load()
|
||||
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]
|
||||
@@ -21,6 +24,30 @@ for m in range(1, image.size[0] - 1):
|
||||
ne = I[m - 1, n + 1]
|
||||
A = [e, se, s, sw, w, nw, no, ne]
|
||||
if max(A) - min(A) <= THRESHOLD:
|
||||
newI[m, n] = int(round(I[m, n] - mean(A), 0))
|
||||
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()
|
Reference in New Issue
Block a user