MATLAB: I am doing watermarking with dwt. When i don’t use any attacks, the output watermark is fine. But when i do any attack for e.g., mean attack, i get the cover image itself as the extracted watermark.Can you please check and tell what i am doing wron

dwtMATLABwatermarking

clear all;
close all;
%save start time
start_time=cputime;
[filename,pathname]=uigetfile('*.jpg');
if ~filename
errordlg('Select an Image File.');
return;
end
a=imread(strcat(pathname,filename));
figure;imshow(a);
a=rgb2gray(a);
a =im2double(a);
a=imresize(a,[512 512]);
[LL,LH,HL,HH] = dwt2(a,'haar');
[LL1,LH1,HL1,HH1]=dwt2(LL,'haar');
dwt_img=[[LL1,LH1; HL1,HH1],LH;HL,HH];
figure;imshow(dwt_img);
figure;imshow(LL1);
[filename,pathname]=uigetfile('*.jpg');
if ~filename
errordlg('Select an Image File.');
return;
end
wm=imread(strcat(pathname,filename));
figure;imshow(wm);
wm=rgb2gray(wm);
wm=im2double(wm);
wm=imresize(wm,[512 512]);
[wm_LL,wm_LH,wm_HL,wm_HH] = dwt2(wm,'haar');
[wm_LL1,wm_LH1,wm_HL1,wm_HH1]=dwt2(wm_LL,'haar');
dwt_wmimg=[[wm_LL1,wm_LH1; wm_HL1,wm_HH1],wm_LH;wm_HL,wm_HH];
figure;imshow(dwt_wmimg);
figure;imshow(wm_LL1);
%watermarking using alpha-blending technique
WMI = HL1+ 0.01*wm_HL1;
figure;imshow(WMI);
%computing level-1 idwt2
Watermarkedimage_level1= idwt2(LL1,LH1,WMI,HH1,'haar');
%computing level-2 idwt2
wm=idwt2(Watermarkedimage_level1,LH,HL,HH,'haar');
figure;imshow(wm,[]);title('watermarked image');
%Watermark Extraction before attack
watermarkedimage=wm;
[ex_LL, ex_LH, ex_HL, ex_HH]=dwt2(watermarkedimage,'haar');
[ex_LL1, ex_LH1, ex_HL1, ex_HH1]=dwt2(ex_LL,'haar');
recovered_watermark_HL1=(ex_HL1-HL1)/0.01;
figure;
imshow(recovered_watermark_HL1,[]);
title('extracted watermark')
%output
Im=idwt2(wm_LL1,wm_LH1,recovered_watermark_HL1, wm_HH1,'haar');
Recovered_watermark=idwt2(wm_LL,wm_LH, Im, wm_HH,'haar');
figure;imshow(Recovered_watermark,[]);title('Recovered Watermark');
%attack
attackedImage = meanAttack(wm);
figure;imshow(attackedImage,[]);title('Attacked Image');
watermarkedattackedimage=attackedImage;
[at_LL, at_LH, at_HL, at_HH]=dwt2(watermarkedattackedimage,'haar');
[at_LL1, at_LH1, at_HL1, at_HH1]=dwt2(at_LL,'haar');
recovered_attacked_watermark_HL1=(at_HL1-HL1)/0.01;
figure;
imshow(recovered_attacked_watermark_HL1,[]);
title('extracted watermark')
RecIm=idwt2(wm_LL1,wm_LH1,recovered_attacked_watermark_HL1, wm_HH1,'haar');
Recovered_watermark=idwt2(wm_LL,wm_LH, RecIm, wm_HH,'haar');
figure;imshow(Recovered_watermark,[]);title('Recovered Watermark');
%function
function meanImageAttacked = meanAttack(watermarked_image)
h = fspecial('average', [5, 5]);
meanImageAttacked = filter2(h, watermarked_image);
end

Best Answer

Your procedure is flawed in that you failed to do a control test to see what would happen if you applied the attack process to an image that had not been watermarked (or which had been watermarked with a constant image)
Your attack is affecting the entire watermarked image, including the bits used to encode the watermark. When you dct2 the attacked image, you are not going to extract the hidden coefficients: you might extract at the proper location but the values have been blurred. Your hiding process depends upon the premise that the attack will not be severe enough to make the extracted coefficients unrecognizable -- but what if the attack is sufficiently severe? Then your extracted image might not be good enough. If you had tested with a constant image (all zero for example) then you would probably have observed that the image that came out looked a fair bit like the cover image.
It is impossible to embed an image well enough to make it recoverable from all attacks -- since, after all, the attack could change most every bit of the image, making it a different image of the same size. So every embedding is limited. Your attack is too strong for your embedding scheme, and you did not plan for what the result might look like in that case so you thought you were getting the original image out.
There are other embedding schemes that can stand up to more. For example you can put the data to be embedded through an Error Correcting Code. Or you could embed the same data multiple times and when you recover, have the copies "vote" as to what the correct bit is.