I take the dwt2 for an image and saved it's coefficients (LL,Lh, hl,hh) using pywt.dwt2 (image,'haar'),in my project I have to change them to uint8 but when I change their types and reconstruct the image an error appear prevent the show of the result image .can any body help me with this problem! My code is like:

import cv2
import numpy as np
import pywt
  • $\begingroup$ 1) The question does not contain any information regarding the error (?) 2) Your coefficients are in a different scale (and way of representation) between each other. If you convert 0.5 to an integer, it will be truncated to 0. So there are two things here, correcting the error and saving the numbers in a meaningful way. $\endgroup$
    – A_A
    Oct 26 '18 at 8:44
  • $\begingroup$ 1 )the message error is: AttributeError: 'numpy.ndarray' object has no attribute 'imshow' $\endgroup$
    – Ann basem
    Oct 26 '18 at 9:14
  • $\begingroup$ Thank you. In that case, the question is definitely off topic for DSP.SE. The error comes from the fact that you are trying to use a matplotlib function but instead try to invoke it from the ndarray object of the numpy package. But even when you solve that error, if you don't properly scale the image values you will still be looking at the wrong image. Instead of converting it yourself, pass it to imshow directly and possibly supply vmin, vmax too. $\endgroup$
    – A_A
    Oct 26 '18 at 9:20
  • $\begingroup$ 2)I applied the same code in matlab and it runs with out error ,of courcourse when I let the LL without change the result is better ,do you have another way to handle the coefficients 8 bit pixels and reconstruct the image again? $\endgroup$
    – Ann basem
    Oct 26 '18 at 9:20
  • $\begingroup$ Thank you for your answer, I am new in python I am trying to convert the matlab steganography code to python so I want to change the dwt pixels 8 bit values and reconstruct it again can you typing the corrected code for this please $\endgroup$
    – Ann basem
    Oct 26 '18 at 9:32

Three aspects should be taken into account. Suppose that the image is initially positive, coded on eight bits. Then:

  • standard wavelet oefficients become real, and are not converted to uint8 easily, because wavelet filter coefficients are often real (and not dyadic rationals)
  • since the filters are orthogonal, the coefficients risk to grow, and exceed the initial $[0,\ldots,255]$ range. So a mere rounding is dangerous
  • even if you use integer wavelets (by lifting), the same happen, and in 2D you may need $1$ more bit for each level
  • some detail coefficients will be negative, so uint8 will destroy the signs in a way you cannot recover afterward.

So, the inverse wavelet tranform is likely to be crappy. THe simplest way around:

  • divide all coeffciients by their maximal absolute value (and keep track of it),
  • code all the $[0,1]$ magnitudes on $7$ bit,
  • code the sign on the $8$th bit.

Not the answer you're looking for? Browse other questions tagged or ask your own question.