Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
need help to understand wavelet image compression code, in matlab

Subject: need help to understand wavelet image compression code, in matlab

From: chetan sood

Date: 29 Apr, 2009 15:44:01

Message: 1 of 10

thank you in advance actually i am studying in btech electronics and communication and have undertaken a project to use wavelets to compress images, i searched a lot of codes online in an attempt to understand how to build a software to compress images . one of the code i am interested in is given below and i would like the help of brilliant ppl who visit this forum . i hope some one would help me on this .

below is the code and i have put in my problems with it in comments . thank u again in advance

any explanation would be helpful as i am a rookie in the field of matlab and wavelets



clear all;
close all;
image = imread('rice.png');
input_image_used = im2double(image); % why do we need double ????
n=input('Enter the decomposition level : ');
[Lo_D,Hi_D,Lo_R,Hi_R] = wfilters('haar'); % what is the physical significance of the
% 4 filters .
[c,s]=wavedec2(input_image_used,n,Lo_D,Hi_D);
disp('The decomposition vector output is');
disp(c);
disp('Size : ');
disp(s);
[thr,nkeep] = wdcbm2(c,s,1.5,3*prod(s(1,:)));
[xd,cxd,sxd,perf0,perfl2] = wdencmp('lvd',c,s,'haar',n,thr,'s');
disp('Compression ratio in percentage : ');
disp(perfl2);
subplot(1,2,1);
imshow(input_image_used);
title('Input image');
subplot(1,2,2);
imshow(xd);
title('Compressed image');

Subject: need help to understand wavelet image compression code, in matlab

From: Thomas Clark

Date: 29 Apr, 2009 20:43:01

Message: 2 of 10

Hi Chetan.

Firstly, you'll find a number of toolboxes for wavelet compression here on the mathworks file exchange.
So, I started off going through your code below, but it's basically ended up as an explanation of wavelets! Bear in mind that I'm a diehard Matlab user - so given that you're new, I'll insert some (hopefully useful) comments on coding style at the same time.

> clear all;
> close all;

You're killing us. If you clear all and close all, you'll lose any work currently in the Matlab environment. Consider putting the following code into a function, then you don't have to worry about it affecting other work, and functions have their own 'variable workspace'

> image = imread('rice.png');
'image' is a function name, so don't use it as a variable name as well - sooner or later things will get confusing!

> input_image_used = im2double(image); % why do we need double ????
Depending on the file read in, the imread function can read in arrays which are of the wrong data type - often an integer (do some research about variable type and class if you're unsure what this means). Converting to double (which is MATLAB's default variable type) means you are able to use this variable in subsequent analysis codes.

> n=input('Enter the decomposition level : ');
> [Lo_D,Hi_D,Lo_R,Hi_R] = wfilters('haar'); % what is the physical significance of the 4 filters?

Wow, this is a really hard one to answer in brief. I'd recommend you get a copy of Ingrid Daubechies' book 'Ten Lectures On Wavelets' to fully understand the issue. However, I have a simple explanation.
*CAVEAT#1* This will get the mathematicians really screeching, because there are many subtleties - but might help you to understand whats going on.
*CAVEAT#2* Don't actually try taking a wavelet transform using the procedure below. Again, hidden nasties - so always use a proper wavelet transform code.
1. Take an FFT of a signal (say an image)
2. Take the FFT of a different signal, let's call that signal W
3. Convolve the two signals (multiply in the fourier domain)
4. Take the IFFT
What this does is extract particular features of the image - so any features within the image which resemble the signal W will be accentuated; any features which don't will be suppressed.
NB hopefully this stuff should be familiar to you - if it isn't, don't even think about wavelets until you've learned at least the basics of fourier transforms.
We call this signal W a 'filter', as convolving the image with it filters out aspects of the image which are not similar to the filter signal.

A wavelet is a filter just like that; but there is an additional step. You may have heard of a multiresolution analysis (MRA) - if not, you are now! Imagine repeating the process above many times - each time you halve the size of your filter signal W, keeping it the same shape. Comparing the output images (after the several different IFFTs) you'd notice something: Large basic shapes would be represented where the larger filters are used; and smaller details of the image would come out in the results where smaller filters are used. Make sense?

A wavelet transform is a multiresolution analysis, using the shape of the wavelet as the filter - thereby separating the input signal into different scale components. Once you've taken the forward wavelet transform you end up with, a signal which contains position and 'energy' (magnitude) of the components of your signal, for each different scale. **

So, you want to try this? if your signal W is some general or arbitrary signal, you'll get garbage. However, nice mathematicians come along and define particular shapes which don't give garbage. These shapes are 'wavelets'.

Different types of wavelet have different characteristics. A few keywords describing wavelet characteristics you might hear are 'compact support' and 'biorthogonal'. Wavelets often come in familes (similar shapes/derivations) - you'll hear about 'mexican hats', 'haar', 'daubechies', 'interpolets'... a great one to start with is Daubechies 4th order wavelets - they're good for image processing (I *think i remember* they have compact support, and good aliasing characteristics).

In the case of a haar type (if I remember correctly) the shape of the wavelet is a square pulse (someone please correct me if I'm wrong). So these wavelets are good at capturing sharp edges in input signals. I *think* that haar wavelets are second order (I'm remembering this stuff from ages ago!).

Picking up from the ** earlier on, it's worth mentioning the reason why wavelets are so good for compression - if you look at the magnitude of your signal, then lots of it is actually close to zero at any given scale. A full transform takes up the same space in memory as the original image - so get rid of the components which are close to zero and store only the 'energetic' components. This takes up much less memory, but you can reconstruct the image very well as you've only got rid of bits which barely contributed anyway. You'll probably know by now that this is how JPEG images work - and store pictures in such little memory.

Getting back to the original question, the 4 filters are: 2 for decomposition, 2 for reconstruction.
I think that you only do two resolution levels using the haar wavelet - so I infer (not familiar with the toolbox you're using either, as I write my own codes) that Lo_D is the filter for the lower resolution and Hi_D is the one for (surprise!) higher resolution.

Note that I've been using the word filter very generally here; so we need to be a little careful. The reason filters are used to perform a wavelet transform is that the FFT introduces nasties (as sines and cosines are the basis functions, they are infinite functions therefor don't have compact support - you can get some strange effects). It was found (I think by the mathematician Meyer) that instead of applying the FFT-Convolve type approach I stated above, that you could do to wavelet transforms by directly deriving the filters associated with the wavelet shapes you want and operating with them - not requiring an FFT. This is faster and eliminates problems of compact support. These are the types of filters represeneted by the variables Hi_D and Lo_D. (I.e. those variables don't directly contain the wavelet, just the filter associated with it).


> [c,s]=wavedec2(input_image_used,n,Lo_D,Hi_D);
I'm sorry, pal - I'm lost from here on in, because I'm not familiar with the toolbox so couldn't explain the individual commands. Hopefully in the light of what I've said above, the toolbox documentation will be a little clearer so you can make your own way.

Subject: need help to understand wavelet image compression code, in matlab

From: chetan sood

Date: 1 May, 2009 16:37:01

Message: 3 of 10

"Thomas Clark" <t.clark@remove.spamcantab.net> wrote in message <gtae4l$dll$1@fred.mathworks.com>...
> Hi Chetan.
>
> Firstly, you'll find a number of toolboxes for wavelet compression here on the mathworks file exchange.
> So, I started off going through your code below, but it's basically ended up as an explanation of wavelets! Bear in mind that I'm a diehard Matlab user - so given that you're new, I'll insert some (hopefully useful) comments on coding style at the same time.
>
> > clear all;
> > close all;
>
> You're killing us. If you clear all and close all, you'll lose any work currently in the Matlab environment. Consider putting the following code into a function, then you don't have to worry about it affecting other work, and functions have their own 'variable workspace'
>
> > image = imread('rice.png');
> 'image' is a function name, so don't use it as a variable name as well - sooner or later things will get confusing!
>
> > input_image_used = im2double(image); % why do we need double ????
> Depending on the file read in, the imread function can read in arrays which are of the wrong data type - often an integer (do some research about variable type and class if you're unsure what this means). Converting to double (which is MATLAB's default variable type) means you are able to use this variable in subsequent analysis codes.
>
> > n=input('Enter the decomposition level : ');
> > [Lo_D,Hi_D,Lo_R,Hi_R] = wfilters('haar'); % what is the physical significance of the 4 filters?
>
> Wow, this is a really hard one to answer in brief. I'd recommend you get a copy of Ingrid Daubechies' book 'Ten Lectures On Wavelets' to fully understand the issue. However, I have a simple explanation.
> *CAVEAT#1* This will get the mathematicians really screeching, because there are many subtleties - but might help you to understand whats going on.
> *CAVEAT#2* Don't actually try taking a wavelet transform using the procedure below. Again, hidden nasties - so always use a proper wavelet transform code.
> 1. Take an FFT of a signal (say an image)
> 2. Take the FFT of a different signal, let's call that signal W
> 3. Convolve the two signals (multiply in the fourier domain)
> 4. Take the IFFT
> What this does is extract particular features of the image - so any features within the image which resemble the signal W will be accentuated; any features which don't will be suppressed.
> NB hopefully this stuff should be familiar to you - if it isn't, don't even think about wavelets until you've learned at least the basics of fourier transforms.
> We call this signal W a 'filter', as convolving the image with it filters out aspects of the image which are not similar to the filter signal.
>
> A wavelet is a filter just like that; but there is an additional step. You may have heard of a multiresolution analysis (MRA) - if not, you are now! Imagine repeating the process above many times - each time you halve the size of your filter signal W, keeping it the same shape. Comparing the output images (after the several different IFFTs) you'd notice something: Large basic shapes would be represented where the larger filters are used; and smaller details of the image would come out in the results where smaller filters are used. Make sense?
>
> A wavelet transform is a multiresolution analysis, using the shape of the wavelet as the filter - thereby separating the input signal into different scale components. Once you've taken the forward wavelet transform you end up with, a signal which contains position and 'energy' (magnitude) of the components of your signal, for each different scale. **
>
> So, you want to try this? if your signal W is some general or arbitrary signal, you'll get garbage. However, nice mathematicians come along and define particular shapes which don't give garbage. These shapes are 'wavelets'.
>
> Different types of wavelet have different characteristics. A few keywords describing wavelet characteristics you might hear are 'compact support' and 'biorthogonal'. Wavelets often come in familes (similar shapes/derivations) - you'll hear about 'mexican hats', 'haar', 'daubechies', 'interpolets'... a great one to start with is Daubechies 4th order wavelets - they're good for image processing (I *think i remember* they have compact support, and good aliasing characteristics).
>
> In the case of a haar type (if I remember correctly) the shape of the wavelet is a square pulse (someone please correct me if I'm wrong). So these wavelets are good at capturing sharp edges in input signals. I *think* that haar wavelets are second order (I'm remembering this stuff from ages ago!).
>
> Picking up from the ** earlier on, it's worth mentioning the reason why wavelets are so good for compression - if you look at the magnitude of your signal, then lots of it is actually close to zero at any given scale. A full transform takes up the same space in memory as the original image - so get rid of the components which are close to zero and store only the 'energetic' components. This takes up much less memory, but you can reconstruct the image very well as you've only got rid of bits which barely contributed anyway. You'll probably know by now that this is how JPEG images work - and store pictures in such little memory.
>
> Getting back to the original question, the 4 filters are: 2 for decomposition, 2 for reconstruction.
> I think that you only do two resolution levels using the haar wavelet - so I infer (not familiar with the toolbox you're using either, as I write my own codes) that Lo_D is the filter for the lower resolution and Hi_D is the one for (surprise!) higher resolution.
>
> Note that I've been using the word filter very generally here; so we need to be a little careful. The reason filters are used to perform a wavelet transform is that the FFT introduces nasties (as sines and cosines are the basis functions, they are infinite functions therefor don't have compact support - you can get some strange effects). It was found (I think by the mathematician Meyer) that instead of applying the FFT-Convolve type approach I stated above, that you could do to wavelet transforms by directly deriving the filters associated with the wavelet shapes you want and operating with them - not requiring an FFT. This is faster and eliminates problems of compact support. These are the types of filters represeneted by the variables Hi_D and Lo_D. (I.e. those variables don't directly contain the wavelet, just the filter associated with it).
>
>
> > [c,s]=wavedec2(input_image_used,n,Lo_D,Hi_D);
> I'm sorry, pal - I'm lost from here on in, because I'm not familiar with the toolbox so couldn't explain the individual commands. Hopefully in the light of what I've said above, the toolbox documentation will be a little clearer so you can make your own way.






THANKS A LOT,
 first of all i would really thank you for taking a minute (actually i believe it would have been more than that) to reply to my query. your reply was of utmost help, and yes i am familiar with MRA and ffts and convolution. your answer was very helpful. again heartiest thanks.

and may god bless you.

Subject: need help to understand wavelet image compression code, in matlab

From: chetan sood

Date: 1 May, 2009 16:38:01

Message: 4 of 10

"Thomas Clark" <t.clark@remove.spamcantab.net> wrote in message <gtae4l$dll$1@fred.mathworks.com>...
> Hi Chetan.
>
> Firstly, you'll find a number of toolboxes for wavelet compression here on the mathworks file exchange.
> So, I started off going through your code below, but it's basically ended up as an explanation of wavelets! Bear in mind that I'm a diehard Matlab user - so given that you're new, I'll insert some (hopefully useful) comments on coding style at the same time.
>
> > clear all;
> > close all;
>
> You're killing us. If you clear all and close all, you'll lose any work currently in the Matlab environment. Consider putting the following code into a function, then you don't have to worry about it affecting other work, and functions have their own 'variable workspace'
>
> > image = imread('rice.png');
> 'image' is a function name, so don't use it as a variable name as well - sooner or later things will get confusing!
>
> > input_image_used = im2double(image); % why do we need double ????
> Depending on the file read in, the imread function can read in arrays which are of the wrong data type - often an integer (do some research about variable type and class if you're unsure what this means). Converting to double (which is MATLAB's default variable type) means you are able to use this variable in subsequent analysis codes.
>
> > n=input('Enter the decomposition level : ');
> > [Lo_D,Hi_D,Lo_R,Hi_R] = wfilters('haar'); % what is the physical significance of the 4 filters?
>
> Wow, this is a really hard one to answer in brief. I'd recommend you get a copy of Ingrid Daubechies' book 'Ten Lectures On Wavelets' to fully understand the issue. However, I have a simple explanation.
> *CAVEAT#1* This will get the mathematicians really screeching, because there are many subtleties - but might help you to understand whats going on.
> *CAVEAT#2* Don't actually try taking a wavelet transform using the procedure below. Again, hidden nasties - so always use a proper wavelet transform code.
> 1. Take an FFT of a signal (say an image)
> 2. Take the FFT of a different signal, let's call that signal W
> 3. Convolve the two signals (multiply in the fourier domain)
> 4. Take the IFFT
> What this does is extract particular features of the image - so any features within the image which resemble the signal W will be accentuated; any features which don't will be suppressed.
> NB hopefully this stuff should be familiar to you - if it isn't, don't even think about wavelets until you've learned at least the basics of fourier transforms.
> We call this signal W a 'filter', as convolving the image with it filters out aspects of the image which are not similar to the filter signal.
>
> A wavelet is a filter just like that; but there is an additional step. You may have heard of a multiresolution analysis (MRA) - if not, you are now! Imagine repeating the process above many times - each time you halve the size of your filter signal W, keeping it the same shape. Comparing the output images (after the several different IFFTs) you'd notice something: Large basic shapes would be represented where the larger filters are used; and smaller details of the image would come out in the results where smaller filters are used. Make sense?
>
> A wavelet transform is a multiresolution analysis, using the shape of the wavelet as the filter - thereby separating the input signal into different scale components. Once you've taken the forward wavelet transform you end up with, a signal which contains position and 'energy' (magnitude) of the components of your signal, for each different scale. **
>
> So, you want to try this? if your signal W is some general or arbitrary signal, you'll get garbage. However, nice mathematicians come along and define particular shapes which don't give garbage. These shapes are 'wavelets'.
>
> Different types of wavelet have different characteristics. A few keywords describing wavelet characteristics you might hear are 'compact support' and 'biorthogonal'. Wavelets often come in familes (similar shapes/derivations) - you'll hear about 'mexican hats', 'haar', 'daubechies', 'interpolets'... a great one to start with is Daubechies 4th order wavelets - they're good for image processing (I *think i remember* they have compact support, and good aliasing characteristics).
>
> In the case of a haar type (if I remember correctly) the shape of the wavelet is a square pulse (someone please correct me if I'm wrong). So these wavelets are good at capturing sharp edges in input signals. I *think* that haar wavelets are second order (I'm remembering this stuff from ages ago!).
>
> Picking up from the ** earlier on, it's worth mentioning the reason why wavelets are so good for compression - if you look at the magnitude of your signal, then lots of it is actually close to zero at any given scale. A full transform takes up the same space in memory as the original image - so get rid of the components which are close to zero and store only the 'energetic' components. This takes up much less memory, but you can reconstruct the image very well as you've only got rid of bits which barely contributed anyway. You'll probably know by now that this is how JPEG images work - and store pictures in such little memory.
>
> Getting back to the original question, the 4 filters are: 2 for decomposition, 2 for reconstruction.
> I think that you only do two resolution levels using the haar wavelet - so I infer (not familiar with the toolbox you're using either, as I write my own codes) that Lo_D is the filter for the lower resolution and Hi_D is the one for (surprise!) higher resolution.
>
> Note that I've been using the word filter very generally here; so we need to be a little careful. The reason filters are used to perform a wavelet transform is that the FFT introduces nasties (as sines and cosines are the basis functions, they are infinite functions therefor don't have compact support - you can get some strange effects). It was found (I think by the mathematician Meyer) that instead of applying the FFT-Convolve type approach I stated above, that you could do to wavelet transforms by directly deriving the filters associated with the wavelet shapes you want and operating with them - not requiring an FFT. This is faster and eliminates problems of compact support. These are the types of filters represeneted by the variables Hi_D and Lo_D. (I.e. those variables don't directly contain the wavelet, just the filter associated with it).
>
>
> > [c,s]=wavedec2(input_image_used,n,Lo_D,Hi_D);
> I'm sorry, pal - I'm lost from here on in, because I'm not familiar with the toolbox so couldn't explain the individual commands. Hopefully in the light of what I've said above, the toolbox documentation will be a little clearer so you can make your own way.






THANKS A LOT,
 first of all i would really thank you for taking a minute (actually i believe it would have been more than that) to reply to my query. your reply was of utmost help, and yes i am familiar with MRA and ffts and convolution. your answer was very helpful. again heartiest thanks.

and may god bless you.

Subject: need help to understand wavelet image compression code, in matlab

From: Thomas Clark

Date: 2 May, 2009 11:33:01

Message: 5 of 10

You're welcome, chetan. Good luck :)

Subject: need help to understand wavelet image compression code, in matlab

From: sirishasrinivas.m@gmail.com

Date: 9 Jun, 2009 09:33:06

Message: 6 of 10


hi sir
every thing is understadable.
And i have done my project upto that.
I dont know how the classification step is done.
please send the code for classification using neural networks.

On Apr 30, 1:43 am, "Thomas Clark" <t.cl...@remove.spamcantab.net>
wrote:
> Hi Chetan.
>
> Firstly, you'll find a number of toolboxes for wavelet compression here on the mathworks file exchange.
> So, I started off going through your code below, but it's basically ended up as an explanation of wavelets! Bear in mind that I'm a diehard Matlab user - so given that you're new, I'll insert some (hopefully useful) comments on coding style at the same time.
>
> > clear all;
> > close all;
>
> You're killing us. If you clear all and close all, you'll lose any work currently in the Matlab environment. Consider putting the following code into a function, then you don't have to worry about it affecting other work, and functions have their own 'variable workspace'
>
> > image = imread('rice.png');
>
> 'image' is a function name, so don't use it as a variable name as well - sooner or later things will get confusing!
>
> > input_image_used = im2double(image); % why do we need double ????
>
> Depending on the file read in, the imread function can read in arrays which are of the wrong data type - often an integer (do some research about variable type and class if you're unsure what this means). Converting to double (which is MATLAB's default variable type) means you are able to use this variable in subsequent analysis codes.
>
> > n=input('Enter the decomposition level : ');
> > [Lo_D,Hi_D,Lo_R,Hi_R] = wfilters('haar'); % what is the physical significance of the 4 filters?
>
> Wow, this is a really hard one to answer in brief. I'd recommend you get a copy of Ingrid Daubechies' book 'Ten Lectures On Wavelets' to fully understand the issue. However, I have a simple explanation.
> *CAVEAT#1* This will get the mathematicians really screeching, because there are many subtleties - but might help you to understand whats going on.
> *CAVEAT#2* Don't actually try taking a wavelet transform using the procedure below. Again, hidden nasties - so always use a proper wavelet transform code.
> 1. Take an FFT of a signal (say an image)
> 2. Take the FFT of a different signal, let's call that signal W
> 3. Convolve the two signals (multiply in the fourier domain)
> 4. Take the IFFT
> What this does is extract particular features of the image - so any features within the image which resemble the signal W will be accentuated; any features which don't will be suppressed.
> NB hopefully this stuff should be familiar to you - if it isn't, don't even think about wavelets until you've learned at least the basics of fourier transforms.
> We call this signal W a 'filter', as convolving the image with it filters out aspects of the image which are not similar to the filter signal.
>
> A wavelet is a filter just like that; but there is an additional step. You may have heard of a multiresolution analysis (MRA) - if not, you are now! Imagine repeating the process above many times - each time you halve the size of your filter signal W, keeping it the same shape. Comparing the output images (after the several different IFFTs) you'd notice something: Large basic shapes would be represented where the larger filters are used; and smaller details of the image would come out in the results where smaller filters are used. Make sense?
>
> A wavelet transform is a multiresolution analysis, using the shape of the wavelet as the filter - thereby separating the input signal into different scale components. Once you've taken the forward wavelet transform you end up with, a signal which contains position and 'energy' (magnitude) of the components of your signal, for each different scale. **
>
> So, you want to try this? if your signal W is some general or arbitrary signal, you'll get garbage. However, nice mathematicians come along and define particular shapes which don't give garbage. These shapes are 'wavelets'.
>
> Different types of wavelet have different characteristics. A few keywords describing wavelet characteristics you might hear are 'compact support' and 'biorthogonal'. Wavelets often come in familes (similar shapes/derivations) - you'll hear about 'mexican hats', 'haar', 'daubechies', 'interpolets'... a great one to start with is Daubechies 4th order wavelets - they're good for image processing (I *think i remember* they have compact support, and good aliasing characteristics).
>
> In the case of a haar type (if I remember correctly) the shape of the wavelet is a square pulse (someone please correct me if I'm wrong). So these wavelets are good at capturing sharp edges in input signals. I *think* that haar wavelets are second order (I'm remembering this stuff from ages ago!).
>
> Picking up from the ** earlier on, it's worth mentioning the reason why wavelets are so good for compression - if you look at the magnitude of your signal, then lots of it is actually close to zero at any given scale. A full transform takes up the same space in memory as the original image - so get rid of the components which are close to zero and store only the 'energetic' components. This takes up much less memory, but you can reconstruct the image very well as you've only got rid of bits which barely contributed anyway. You'll probably know by now that this is how JPEG images work - and store pictures in such little memory.
>
> Getting back to the original question, the 4 filters are: 2 for decomposition, 2 for reconstruction.
> I think that you only do two resolution levels using the haar wavelet - so I infer (not familiar with the toolbox you're using either, as I write my own codes) that Lo_D is the filter for the lower resolution and Hi_D is the one for (surprise!) higher resolution.
>
> Note that I've been using the word filter very generally here; so we need to be a little careful. The reason filters are used to perform a wavelet transform is that the FFT introduces nasties (as sines and cosines are the basis functions, they are infinite functions therefor don't have compact support - you can get some strange effects). It was found (I think by the mathematician Meyer) that instead of applying the FFT-Convolve type approach I stated above, that you could do to wavelet transforms by directly deriving the filters associated with the wavelet shapes you want and operating with them - not requiring an FFT. This is faster and eliminates problems of compact support. These are the types of filters represeneted by the variables Hi_D and Lo_D. (I.e. those variables don't directly contain the wavelet, just the filter associated with it).
>
> > [c,s]=wavedec2(input_image_used,n,Lo_D,Hi_D);
>
> I'm sorry, pal - I'm lost from here on in, because I'm not familiar with the toolbox so couldn't explain the individual commands. Hopefully in the light of what I've said above, the toolbox documentation will be a little clearer so you can make your own way.

Subject: need help to understand wavelet image compression code, in matlab

From: Thomas Clark

Date: 10 Jun, 2009 08:30:06

Message: 7 of 10

Hello,

I think you've got the wrong thread- this one is about wavelet compression, not classification using neural networks - that's an entirely different subject.

Also, your requests are likely to get a better response if you give a thorough explanation of the problem.

Tom Clark

sirishasrinivas.m@gmail.com wrote in message <237a0c21-f5e9-4be8-a62b-b31189cdf624@21g2000vbk.googlegroups.com>...
>
> hi sir
> every thing is understadable.
> And i have done my project upto that.
> I dont know how the classification step is done.
> please send the code for classification using neural networks.
>

Subject: need help to understand haar wavelet

From: meena rao

Date: 25 Apr, 2011 00:24:05

Message: 8 of 10

Hi

Got lot of information about wavelets from your reply to chetan.
I used the wavedec2 function to perform wavelet decomposition on the image, and i had used haar wavelet. Since i have used the built in function to perform wavelet decomposition, i justed wanted to know whether i can get the basis function for haar wavelet and how it works when applied to images.

I just wanted to know whether can i get the horizontal, vertical and diagonal decomposition matrix by using the basis function rather then using the wavedec2 builtin function.

Thanks

meena
 

"chetan sood" <chetan_mastnath@yahoo.com> wrote in message <gtf8fd$qid$1@fred.mathworks.com>...
> "Thomas Clark" <t.clark@remove.spamcantab.net> wrote in message <gtae4l$dll$1@fred.mathworks.com>...
> > Hi Chetan.
> >
> > Firstly, you'll find a number of toolboxes for wavelet compression here on the mathworks file exchange.
> > So, I started off going through your code below, but it's basically ended up as an explanation of wavelets! Bear in mind that I'm a diehard Matlab user - so given that you're new, I'll insert some (hopefully useful) comments on coding style at the same time.
> >
> > > clear all;
> > > close all;
> >
> > You're killing us. If you clear all and close all, you'll lose any work currently in the Matlab environment. Consider putting the following code into a function, then you don't have to worry about it affecting other work, and functions have their own 'variable workspace'
> >
> > > image = imread('rice.png');
> > 'image' is a function name, so don't use it as a variable name as well - sooner or later things will get confusing!
> >
> > > input_image_used = im2double(image); % why do we need double ????
> > Depending on the file read in, the imread function can read in arrays which are of the wrong data type - often an integer (do some research about variable type and class if you're unsure what this means). Converting to double (which is MATLAB's default variable type) means you are able to use this variable in subsequent analysis codes.
> >
> > > n=input('Enter the decomposition level : ');
> > > [Lo_D,Hi_D,Lo_R,Hi_R] = wfilters('haar'); % what is the physical significance of the 4 filters?
> >
> > Wow, this is a really hard one to answer in brief. I'd recommend you get a copy of Ingrid Daubechies' book 'Ten Lectures On Wavelets' to fully understand the issue. However, I have a simple explanation.
> > *CAVEAT#1* This will get the mathematicians really screeching, because there are many subtleties - but might help you to understand whats going on.
> > *CAVEAT#2* Don't actually try taking a wavelet transform using the procedure below. Again, hidden nasties - so always use a proper wavelet transform code.
> > 1. Take an FFT of a signal (say an image)
> > 2. Take the FFT of a different signal, let's call that signal W
> > 3. Convolve the two signals (multiply in the fourier domain)
> > 4. Take the IFFT
> > What this does is extract particular features of the image - so any features within the image which resemble the signal W will be accentuated; any features which don't will be suppressed.
> > NB hopefully this stuff should be familiar to you - if it isn't, don't even think about wavelets until you've learned at least the basics of fourier transforms.
> > We call this signal W a 'filter', as convolving the image with it filters out aspects of the image which are not similar to the filter signal.
> >
> > A wavelet is a filter just like that; but there is an additional step. You may have heard of a multiresolution analysis (MRA) - if not, you are now! Imagine repeating the process above many times - each time you halve the size of your filter signal W, keeping it the same shape. Comparing the output images (after the several different IFFTs) you'd notice something: Large basic shapes would be represented where the larger filters are used; and smaller details of the image would come out in the results where smaller filters are used. Make sense?
> >
> > A wavelet transform is a multiresolution analysis, using the shape of the wavelet as the filter - thereby separating the input signal into different scale components. Once you've taken the forward wavelet transform you end up with, a signal which contains position and 'energy' (magnitude) of the components of your signal, for each different scale. **
> >
> > So, you want to try this? if your signal W is some general or arbitrary signal, you'll get garbage. However, nice mathematicians come along and define particular shapes which don't give garbage. These shapes are 'wavelets'.
> >
> > Different types of wavelet have different characteristics. A few keywords describing wavelet characteristics you might hear are 'compact support' and 'biorthogonal'. Wavelets often come in familes (similar shapes/derivations) - you'll hear about 'mexican hats', 'haar', 'daubechies', 'interpolets'... a great one to start with is Daubechies 4th order wavelets - they're good for image processing (I *think i remember* they have compact support, and good aliasing characteristics).
> >
> > In the case of a haar type (if I remember correctly) the shape of the wavelet is a square pulse (someone please correct me if I'm wrong). So these wavelets are good at capturing sharp edges in input signals. I *think* that haar wavelets are second order (I'm remembering this stuff from ages ago!).
> >
> > Picking up from the ** earlier on, it's worth mentioning the reason why wavelets are so good for compression - if you look at the magnitude of your signal, then lots of it is actually close to zero at any given scale. A full transform takes up the same space in memory as the original image - so get rid of the components which are close to zero and store only the 'energetic' components. This takes up much less memory, but you can reconstruct the image very well as you've only got rid of bits which barely contributed anyway. You'll probably know by now that this is how JPEG images work - and store pictures in such little memory.
> >
> > Getting back to the original question, the 4 filters are: 2 for decomposition, 2 for reconstruction.
> > I think that you only do two resolution levels using the haar wavelet - so I infer (not familiar with the toolbox you're using either, as I write my own codes) that Lo_D is the filter for the lower resolution and Hi_D is the one for (surprise!) higher resolution.
> >
> > Note that I've been using the word filter very generally here; so we need to be a little careful. The reason filters are used to perform a wavelet transform is that the FFT introduces nasties (as sines and cosines are the basis functions, they are infinite functions therefor don't have compact support - you can get some strange effects). It was found (I think by the mathematician Meyer) that instead of applying the FFT-Convolve type approach I stated above, that you could do to wavelet transforms by directly deriving the filters associated with the wavelet shapes you want and operating with them - not requiring an FFT. This is faster and eliminates problems of compact support. These are the types of filters represeneted by the variables Hi_D and Lo_D. (I.e. those variables don't directly contain the wavelet, just the filter associated with it).
> >
> >
> > > [c,s]=wavedec2(input_image_used,n,Lo_D,Hi_D);
> > I'm sorry, pal - I'm lost from here on in, because I'm not familiar with the toolbox so couldn't explain the individual commands. Hopefully in the light of what I've said above, the toolbox documentation will be a little clearer so you can make your own way.
>
>
>
>
>
>
> THANKS A LOT,
> first of all i would really thank you for taking a minute (actually i believe it would have been more than that) to reply to my query. your reply was of utmost help, and yes i am familiar with MRA and ffts and convolution. your answer was very helpful. again heartiest thanks.
>
> and may god bless you.

Subject: need help to understand wavelet image compression code, in matlab

From: Wayne King

Date: 25 Apr, 2011 07:07:03

Message: 9 of 10

"chetan sood" <chetan_mastnath@yahoo.com> wrote in message <gt9sk1$jph$1@fred.mathworks.com>...
> thank you in advance actually i am studying in btech electronics and communication and have undertaken a project to use wavelets to compress images, i searched a lot of codes online in an attempt to understand how to build a software to compress images . one of the code i am interested in is given below and i would like the help of brilliant ppl who visit this forum . i hope some one would help me on this .
>
> below is the code and i have put in my problems with it in comments . thank u again in advance
>
> any explanation would be helpful as i am a rookie in the field of matlab and wavelets
>
>
>
> clear all;
> close all;
> image = imread('rice.png');
> input_image_used = im2double(image); % why do we need double ????
> n=input('Enter the decomposition level : ');
> [Lo_D,Hi_D,Lo_R,Hi_R] = wfilters('haar'); % what is the physical significance of the
> % 4 filters .
> [c,s]=wavedec2(input_image_used,n,Lo_D,Hi_D);
> disp('The decomposition vector output is');
> disp(c);
> disp('Size : ');
> disp(s);
> [thr,nkeep] = wdcbm2(c,s,1.5,3*prod(s(1,:)));
> [xd,cxd,sxd,perf0,perfl2] = wdencmp('lvd',c,s,'haar',n,thr,'s');
> disp('Compression ratio in percentage : ');
> disp(perfl2);
> subplot(1,2,1);
> imshow(input_image_used);
> title('Input image');
> subplot(1,2,2);

Hi , Have you tried using wcompress()?

Wayne
> imshow(xd);
> title('Compressed image');

Subject: help on wavelets

From: meena rao

Date: 28 Apr, 2011 00:21:07

Message: 10 of 10

Hi Wayne

I am using the following code for my project work,

im=cellstr(im1); %%% im1 is an array with 15 images.
[r,c]=size(im);

close all;
for row=1:r
img=char(im(row));
img1=imread(img);
img1=im2double(img1); %% should i convert the image to double
    [c,s] = wavedec2(img1,2,'haar');
  
    %%second level detail coefficients %%%

[cH1,cV1,cD1] = detcoef2('all',c,s,2);

   %%% converting 2d to 1d array horizontal
  
  rk1 = reshape(cH1, 1, prod(size(cH1))) ;

t=numel(rk1);
y=1;
   for j=1:t
     if(rk1(1,j)~=0)
         ckH1(row,y)=rk1(1,j); %% eliminating zero's
         y=y+1;
     end
   end
   
 %%% converting 2d to 1d array vertical
rk1 = reshape(cV1, 1, prod(size(cV1))) ;

t=numel(rk1);
y=1;
   for j=1:t
     if(rk1(1,j)~=0)
         ckV1(row,y)=rk1(1,j); %% eliminating zero's
         y=y+1;
     end
   end
   
 %%% converting 2d to 1d array diagonal
rk1 = reshape(cD1, 1, prod(size(cD1))) ;

t=numel(rk1);
y=1;
   for j=1:t
     if(rk1(1,j)~=0)
         ckD1(row,y)=rk1(1,j);
         y=y+1;
     end
   end
   
end
   

meanckH1=mean(ckH1(:));
meanckV1=mean(ckV1(:));
meanckD1=mean(ckD1(:));


1. I am getting different decomposed coefficent values when i convert the image to double and then call wavedec2 function, compared to when i dont convert and use the image directly. Which is right should i convert the image to double or directly use it.

2. The values which i get are few negative values

3. The decomposed values are also decimal values

4. Can i go for 4th level of decomposition using haar wavelet.

Thanks in advance

Meena

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us