GithubHelp home page GithubHelp logo

Comments (7)

imagesc-bot avatar imagesc-bot commented on May 28, 2024

This issue has been mentioned on Image.sc Forum. There might be relevant details there:

https://forum.image.sc/t/wiener-deconvolution-implementation-handling-complex-psf-in-scikit-image/94170/4

from scikit-image.

lagru avatar lagru commented on May 28, 2024

[...] the implementation may discard the imaginary part of a complex PSF, focusing only on the real component (psf = psf.real.astype(float_type, copy=False)). This approach could potentially omit critical phase information inherent in the imaginary part, which might be essential for accurate deconvolution. [...]

As stated in my comment, I’m not really familiar with that function but after some Git sleuthing it looks like it might have been an oversight that was introduced in single precision support in skimage.restoration module #5219. I haven’t checked the PR and context in detail though.

It seems like instead of always casting the real part to float_type it should check if reg and psf are complex and then use the corresponding complex type instead. E.g. float32 β†’ complex64…

Code snippet for investigation

Not sure if this actually makes sense in practice actually!

import skimage as ski
import scipy as sp
import numpy as np

img = ski.color.rgb2gray(ski.data.astronaut())
psf = np.ones((5, 5)) / 25
img = sp.signal.convolve2d(img, psf, 'same')
rng = np.random.default_rng()
img += 0.1 * img.std() * rng.standard_normal(img.shape)

psf = psf * (1+1j)

deconvolved_img = ski.restoration.wiener(img, psf, 0.1)

from scikit-image.

lagru avatar lagru commented on May 28, 2024

Optimistically pinging @jni, @grlee77 and @FirefoxMetzger in the hope they have a deeper understanding of this and some quick insights into the potential bug. πŸ™

from scikit-image.

imagesc-bot avatar imagesc-bot commented on May 28, 2024

This issue has been mentioned on Image.sc Forum. There might be relevant details there:

https://forum.image.sc/t/clarification-needed-shape-of-transfer-function-in-restoration-wiener-with-is-real-parameter/94173/2

from scikit-image.

nombac avatar nombac commented on May 28, 2024

It appears that the issue can be addressed by making the following two modifications:

  1. Comment out the following two lines:

    # psf = psf.real.astype(float_type, copy=False)
    # reg = reg.real.astype(float_type, copy=False)
  2. Modify this line:

    if is_real:
        deconv = uft.uirfftn(wiener_filter * uft.urfftn(image), shape=image.shape)  
    else:
        deconv = uft.uifftn(wiener_filter * uft.ufftn(image))

    to:

    if not is_real:
        deconv = np.real(uft.uifftn(wiener_filter * uft.ufftn(image)))
    else:
        deconv = uft.uirfftn(wiener_filter * uft.urfftn(image), shape=image.shape)

The second modification includes logical clarification.

from scikit-image.

nombac avatar nombac commented on May 28, 2024

I've identified another potential bug. When the "psf" (Point Spread Function) is provided with the same shape as the "image", it's directly passed to "trans_func" without being transformed by "ir2tf":

if psf.shape != reg.shape:
    trans_func = uft.ir2tf(psf, image.shape, is_real=is_real)
else:
    trans_func = psf

To ensure that the "psf" is always correctly transformed, regardless of its shape, I suggest modifying the condition to check if the "psf" is a complex object. If it's not, it should be transformed using "ir2tf". Here's the proposed change:

if not np.iscomplexobj(psf):
    trans_func = uft.ir2tf(psf, image.shape, is_real=is_real)
else:
    trans_func = psf

This change ensures that the "psf" is always in the correct format for processing, whether it initially matches the "image" shape or not.

from scikit-image.

nombac avatar nombac commented on May 28, 2024

Below is the "git diff" highlighting the minimal changes needed. From my testing, these modifications work well for my specific use cases.

--- a/skimage/restoration/deconvolution.py
+++ b/skimage/restoration/deconvolution.py
@@ -116,10 +116,8 @@ def wiener(image, psf, balance, reg=None, is_real=True, clip=True):
         reg = uft.ir2tf(reg, image.shape, is_real=is_real)
     float_type = _supported_float_type(image.dtype)
     image = image.astype(float_type, copy=False)
-    psf = psf.real.astype(float_type, copy=False)
-    reg = reg.real.astype(float_type, copy=False)
 
-    if psf.shape != reg.shape:
+    if not np.iscomplexobj(psf):
         trans_func = uft.ir2tf(psf, image.shape, is_real=is_real)
     else:
         trans_func = psf
@@ -130,7 +128,7 @@ def wiener(image, psf, balance, reg=None, is_real=True, clip=True):
         deconv = uft.uirfftn(wiener_filter * uft.urfftn(image),
                              shape=image.shape)
     else:
-        deconv = uft.uifftn(wiener_filter * uft.ufftn(image))
+        deconv = np.real(uft.uifftn(wiener_filter * uft.ufftn(image)))
 
     if clip:
         deconv[deconv > 1] = 1

from scikit-image.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.