PyWavelets / pywt

PyWavelets - Wavelet Transforms in Python
http://pywavelets.readthedocs.org
MIT License
1.97k stars 460 forks source link

How to Apply CLAHE and Bayesian Thresholding to the Coefficients for Reconstruction? #630

Open sarmientoj24 opened 2 years ago

sarmientoj24 commented 2 years ago

I am trying to follow this image preprocessing block using pywt but I cannot do CLAHE on the coefficients such as LL, and thresholding on the other three. JMSS-5-59-g001

import pywt
import pywt.data

# Wavelet transform of image, and plot approximation and details
titles = ['Approximation', ' Horizontal detail',
          'Vertical detail', 'Diagonal detail']
coeffs2 = pywt.dwt2(original, 'bior1.3')
LL, (LH, HL, HH) = coeffs2
fig = plt.figure(figsize=(12, 3))
for i, a in enumerate([LL, LH, HL, HH]):
    ax = fig.add_subplot(1, 4, i + 1)
    ax.imshow(a, interpolation="nearest", cmap=plt.cm.gray)
    ax.set_title(titles[i], fontsize=10)
    ax.set_xticks([])
    ax.set_yticks([])

fig.tight_layout()
plt.show()

How do you apply CLAHE for LL and thresholding for LH, HL, HH and reconstruct the wavelets?

grlee77 commented 2 years ago

Hi @sarmientoj24, here are some ideas:

For CLAHE, I would install scikit-image and then use skimage.exposure.equalize_adapthist.

For BayesShrink there is an implementation in skimage.restoration.denoise_wavelet that is based on PyWavelets. The denoise_wavelet source code is likely a good starting point for your implementation. It currently is nD as oppopsed to 2D specific, so you will see pywt.wavedecn and pywt.waverecn used instead of their 2D counterparts.

The default for that function is method='BayesShrink', however it does NOT apply CLAHE to the approximation coefficients (it thresholds the detail coefficients, but also does not apply whatever the "linear enhancement" in the diagram above is). By default it uses a multilevel decomposition (i.e. using pywt.wavedecn instead of pywt.dwt2), but you could set wavelet_levels=1 to force this to be only a single level as in the diagram above.

sarmientoj24 commented 2 years ago

Thank you @grlee77 Yeah I can do CLAHE actually. But then, when I reconstruct it, it's just some garbage image that is remaining. I guess it's because the values are now changed.

My question is more of a pseudocode or instruction how to do the pywt deconstruction -> clahe, then reconstruction.