To accomplish this task using what are wavelets, which are a type of Fourier transform. The wavelet transform is to compare the signal with certain wavelet functions, which are obtained from the mother wavelet. The comparison allows to obtain the coefficients are interpretable and further manipulation.
To carry out the compression used pywavelets which is a wave abirto code and serves to transform python. It is necessary to use this module you need to install pywt. To do this we can just download it from the terminal with the following line:
sudo apt-get install python-pywt
To do this work with the Discrete Wavelet Transform, with which we obtain a new representation of the image to be treated.
Within this process the TWD provides a matrix of coefficients, which are the four wavelet coefficients:
- horizontal coefficients
- vertical coefficients
- diagonal coefficients
In the following image shows how the coefficients are obtained:
Haar transform used to treat 2-dimensional images, which we use to get our 4 coefficients.
The process is performed as follows:
- Enter imgagen you want to compress
- After you get the size in bytes of the image from the original image
- We estimate each of the coefficients (approximations, horizontal, vertical and diagonal)
- After obtaining the coefficients, we apply them within a binary filter for so the new images.
- Also after that calculate the inverse image to apply the compression process.
Below will show some of the tests performed with different images.
Finally this is the compressed image below shows the change that was from one image to another. (reverse image)
Here attached another test that you make with a different image which contained a little more color, and is slightly heavier than the first.
According to the tests you make think it depends on the weight of the image, so that when compressing it look as similar to the original, another thing I noticed is that it also depends on how well defined this image, because for example in the first test can fully appreciate the detail between one figure and another, while in the second test is different.
Another thing you can do is check if the compression works with images that contain a lot of noise and not lost many details of the original image. Below I made a small test this:
And the reverse image:
Here attached the code you use.