Wiener filter #10

Closed
opened 2024-03-20 12:00:33 +01:00 by Benjamin_Loison · 3 comments

https://web.archive.org/web/20240320105334/https://web.stanford.edu/class/ee368/Handouts/Lectures/2014_Spring/8-Linear-Image-Processing/Wiener_Filtering.pdf#page=5

pdfimages Wiener_Filtering.pdf -png -f 5 -l 5 Wiener_Filtering

Unclear how to get Wiener_Filtering-006.png from Wiener_Filtering-001.png.

from scipy.signal import wiener
import matplotlib.pyplot as plt
import numpy as np
from PIL import Image

img = Image.open('Wiener_Filtering-001.png').convert('L')
filtered_img = wiener(img, (5, 5))
f, (plot1, plot2) = plt.subplots(1, 2)
plot1.imshow(img)
plot2.imshow(filtered_img)
plt.show()

Based on https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.wiener.html

Let us try to reproduce:

https://web.archive.org/web/20240320105334/https://web.stanford.edu/class/ee368/Handouts/Lectures/2014_Spring/8-Linear-Image-Processing/Wiener_Filtering.pdf#page=7

pdfimages Wiener_Filtering.pdf -png -f 7 -l 7 Wiener_Filtering

Wiener_Filtering-002.png is the original one, Wiener_Filtering-001.png the noise one and Wiener_Filtering-000.png the treated one.

Similar results as above:

from scipy.signal import wiener
import matplotlib.pyplot as plt
from PIL import Image
import warnings

img = Image.open('Wiener_Filtering-001.png').convert('L')
figure, plots = plt.subplots(1, 5)
figure.suptitle('Comparison of multiple Wiener filter size')
plot = plots[0]
plot.imshow(img)
plot.set_title('Original')
for i in range(1, 5):
    warnings.filterwarnings('error')
    try:
        filtered_img = wiener(img, i)
        hasError = False
    except:
        warnings.resetwarnings()
        filtered_img = wiener(img, i)
        hasError = True
    plot = plots[i]
    plot.set_title(f'wiener(img, {i})\n({hasError=})')
    plot.imshow(filtered_img)
plt.tight_layout()
plt.show()

The error is:

/home/benjamin/.local/lib/python3.10/site-packages/scipy/signal/_signaltools.py:1659: RuntimeWarning: divide by zero encountered in divide
  res *= (1 - noise / lVar)
/home/benjamin/.local/lib/python3.10/site-packages/scipy/signal/_signaltools.py:1659: RuntimeWarning: invalid value encountered in multiply
  res *= (1 - noise / lVar)

While https://stackoverflow.com/a/41020626 looks interesting:

from skimage import color, data, restoration
from scipy.signal import convolve2d

img = color.rgb2gray(data.astronaut())
psf = np.ones((5, 5)) / 25
img = convolve2d(img, psf, 'same')
img += 0.1 * img.std() * np.random.standard_normal(img.shape)
deconvolved_img = restoration.wiener(img, psf, 1100)
f, (plot1, plot2) = plt.subplots(1, 2)
plot1.imshow(img)
plot2.imshow(deconvolved_img)
plt.show()

with my image it does not seem to be interesting.

from skimage import color, data, restoration
from scipy.signal import convolve2d
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt

img = Image.open('Wiener_Filtering-001.png').convert('L')
img = np.array(img)
psf = np.ones((5, 5)) / 25
deconvolved_img = restoration.wiener(img, psf, 1100)

f, (plot1, plot2) = plt.subplots(1, 2)
plot1.imshow(img)
plot2.imshow(deconvolved_img)
plt.show()

I am able to correctly compute RMS thanks to the Stack Overflow answer 11818358.

from PIL import Image, ImageChops
import math
import operator
import functools

def openImage(filePath):
    return Image.open(filePath).convert('L')

im1 = openImage('Wiener_Filtering-001.png')
im2 = openImage('Wiener_Filtering-002.png')

def rmsdiff(im1, im2):
    "Calculate the root-mean-square difference between two images"

    h = ImageChops.difference(im1, im2).histogram()

    # calculate rms
    return math.sqrt(functools.reduce(operator.add,
        map(lambda h, i: h*(i**2), h, range(256))
    ) / (float(im1.size[0]) * im1.size[1]))

print(rmsdiff(im1, im2))

Check initial CAI paper for details, also see the end of section IV. C. for details.

https://web.stanford.edu/class/ee368/Handouts/Lectures/Examples/8-Linear-Image-Processing/Wiener_Filtering/

It seems that thanks to #5 can deduce if denoised.

https://ipolcore.ipol.im/api/demoinfo/staticData/demoExtras/77777000278/

https://web.archive.org/web/20240320105334/https://web.stanford.edu/class/ee368/Handouts/Lectures/2014_Spring/8-Linear-Image-Processing/Wiener_Filtering.pdf#page=5 ```bash pdfimages Wiener_Filtering.pdf -png -f 5 -l 5 Wiener_Filtering ``` Unclear how to get `Wiener_Filtering-006.png` from `Wiener_Filtering-001.png`. ```py from scipy.signal import wiener import matplotlib.pyplot as plt import numpy as np from PIL import Image img = Image.open('Wiener_Filtering-001.png').convert('L') filtered_img = wiener(img, (5, 5)) f, (plot1, plot2) = plt.subplots(1, 2) plot1.imshow(img) plot2.imshow(filtered_img) plt.show() ``` Based on https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.wiener.html Let us try to reproduce: https://web.archive.org/web/20240320105334/https://web.stanford.edu/class/ee368/Handouts/Lectures/2014_Spring/8-Linear-Image-Processing/Wiener_Filtering.pdf#page=7 ```bash pdfimages Wiener_Filtering.pdf -png -f 7 -l 7 Wiener_Filtering ``` `Wiener_Filtering-002.png` is the original one, `Wiener_Filtering-001.png` the noise one and `Wiener_Filtering-000.png` the treated one. Similar results as above: ```py from scipy.signal import wiener import matplotlib.pyplot as plt from PIL import Image import warnings img = Image.open('Wiener_Filtering-001.png').convert('L') figure, plots = plt.subplots(1, 5) figure.suptitle('Comparison of multiple Wiener filter size') plot = plots[0] plot.imshow(img) plot.set_title('Original') for i in range(1, 5): warnings.filterwarnings('error') try: filtered_img = wiener(img, i) hasError = False except: warnings.resetwarnings() filtered_img = wiener(img, i) hasError = True plot = plots[i] plot.set_title(f'wiener(img, {i})\n({hasError=})') plot.imshow(filtered_img) plt.tight_layout() plt.show() ``` The error is: ``` /home/benjamin/.local/lib/python3.10/site-packages/scipy/signal/_signaltools.py:1659: RuntimeWarning: divide by zero encountered in divide res *= (1 - noise / lVar) /home/benjamin/.local/lib/python3.10/site-packages/scipy/signal/_signaltools.py:1659: RuntimeWarning: invalid value encountered in multiply res *= (1 - noise / lVar) ``` While https://stackoverflow.com/a/41020626 looks interesting: ```py from skimage import color, data, restoration from scipy.signal import convolve2d img = color.rgb2gray(data.astronaut()) psf = np.ones((5, 5)) / 25 img = convolve2d(img, psf, 'same') img += 0.1 * img.std() * np.random.standard_normal(img.shape) deconvolved_img = restoration.wiener(img, psf, 1100) f, (plot1, plot2) = plt.subplots(1, 2) plot1.imshow(img) plot2.imshow(deconvolved_img) plt.show() ``` with my image it does not seem to be interesting. ```py from skimage import color, data, restoration from scipy.signal import convolve2d from PIL import Image import numpy as np import matplotlib.pyplot as plt img = Image.open('Wiener_Filtering-001.png').convert('L') img = np.array(img) psf = np.ones((5, 5)) / 25 deconvolved_img = restoration.wiener(img, psf, 1100) f, (plot1, plot2) = plt.subplots(1, 2) plot1.imshow(img) plot2.imshow(deconvolved_img) plt.show() ``` I am able to correctly compute RMS thanks to [the Stack Overflow answer 11818358](https://stackoverflow.com/a/11818358). ```py from PIL import Image, ImageChops import math import operator import functools def openImage(filePath): return Image.open(filePath).convert('L') im1 = openImage('Wiener_Filtering-001.png') im2 = openImage('Wiener_Filtering-002.png') def rmsdiff(im1, im2): "Calculate the root-mean-square difference between two images" h = ImageChops.difference(im1, im2).histogram() # calculate rms return math.sqrt(functools.reduce(operator.add, map(lambda h, i: h*(i**2), h, range(256)) ) / (float(im1.size[0]) * im1.size[1])) print(rmsdiff(im1, im2)) ``` Check initial CAI paper for details, also see the end of section IV. C. for details. https://web.stanford.edu/class/ee368/Handouts/Lectures/Examples/8-Linear-Image-Processing/Wiener_Filtering/ It seems that thanks to #5 can deduce if denoised. https://ipolcore.ipol.im/api/demoinfo/staticData/demoExtras/77777000278/
Benjamin_Loison added the
Context-Adaptive Interpolator
label 2024-03-21 10:08:01 +01:00
Author
Owner

With Brightness 127 and Contrast 100, get:

Before Wiener filter:

image

After Wiener filter:

image

Taking an example image region:

Before Wiener filter:

image

After Wiener filter:

image

The image looks simpler, hence is possibly denoised.

Should try applying on an image where I added Gaussian noise and measure RMS.

With `Brightness` `127` and `Contrast` `100`, get: Before Wiener filter: ![image](/attachments/8602725c-b247-4755-976d-0135478248ea) After Wiener filter: ![image](/attachments/1ad5f8fd-ba4c-4baf-82c1-befa1c3e93fc) Taking an example image region: Before Wiener filter: ![image](/attachments/0fde8051-a9ea-4550-90f8-163e3acc1ca0) After Wiener filter: ![image](/attachments/d0013061-ab96-4509-b4e2-a9db5c3760fc) The image looks simpler, hence is possibly denoised. Should try applying on an image where I added Gaussian noise and measure RMS.
Author
Owner
from PIL import Image, ImageChops
import math
import operator
import functools
import matplotlib.pyplot as plt
import numpy

def openImage(filePath):
    return Image.open(filePath).convert('L')

im1 = openImage('Wiener_Filtering-001.png')
im1Pixels = im1.load()
im2 = openImage('Wiener_Filtering-002.png')

def rmsdiff(im1, im2):
    "Calculate the root-mean-square difference between two images"

    h = ImageChops.difference(im1, im2).histogram()

    # calculate rms
    return math.sqrt(functools.reduce(operator.add,
        map(lambda h, i: h*(i**2), h, range(256))
    ) / (float(im1.size[0]) * im1.size[1]))

print(rmsdiff(im1, im2))

Y = []
for sigma_0 in numpy.arange(1, 20, 1):
    h_wImage = Image.new(MODE, (im1.size[0], im1.size[1]))
    h_wImagePixels = h_wImage.load()

    for i in tqdm(range(im1.size[0])):
        for j in range(im1.size[1]):
            h_wImagePixels[i, j] = round(h_w(im1, im1Pixels, i, j))

    rmsdiffValue = rmsdiff(h_wImage, im2)
    Y += [rmsdiffValue]

plt.plot(Y)
plt.show()

Figure_1

Unable to reduce RMS results of denoised https://web.archive.org/web/20240320105334/https://web.stanford.edu/class/ee368/Handouts/Lectures/2014_Spring/8-Linear-Image-Processing/Wiener_Filtering.pdf#page=7 maybe should try with other Q values but it would need adapting the code not to have out of bound errors.

```py from PIL import Image, ImageChops import math import operator import functools import matplotlib.pyplot as plt import numpy def openImage(filePath): return Image.open(filePath).convert('L') im1 = openImage('Wiener_Filtering-001.png') im1Pixels = im1.load() im2 = openImage('Wiener_Filtering-002.png') def rmsdiff(im1, im2): "Calculate the root-mean-square difference between two images" h = ImageChops.difference(im1, im2).histogram() # calculate rms return math.sqrt(functools.reduce(operator.add, map(lambda h, i: h*(i**2), h, range(256)) ) / (float(im1.size[0]) * im1.size[1])) print(rmsdiff(im1, im2)) Y = [] for sigma_0 in numpy.arange(1, 20, 1): h_wImage = Image.new(MODE, (im1.size[0], im1.size[1])) h_wImagePixels = h_wImage.load() for i in tqdm(range(im1.size[0])): for j in range(im1.size[1]): h_wImagePixels[i, j] = round(h_w(im1, im1Pixels, i, j)) rmsdiffValue = rmsdiff(h_wImage, im2) Y += [rmsdiffValue] plt.plot(Y) plt.show() ``` ![Figure_1](/attachments/4d748906-f2a3-4be8-ae70-a4846bc8e9f3) Unable to reduce RMS results of denoised https://web.archive.org/web/20240320105334/https://web.stanford.edu/class/ee368/Handouts/Lectures/2014_Spring/8-Linear-Image-Processing/Wiener_Filtering.pdf#page=7 maybe should try with other `Q` values but it would need adapting the code not to have out of bound errors.
Author
Owner
Related to [Benjamin_Loison/PRNU_extraction/issues/1](https://codeberg.org/Benjamin_Loison/PRNU_extraction/issues/1).
Sign in to join this conversation.
No description provided.