2D Forward and Inverse Discrete Wavelet Transform¶
Single level dwt2
¶
-
pywt.
dwt2
(data, wavelet, mode='symmetric', axes=(-2, -1))¶ 2D Discrete Wavelet Transform.
Parameters: data : array_like
2D array with input data
wavelet : Wavelet object or name string
Wavelet to use
mode : str, optional
Signal extension mode, see Modes (default: ‘symmetric’)
axes : 2-tuple of ints, optional
Axes over which to compute the DWT. Repeated elements mean the DWT will be performed multiple times along these axes.
Returns: (cA, (cH, cV, cD)) : tuple
Approximation, horizontal detail, vertical detail and diagonal detail coefficients respectively. Horizontal refers to array axis 0 (or
axes[0]
for user-specifiedaxes
).Examples
>>> import numpy as np >>> import pywt >>> data = np.ones((4,4), dtype=np.float64) >>> coeffs = pywt.dwt2(data, 'haar') >>> cA, (cH, cV, cD) = coeffs >>> cA array([[ 2., 2.], [ 2., 2.]]) >>> cV array([[ 0., 0.], [ 0., 0.]])
The relation to the other common data layout where all the approximation and details coefficients are stored in one big 2D array is as follows:
------------------- | | | | cA(LL) | cH(LH) | | | | (cA, (cH, cV, cD)) <---> ------------------- | | | | cV(HL) | cD(HH) | | | | -------------------
PyWavelets does not follow this pattern because of pure practical reasons of simple access to particular type of the output coefficients.
Single level idwt2
¶
-
pywt.
idwt2
(coeffs, wavelet, mode='symmetric', axes=(-2, -1))¶ 2-D Inverse Discrete Wavelet Transform.
Reconstructs data from coefficient arrays.
Parameters: coeffs : tuple
(cA, (cH, cV, cD)) A tuple with approximation coefficients and three details coefficients 2D arrays like from dwt2()
wavelet : Wavelet object or name string
Wavelet to use
mode : str, optional
Signal extension mode, see Modes (default: ‘symmetric’)
axes : 2-tuple of ints, optional
Axes over which to compute the IDWT. Repeated elements mean the IDWT will be performed multiple times along these axes.
Examples
>>> import numpy as np >>> import pywt >>> data = np.array([[1,2], [3,4]], dtype=np.float64) >>> coeffs = pywt.dwt2(data, 'haar') >>> pywt.idwt2(coeffs, 'haar') array([[ 1., 2.], [ 3., 4.]])
2D multilevel decomposition using wavedec2
¶
-
pywt.
wavedec2
(data, wavelet, mode='symmetric', level=None, axes=(-2, -1))¶ Multilevel 2D Discrete Wavelet Transform.
Parameters: data : ndarray
2D input data
wavelet : Wavelet object or name string
Wavelet to use
mode : str, optional
Signal extension mode, see Modes (default: ‘symmetric’)
level : int, optional
Decomposition level (must be >= 0). If level is None (default) then it will be calculated using the
dwt_max_level
function.axes : 2-tuple of ints, optional
Axes over which to compute the DWT. Repeated elements are not allowed.
Returns: [cAn, (cHn, cVn, cDn), ... (cH1, cV1, cD1)] : list
Coefficients list. For user-specified
axes
,cH*
corresponds toaxes[0]
whilecV*
corresponds toaxes[1]
. The first element returned is the approximation coefficients for the nth level of decomposition. Remaining elements are tuples of detail coefficients in descending order of decomposition level. (i.e. cH1 are the horizontal detail coefficients at the first level)Examples
>>> import pywt >>> import numpy as np >>> coeffs = pywt.wavedec2(np.ones((4,4)), 'db1') >>> # Levels: >>> len(coeffs)-1 2 >>> pywt.waverec2(coeffs, 'db1') array([[ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., 1.]])
2D multilevel reconstruction using waverec2
¶
-
pywt.
waverec2
(coeffs, wavelet, mode='symmetric', axes=(-2, -1))¶ Multilevel 2D Inverse Discrete Wavelet Transform.
- coeffs : list or tuple
- Coefficients list [cAn, (cHn, cVn, cDn), ... (cH1, cV1, cD1)]
- wavelet : Wavelet object or name string
- Wavelet to use
- mode : str, optional
- Signal extension mode, see Modes (default: ‘symmetric’)
- axes : 2-tuple of ints, optional
- Axes over which to compute the IDWT. Repeated elements are not allowed.
Returns: 2D array of reconstructed data. Examples
>>> import pywt >>> import numpy as np >>> coeffs = pywt.wavedec2(np.ones((4,4)), 'db1') >>> # Levels: >>> len(coeffs)-1 2 >>> pywt.waverec2(coeffs, 'db1') array([[ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., 1.]])
2D coordinate conventions¶
The labels for “horizontal” and “vertical” used by dwt2
and idwt2
follow the common mathematical convention that coordinate axis 0
is horizontal while axis 1 is vertical:
dwt2, idwt2 convention
----------------------
axis 1 ^
|
|
|
|--------->
axis 0
Note that this is different from another common convention used in computer
graphics and image processing (e.g. by matplotlib’s imshow
and functions in
scikit-image
). In those packages axis 0 is a vertical axis and axis 1 is
horizontal as follows:
imshow convention
-------------------
axis 1
|--------->
|
|
|
axis 0 v