dct2_embed.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 Embeding
  6. clear all;
  7. % save start time
  8. start_time=cputime;
  9. k=20;                           % set gain factor for embeding
  10. blocksize=8;                    % set the dct blocksize
  11. midband=[   0,0,0,1,1,1,1,0;    % defines the mid-band frequencies of an 8x8 dct
  12.             0,0,1,1,1,1,0,0;
  13.             0,1,1,1,1,0,0,0;
  14.             1,1,1,1,0,0,0,0;
  15.             1,1,1,0,0,0,0,0;
  16.             1,1,0,0,0,0,0,0;
  17.             1,0,0,0,0,0,0,0;
  18.             0,0,0,0,0,0,0,0 ];
  19.         
  20. % read in the cover object
  21. file_name='_lena_std_bw.bmp';
  22. cover_object=double(imread(file_name));
  23. % determine size of cover image
  24. Mc=size(cover_object,1);         %Height
  25. Nc=size(cover_object,2);         %Width
  26. % determine maximum message size based on cover object, and blocksize
  27. max_message=Mc*Nc/(blocksize^2);
  28. % read in the message image
  29. file_name='_copyright.bmp';
  30. message=double(imread(file_name));
  31. Mm=size(message,1);                 %Height
  32. Nm=size(message,2);                 %Width
  33. % reshape the message to a vector
  34. message=round(reshape(message,Mm*Nm,1)./256);
  35. % check that the message isn't too large for cover
  36. %if (length(message) > max_message)
  37. %    error('Message too large to fit in Cover Object')
  38. %end
  39. % pad the message out to the maximum message size with ones's
  40. message_vector=ones(1,max_message);
  41. message_vector(1:length(message))=message;
  42. % generate shell of watermarked image
  43. watermarked_image=cover_object;
  44. % read in key for PN generator
  45. file_name='_key.bmp';
  46. key=double(imread(file_name))./256;
  47. % reset MATLAB's PN generator to state "key"
  48. rand('state',key);
  49. % generate PN sequence
  50. pn_sequence_zero=round(2*(rand(1,sum(sum(midband)))-0.5));
  51. % process the image in blocks
  52. x=1;
  53. y=1;
  54. for (kk = 1:length(message_vector))
  55.     % transform block using DCT
  56.     dct_block=dct2(cover_object(y:y+blocksize-1,x:x+blocksize-1));
  57.     
  58.     % if message bit contains zero then embed pn_sequence_zero into the mid-band
  59.     % componants of the dct_block
  60.     ll=1;
  61.     if (message_vector(kk)==0)
  62.         for ii=1:blocksize
  63.             for jj=1:blocksize
  64.                 if (midband(jj,ii)==1)
  65.                     dct_block(jj,ii)=dct_block(jj,ii)+k*pn_sequence_zero(ll);
  66.                     ll=ll+1;
  67.                 end
  68.             end
  69.         end
  70.     end
  71.     
  72.     % transform block back into spatial domain
  73.     watermarked_image(y:y+blocksize-1,x:x+blocksize-1)=idct2(dct_block);    
  74.     
  75.     % move on to next block. At and of row move to next row
  76.     if (x+blocksize) >= Nc
  77.         x=1;
  78.         y=y+blocksize;
  79.     else
  80.         x=x+blocksize;
  81.     end
  82. end
  83. % convert to uint8 and write the watermarked image out to a file
  84. watermarked_image_int=uint8(watermarked_image);
  85. imwrite(watermarked_image_int,'dct2_watermarked.bmp','bmp');
  86. % display processing time
  87. elapsed_time=cputime-start_time,
  88. % display psnr of watermarked image
  89. psnr=psnr(cover_object,watermarked_image,Nc,Mc),
  90. % display watermarked image
  91. figure(1)
  92. imshow(watermarked_image,[])
  93. title('Watermarked Image')