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

图形图象

开发平台:

Matlab

  1. %Name: Chris Shoemaker
  2. %Course: EER-280 - Digital Watermarking
  3. %Project:  Comparison-Based Correlation in DCT mid-band
  4. %           Uses two PN sequences; one for a "0" and another for a "1"
  5. %           Watermark Embeding
  6. clear all;
  7. % save start time
  8. start_time=cputime;
  9. k=15;                           % set gain factor for embeding
  10. blocksize=8;                    % set the dct blocksize
  11. pn_sequence_search='T';         % perform search to find highly uncorrelated pn sequences {T,F}
  12. midband=[   0,0,0,1,1,1,1,0;    % defines the mid-band frequencies of an 8x8 dct
  13.             0,0,1,1,1,1,0,0;
  14.             0,1,1,1,1,0,0,0;
  15.             1,1,1,1,0,0,0,0;
  16.             1,1,1,0,0,0,0,0;
  17.             1,1,0,0,0,0,0,0;
  18.             1,0,0,0,0,0,0,0;
  19.             0,0,0,0,0,0,0,0 ];
  20.         
  21. % read in the cover object
  22. file_name='_lena_std_bw.bmp';
  23. cover_object=double(imread(file_name));
  24. % determine size of cover image
  25. Mc=size(cover_object,1);         %Height
  26. Nc=size(cover_object,2);         %Width
  27. % determine maximum message size based on cover object, and blocksize
  28. max_message=Mc*Nc/(blocksize^2);
  29. % read in the message image
  30. file_name='_copyright.bmp';
  31. message=double(imread(file_name));
  32. Mm=size(message,1);                 %Height
  33. Nm=size(message,2);                 %Width
  34. % reshape the message to a vector
  35. message=round(reshape(message,Mm*Nm,1)./256);
  36. % check that the message isn't too large for cover
  37. if (length(message) > max_message)
  38.     error('Message too large to fit in Cover Object')
  39. end
  40. % pad the message out to the maximum message size with ones's
  41. message_vector=ones(1,max_message);
  42. message_vector(1:length(message))=message;
  43. % generate shell of watermarked image
  44. watermarked_image=cover_object;
  45. % read in key for PN generator
  46. file_name='_key.bmp';
  47. key=double(imread(file_name))./256;
  48. % reset MATLAB's PN generator to state "key"
  49. rand('state',key);
  50. % generate PN sequences for "1" and "0"
  51. pn_sequence_one=round(2*(rand(1,sum(sum(midband)))-0.5));
  52. pn_sequence_zero=round(2*(rand(1,sum(sum(midband)))-0.5));
  53. % find two highly un-correlated PN sequences
  54. if (pn_sequence_search=='T')
  55.     while (corr2(pn_sequence_one,pn_sequence_zero) > -0.55)
  56.         pn_sequence_one=round(2*(rand(1,sum(sum(midband)))-0.5));
  57.         pn_sequence_zero=round(2*(rand(1,sum(sum(midband)))-0.5));
  58.     end
  59. end
  60.     
  61. % process the image in blocks
  62. x=1;
  63. y=1;
  64. for (kk = 1:length(message_vector))
  65.     % transform block using DCT
  66.     dct_block=dct2(cover_object(y:y+blocksize-1,x:x+blocksize-1));
  67.     
  68.     % if message bit contains zero then embed pn_sequence_zero into the mid-band
  69.     % componants of the dct_block
  70.     ll=1;
  71.     if (message_vector(kk)==0)
  72.         for ii=1:blocksize
  73.             for jj=1:blocksize
  74.                 if (midband(jj,ii)==1)
  75.                     dct_block(jj,ii)=dct_block(jj,ii)+k*pn_sequence_zero(ll);
  76.                     ll=ll+1;
  77.                 end
  78.             end
  79.         end
  80.     
  81.     % otherwise, embed pn_sequence_one into the mid-band componants of dct_block    
  82.     else
  83.         for ii=1:blocksize
  84.             for jj=1:blocksize
  85.                 if (midband(jj,ii)==1)
  86.                     dct_block(jj,ii)=dct_block(jj,ii)+k*pn_sequence_one(ll);
  87.                     ll=ll+1;
  88.                 end
  89.             end
  90.         end               
  91.     end
  92.     
  93.     % transform block back into spatial domain
  94.     watermarked_image(y:y+blocksize-1,x:x+blocksize-1)=idct2(dct_block);    
  95.     
  96.     % move on to next block. At and of row move to next row
  97.     if (x+blocksize) >= Nc
  98.         x=1;
  99.         y=y+blocksize;
  100.     else
  101.         x=x+blocksize;
  102.     end
  103. end
  104. % convert to uint8 and write the watermarked image out to a file
  105. watermarked_image_int=uint8(watermarked_image);
  106. imwrite(watermarked_image_int,'dct2_watermarked_mod.bmp','bmp');
  107. % display processing time
  108. elapsed_time=cputime-start_time,
  109. % display psnr of watermarked image
  110. psnr=psnr(cover_object,watermarked_image,Nc,Mc),
  111. % display watermarked image
  112. figure(1)
  113. imshow(watermarked_image,[])
  114. title('Watermarked Image')