Decompose and recompose an image using dwt and dct

185 Views Asked by At

My idea is to decompose an image using dwt2 first and then apply dct2 to coefficients_approximation, apply a watermark to the result and then recompose the image.

final_image = idwt2( idct2 ( dct2 ( dwt2 (starting_image))))

But whenever I do it i lose a lot of quality I can not figure out why. Here is the code, any idea?

clear all; clc;

%   read lena img
lena = double(imread('lena.jpg'));

%   read watermark img
%w = imread('mark.png');
load cookiebears.mat

%   compure DWT on lena and watermark
[approximate_lena, horizontal_lena, vertical_lena, diagonal_lena] = dwt2(lena, 'haar');

%   i have to save the image first and then open it in order to use it in
%   dct2() omitting this step generate an error
imwrite(uint8(approximate_lena), 'ca_lena.jpg');

%   read approximate_lena layer in a variable
lena_ca = double(imread('ca_lena.jpg'));

%   perform DCT on approximation level of lena picture
dct_lena = dct2(lena_ca);

%   embed the watermark into dct_lena
%   insert here function to embed the watermark
%   dct_lena = insert_watermark();

%   now I can recompose the lena coefficients approximation 
lena_ca_recomposed = idct2(dct_lena);

%   recompose DWT
lena_recomposed = idwt2(lena_ca_recomposed, horizontal_lena,vertical_lena, diagonal_lena, 'haar');

As you can see below: In the first row we have the starting picture on the left and the resultant picture on the righr (which is much darker and with less details). In the second row on the left we have the comparison between CA after the dwt2 on the original image and on the right the CA recomposed with idct2 Image

1

There are 1 best solutions below

0
On

The problem was caused by saving the image to a file and reading it again. I removed the following lines:

imwrite(uint8(approximate_lena), 'ca_lena.jpg');

and

lena_ca = double(imread('ca_lena.jpg'));

Now it works