The WTN function returns a multi-dimensional discrete wavelet transform of the input array A. The transform is based on a Daubechies wavelet filter.
WTN is based on the routine
wtn
described in section 13.10 of
Numerical Recipes in C: The Art of Scientific Computing
(Second Edition), published by Cambridge University Press, and is used by permission.
Set this keyword if the input array A is in column-major format (composed of column vectors) rather than in row-major format (composed of row vectors).
This example demonstrates the use of IDL's discrete wavelet transform and sparse array storage format to compress and store an 8-bit gray-scale digital image. First, an image selected from the
people.dat
data file is transformed into its wavelet representation and written to a separate data file using the WRITEU procedure.
Next, the transformed image is converted, using the SPRSIN function, to row-indexed sparse storage format retaining only elements with an absolute magnitude greater than or equal to a specified threshold. The sparse image is written to a data file using the WRITE_SPR procedure.
Finally, the transformed image is reconstructed from the storage file and displayed alongside the original.
Begin by choosing the number of wavelet coefficients to use and a threshold value:
Open the
people.dat
data file, read an image using associated variables, and close the file.
OPENR, 1, FILEPATH('people.dat', SUBDIR = ['examples','data'])
images = assoc(1, bytarr(192, 192))
Expand the image to the nearest power of two using cubic convolution, and transform the image into its wavelet representation using the WTN function.
image_1 = CONGRID(image_1, pwr, pwr, /CUBIC)
wtn_image = WTN(image_1, coeffs)
Write the image to a file using the WRITEU procedure and check the size of the file (in bytes) using the FSTAT function.
PRINT, 'The size of the file is ', status.size, ' bytes.'
The size of the file is 262144 bytes.
Now, we convert the wavelet representation of the image to a row-indexed sparse storage format using the SPRSIN function, write the data to a file using the WRITE_SPR procedure, and check the size of the "compressed" file.
sprs_image = SPRSIN(wtn_image, THRES = thres)
WRITE_SPR, sprs_image, 'sparse.dat'
PRINT, 'The size of the file is ', status.size, ' bytes.'
The size of the file is 69600 bytes.
Determine the number of elements (as a percentage of total elements) whose absolute magnitude is less than the specified threshold. These elements are not retained in the row-indexed sparse storage format.
PRINT, 100.*N_ELEMENTS(WHERE(ABS(wtn_image) LT thres, $
This means the sparse array contains only 13% of the elements contained in the original array. Next, read the row-indexed sparse data back from the file
sparse.dat
using the READ_SPR function and reconstruct the image from the non-zero data using the FULSTR function.
sprs_image = READ_SPR('sparse.dat')
wtn_image = FULSTR(sprs_image)
Apply the inverse wavelet transform to the image.
image_2 = WTN(wtn_image, COEFFS, /INVERSE)
Calculate and print the amount of data used in reconstruction of the image.
PRINT, 'The image on the right is reconstructed from:', $
100.0 - (100.* count/N_ELEMENTS(image_1)),$
The image on the right is reconstructed from:
12.9669% of original image data.
Finally, display the original and reconstructed images side by side.
WINDOW, 1, XSIZE = pwr*2, YSIZE = pwr, $
The image on the left is the original 256 by 256 image. The image on the right was compressed by the above process and was reconstructed from 13% of the original data. The size of the compressed image's data file is 26.6% of the size of the original image's data file. Note that due to limitations in the printing process, differences between the images may not be as evident as they would be on a high-resolution printer or monitor.