0
votes

My task

1. I have a haar filter such as [1][-1]
2. I have an image such as
- - [a][b][c][d]
- - [e][f][g][h]
3. I want to perform a wavelet transform on the image and have the following result:
- - Step 1:
- - - [mean(a,b)][mean(c,d)][a-b][c-d]
- - - [mean(e,f)][mean(g,h)][e-f][g-h]
- - Rename: --- just for a clear presentation
- - - [a'][b'][c'][d']
- - - [e'][f'][g'][h']
- - Step 2:
- - - [mean(a',b')][a'-b'][c'][d']
- - - [mean(e',f')][e'-f'][g'][h']
4. The result of Step 2 is what I want.

Question
I found that there is a toolbox in Matlab that is about wavelet application.
I used this command [cA,cH,cV,cD] = dwt2(I,'haar'); The output cV is close but not what I want.
So I would like to ask that how can I perform my task in Matlab in a simply way which means that I can use some built in tools to perform this task.

My way

function haar_wavelet(input_im)
I = imread(input_im);
I = rgb2gray(I);
I = imresize(I,[512 512]);
[I_row,I_col]=size(I);

if(mod(I_col,2))
    I_col=I_col-1;
end

haar_mask = make_haar(I_row,I_col);

I=double(I);

new_M = I(:,1:I_col);

j=2;
i=I_col;
while(I_col/j ~= 2)
    i = [i I_col/j];
    j = j*2;
end

for k = i
    new_M(:,1:k) = my_haar_trans(new_M(:,1:k),haar_mask(:,1:k));
end
figure
imshow(new_M)


end


function [output_haar] = make_haar(row,col)
    output_haar=ones(row,col)*2;
    for i = 2:2:col
        output_haar(:,i)=output_haar(:,i)*0;
    end
    output_haar=output_haar-1;
end

function [output_M] = my_haar_trans(target_M,mask)
    temp_diff=target_M.*mask;
    [diff_row,diff_col]=size(temp_diff);

    diff_M=zeros(diff_row,diff_col/2);
    for i = 1:diff_col/2
        diff_M(:,i)=temp_diff(:,2*i-1)+temp_diff(:,2*i);
    end

    mean_M=zeros(diff_row,diff_col/2);
    for i = 1:diff_col/2
        mean_M(:,i)=target_M(:,2*i-1)+target_M(:,2*i);
    end

    output_M=[mean_M diff_M];
end

This is what I am doing, is there a easier way?

1

1 Answers

0
votes

As you said there is a built in matlab code to perform 1D and 2D wavelet transform.For example in 2D Haar wavelet case it is [cA,cH,cV,cD] = dwt2(I,'db1'). This is exactly what you want.Also you can write your own function based on wavelet decomposition block diagram.Be aware that for vectors of small size the output must be the same but it may have some residuals for big size vectors like images.