WIP: Context-Adaptive Interpolator (CAI)

This commit is contained in:
Benjamin Loison 2024-03-21 10:04:05 +01:00
parent ad961b75fc
commit 668057e261
Signed by: Benjamin_Loison
SSH Key Fingerprint: SHA256:BtnEgYTlHdOg1u+RmYcDE0mnfz1rhv5dSbQ2gyxW8B8
2 changed files with 53 additions and 23 deletions

View File

@ -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()

View File

@ -699,22 +699,19 @@ and considered here?</text>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">234.01365124 56.90404704 298.47858791 56.90404704</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">48.39965160 68.06617103 292.04445111 68.06617103</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">48.80500548 78.49465807 63.32163914 79.14757222</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">94.00552071 80.22668418 213.89141267 80.22668418</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">240.42370680 80.50878333 294.98619459 80.50878333</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">49.91912143 91.68787530 109.00987130 91.68787530</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">124.66186592 101.93477910 124.66186592 117.71048868 234.42102061 117.71048868 234.42102061 101.93477910 124.66186592 101.93477910</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">94.00552071 80.22668418 213.89141267 80.22668418</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">240.42370680 80.50878333 294.98619459 80.50878333</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">49.91912143 91.68787530 109.00987130 91.68787530</stroke>
<stroke tool="highlighter" color="#ff00007f" width="8.50000000" fill="128">124.66186592 101.93477910 124.66186592 117.71048868 234.42102061 117.71048868 234.42102061 101.93477910 124.66186592 101.93477910</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">68.47389354 129.24359993 187.07496247 129.24359993</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">200.54845048 129.63017707 204.79511423 126.67785163</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">239.35741128 127.06747783 270.20461967 127.06747783</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">47.90201170 138.03103337 65.97986726 139.76853746</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">105.20931531 138.45301071 162.92163797 138.45301071</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">181.47051328 139.65138087 238.59035193 139.65138087</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">47.45419049 151.33026173 73.89024898 151.33026173</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">142.72968280 151.18164977 297.64924626 151.18164977</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">48.52801825 161.95356187 265.31657741 161.95356187</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">117.99112359 174.92225050 291.80041063 174.92225050</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">47.90409179 185.51778172 106.48776344 185.51778172</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">135.37302059 186.22120361 289.07569919 186.22120361</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">200.54845048 129.63017707 204.79511423 126.67785163</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">239.35741128 127.06747783 270.20461967 127.06747783</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">47.90201170 138.03103337 65.97986726 139.76853746</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">105.20931531 138.45301071 162.92163797 138.45301071</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">181.47051328 139.65138087 238.59035193 139.65138087</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">47.45419049 151.33026173 73.89024898 151.33026173</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">117.99112359 174.92225050 291.80041063 174.92225050</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">47.90409179 185.51778172 106.48776344 185.51778172</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">79.84740241 197.12191732 180.26366418 197.12191732</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">91.25706084 205.78669895 91.25706084 233.33486307 257.24161221 233.33486307 257.24161221 205.78669895 91.25706084 205.78669895</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">74.52609696 243.52963985 94.17824960 241.75708008</stroke>
@ -831,11 +828,11 @@ that way keep edge but smooth otherwise</text>
what happens if apply many times the filter? Get a single color image or only edges?</text>
<text font="Sans" size="11.00000000" x="105.08740424" y="713.39660615" color="#00c0ffff" ts="0" fn="">?</text>
<text font="Sans" size="11.00000000" x="47.17761239" y="172.22262609" color="#00c0ffff" ts="0" fn="">Independent and identically distributed random variables</text>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">360.62404437 56.50215566 412.62421835 56.50215566</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">503.93888999 56.74246362 563.85102495 56.74246362</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">314.16312679 67.89352828 377.15982828 67.89352828</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">326.73579824 79.29706273 566.05358802 79.29706273</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">314.14872554 92.91554753 396.99601563 92.91554753</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">360.62404437 56.50215566 412.62421835 56.50215566</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">503.93888999 56.74246362 563.85102495 56.74246362</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">314.16312679 67.89352828 377.15982828 67.89352828</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">326.73579824 79.29706273 566.05358802 79.29706273</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">314.14872554 92.91554753 396.99601563 92.91554753</stroke>
<stroke tool="pen" ts="0" fn="" color="#00c0ffff" width="2.26000000" fill="255">385.42358496 72.64296108 503.46352239 72.64296108</stroke>
<text font="Sans" size="11.00000000" x="359.07074194" y="37.69319842" color="#00c0ffff" ts="0" fn="">why this value and what does it mean?</text>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">331.12401113 122.99908986 558.38174858 122.99908986</stroke>
@ -953,6 +950,12 @@ values, is it?</text>
<text font="Sans" size="11.00000000" x="50.19647041" y="370.38735939" color="#00c0ffff" ts="0" fn="">2</text>
<text font="Sans" size="11.00000000" x="411.02119390" y="102.60461263" color="#00c0ffff" ts="0" fn="">3</text>
<text font="Sans" size="11.00000000" x="468.67129924" y="568.50241754" color="#00c0ffff" ts="0" fn="">4</text>
<text font="Sans" size="11.00000000" x="256.64285278" y="715.37469482" color="#00c0ffff" ts="0" fn="">?</text>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">135.37302059 186.22120361 289.07569919 186.22120361</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">141.05700684 150.30480957 234.35293524 150.30480957</stroke>
<stroke tool="highlighter" color="#00c0ff7f" width="8.50000000" fill="128">104.65768433 161.09030151 263.06051594 161.09030151</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">240.77893066 150.39559937 297.05985950 150.39559937</stroke>
<stroke tool="highlighter" color="#ffff007f" width="8.50000000" fill="128">50.38613892 162.83441162 91.31531997 162.83441162</stroke>
</layer>
</page>
<page width="612.00000000" height="792.00000000">