dct2_recover.m
上传用户:haoweisi88
上传日期:2010-02-03
资源大小:1950k
文件大小:3k
源码类别:

图形图象

开发平台:

Matlab

  1. %Name: Chris Shoemaker
  2. %Course: EER-280 - Digital Watermarking
  3. %Project:  Threshold-Based Correlation in DCT mid-band
  4. %           Uses two PN sequences; one for a "0" and another for a "1"
  5. %           Watermark Recovery
  6. clear all;
  7. % save start time
  8. start_time=cputime;
  9. blocksize=8;                    % set the dct blocksize
  10. midband=[   0,0,0,1,1,1,1,0;    % defines the mid-band frequencies of an 8x8 dct
  11.             0,0,1,1,1,1,0,0;
  12.             0,1,1,1,1,0,0,0;
  13.             1,1,1,1,0,0,0,0;
  14.             1,1,1,0,0,0,0,0;
  15.             1,1,0,0,0,0,0,0;
  16.             1,0,0,0,0,0,0,0;
  17.             0,0,0,0,0,0,0,0 ];
  18.         
  19. % read in the watermarked object
  20. file_name='dct2_watermarked_mod.bmp';
  21. watermarked_image=double(imread(file_name));
  22. % determine size of watermarked image
  23. Mw=size(watermarked_image,1);         %Height
  24. Nw=size(watermarked_image,2);         %Width
  25. % determine maximum message size based on cover object, and blocksize
  26. max_message=Mw*Nw/(blocksize^2);
  27. % read in original watermark
  28. file_name='_copyright.bmp';
  29. orig_watermark=double(imread(file_name));
  30. % determine size of original watermark
  31. Mo=size(orig_watermark,1); %Height
  32. No=size(orig_watermark,2); %Width
  33. % read in key for PN generator
  34. file_name='_key.bmp';
  35. key=double(imread(file_name))./256;
  36. % reset MATLAB's PN generator to state "key"
  37. rand('state',key);
  38. % generate PN sequence
  39. pn_sequence_zero=round(2*(rand(1,sum(sum(midband)))-0.5));
  40. % process the image in blocks
  41. x=1;
  42. y=1;
  43. for (kk = 1:max_message)
  44.     % transform block using DCT
  45.     dct_block=dct2(watermarked_image(y:y+blocksize-1,x:x+blocksize-1));
  46.     
  47.     % extract the middle band coeffcients
  48.     ll=1;
  49.     for ii=1:blocksize
  50.         for jj=1:blocksize
  51.             if (midband(jj,ii)==1)
  52.                 sequence(ll)=dct_block(jj,ii);
  53.                 ll=ll+1;
  54.             end
  55.         end
  56.     end
  57.     
  58.     % calculate the correlation of the middle band sequence to pn sequence
  59.     correlation(kk)=corr2(pn_sequence_zero,sequence);
  60.     % move on to next block. At and of row move to next row
  61.     if (x+blocksize) >= Nw
  62.         x=1;
  63.         y=y+blocksize;
  64.     else
  65.         x=x+blocksize;
  66.     end
  67. end
  68. % if correlation exceeds threshold, set bit to '0', otherwise '1'
  69. for (kk=1:Mo*No)
  70.     if (correlation(kk) < mean(correlation(1:Mo*No)))
  71.         message_vector(kk)=0;
  72.     else
  73.         message_vector(kk)=1;
  74.     end
  75. end
  76. % reshape the embeded message
  77. message=reshape(message_vector(1:Mo*No),Mo,No);
  78. % display processing time
  79. elapsed_time=cputime-start_time,
  80. % display recovered message
  81. figure(2)
  82. imshow(message,[])
  83. title('Recovered Message')