martes, 28 de mayo de 2013

Homework 6 - Compression

For this last entry, will show the work done for the image compression.

Tools

To perform this homework, use Wavelets, a type of Fourier transform, and in conjunction with Python, use the PyWavelets library for use in compression and so do not use other libraries or use other mathematical methods.

First, the tools were:
  • PyWavelets 0.2.2 (Link to download the module: Here)
    • Download: "PyWavelets-0.2.2.win32-py2.7"
  • Should be installed Numpy
  • And likewise have installed PIL
As evidence use JPG images with extension.

Perform Three types of compression, in the first use "2D Wavelet Packets" (wp2), the second using the transformed "Stationary wavelet transform" (SWT) and the third used 2D Forward and Inverse Discrete Wavelet Transform (DWT2).

To perform this task, I based on codes Pywavelets Documentation, and the tasks of my partners.

Operation

First Program:

In this first program, use the package WP2, and then a little explanation:

Wavelet Packet nodes are arranged in a tree. Each node in a WP tree is uniquely identified and addressed by a path string.

In the 1D WaveletPacket case nodes were accessed using 'a' (approximation) and 'd' (details) path names (each node has two 1D children).

Because now we deal with a bit more complex structure (each node has four children), we have four basic path names based on the dwt 2D output convention to address the WP2D structure:

"a" - LL, low-low coefficients
"h" - LH, low-high coefficients
"v"- HL, high-low coefficients
"d" - HH, high-high coefficients

In other words, subnode naming corresponds to the dwt2() function output naming convention (as wavelet packet transform is based on the dwt2 transform):

Here a figure explaining this:

Now, we have the results.

Results:

Test image 1:
Result 1:


Test image 2:

Result 2:

Test Image 3:

Result 3:

Test image 4:

Result 4:

In the first results shows the grayscale image and then show that the images are slowly decomposing.

Code:


Testing:

Time:

Make small calculating the average testing time of the program, the results were:

Time with images 1:
Time with images 2:
Time with images 3:
Time with images 4:

Average:

7.318 seconds

Graph: 

Here, it can be seen that the processing time is not very slow.
----------------------------------------------------------------------------------------------------------------------------------------------

Second Program:

Now, in this second program, use SWT transform, here is a brief explanation:

Performs multilevel 2D Stationary Wavelet Transform:

pywt.swt2(data, wavelet, level[, start_level=0])

Parameters:

data – 2D array with input data.
wavelet – Wavelet to use in the transform. This can be a name of the wavelet from the wavelist() list or a Wavelet object instance.
level – Number of decomposition steps to perform.
start_level – The level at which the decomposition will begin.

The result is a set of coefficients arrays over the range of decomposition levels:

Where cA is approximation, cH is horizontal details, cV is vertical details, cD is diagonal details, n is start_level and m equals n+level.

Results:

Test image 1:

Result 1:

Test image 2:

Result 2:


Test image 3:

Result 3:

Test image 4:

Result 4:

This time, the results, the images are decomposed by levels, and the image is blurred.

Code:


Testing:

Time:

Again, do a small test program by calculating the average time of program operation, the results were:

Time with images 1:
Time with images 2:
Time with images 3:
Time with images 4:

Average:

13.021 seconds

Graph:

Here, we show in a more clear that the processing time is longer.

----------------------------------------------------------------------------------------------------------------------------------------------

Third Program:

The final program, use the package DWT2, and then a little explanation:

The dwt2() function performs single level 2D Discrete Wavelet Transform.

pywt.dwt2(data, wavelet[, mode='sym'])

Parameters:
  • data – 2D input data.
  • wavelet – Wavelet to use in the transform.
  • mode Signal extension mode to deal with the border distortion problem.
Returns one average and three details 2D coefficients arrays. The coefficients arrays are organized in tuples in the following form:

(cA, (cH, cV, cD))

where cA, cH, cV, cD denote approximation, horizontal detail, vertical detail and diagonal detail coefficients respectively.

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:
PyWavelets does not follow this pattern because of pure practical reasons of simple access to particular type of the output coefficients.

Results:

Test image 1:
Result 1:

Test image 2:

Result 2:

Test image 3:

Result 3:

The images are pixelated by the compression

Code:


Testing:

Weight:

After making the process of compressing each image, now show you the final size of each image.

Image1:

Image2:

Image3:
The images are remarkably compressed, it shows they are pixelated

Conclusions:

The make three different transforms and get the results, I concluded that wavelets is excellent for image compression, although the process is rather slow, the compression is done in a good way.

No matter the size of the images and dimensions for testing, provided

And in the time calculations for each program, using packages WP2 is a little faster processing than using STW transform, the packet DWT2 is slow but effective.

Graph:

Comparing the time of the two first tests (SWT and WP2):


It may be noted that the time was less with the tests performed with the package WP2.

The process was slow to make DWT2 testing, compared to the other examples, and and it was difficult to estimate with a graph

Repository:

1 comentario:

  1. Pues, en una compresión hubiera esperado que el resultado final-final fuese la misma imagen pero en un archivo más pequeño :P Sin embargo, el reporte está en inglés y es bastante completo; van 7 pts por ello. Los códigos con varios, aunque no muy originales. Que sean 7 por el programa también.

    ResponderEliminar