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

图形图象

开发平台:

Matlab

  1. %Name: Chris Shoemaker
  2. %Course: EER-280 - Digital Watermarking
  3. %Project:  Block DCT Based method, using comparision between mid-band coeffcients
  4. %           Watermark Embeding
  5. clear all;
  6. % save start time
  7. start_time=cputime;
  8. k=50;           % set minimum coeff difference
  9. blocksize=8;    % set the size of the block in cover to be used for each bit in watermark
  10. % read in the cover object
  11. file_name='_lena_std_bw.bmp';
  12. cover_object=double(imread(file_name));
  13. % determine size of cover image
  14. Mc=size(cover_object,1);         %Height
  15. Nc=size(cover_object,2);         %Width
  16. % determine maximum message size based on cover object, and blocksize
  17. max_message=Mc*Nc/(blocksize^2);
  18. % read in the message image
  19. file_name='_copyright.bmp';
  20. message=double(imread(file_name));
  21. Mm=size(message,1);                 %Height
  22. Nm=size(message,2);                 %Width
  23. % reshape the message to a vector
  24. message=fix(reshape(message,Mm*Nm,1)./2);
  25. message=message(1:2000);
  26. % check that the message isn't too large for cover
  27. if (length(message) > max_message)
  28.     error('Message too large to fit in Cover Object')
  29. end
  30. % pad the message out to the maximum message size with ones
  31. message_pad=ones(1,max_message);
  32. message_pad(1:length(message))=message;
  33. % generate shell of watermarked image
  34. watermarked_image=cover_object;
  35. % process the image in blocks
  36. % encodes such that (5,2) > (4,3) when message(kk)=0
  37. % and that (5,2) < (4,3) when message(kk)=1
  38. x=1;
  39. y=1;
  40. for (kk = 1:length(message_pad))
  41.     % transform block using DCT
  42.     dct_block=dct2(cover_object(y:y+blocksize-1,x:x+blocksize-1));
  43.     
  44.     % if message bit is black, (5,2) > (4,3)
  45.     if (message_pad(kk) == 0)
  46.         % if (5,2) < (4,3) then we need to swap them
  47.         if (dct_block(5,2) < dct_block(4,3))
  48.                 temp=dct_block(4,3);
  49.                 dct_block(4,3)=dct_block(5,2);
  50.                 dct_block(5,2)=temp;
  51.         end
  52.     
  53.     % if message bit is white, (5,2) < (4,3)
  54.     elseif (message_pad(kk) == 1)
  55.         
  56.         % if (5,2) > (4,3) then we need to swap them
  57.         if (dct_block(5,2) >= dct_block(4,3))
  58.                 temp=dct_block(4,3);
  59.                 dct_block(4,3)=dct_block(5,2);
  60.                 dct_block(5,2)=temp;
  61.         end
  62.     end
  63.     
  64.     % now we adjust the two values such that their difference >= k
  65.     if dct_block(5,2) > dct_block(4,3)
  66.         if dct_block(5,2) - dct_block(4,3) < k
  67.             dct_block(5,2)=dct_block(5,2)+(k/2);
  68.             dct_block(4,3)=dct_block(4,3)-(k/2);            
  69.         end
  70.     else  
  71.          if dct_block(4,3) - dct_block(5,2) < k
  72.             dct_block(4,3)=dct_block(4,3)+(k/2);  
  73.             dct_block(5,2)=dct_block(5,2)-(k/2);
  74.         end
  75.     end
  76.         
  77.     % transform block back into spatial domain
  78.     watermarked_image(y:y+blocksize-1,x:x+blocksize-1)=idct2(dct_block);    
  79.     
  80.     % move on to next block. At and of row move to next row
  81.     if (x+blocksize) >= Nc
  82.         x=1;
  83.         y=y+blocksize;
  84.     else
  85.         x=x+blocksize;
  86.     end
  87. end
  88. % convert to uint8 and write the watermarked image out to a file
  89. watermarked_image_int=uint8(watermarked_image);
  90. imwrite(watermarked_image_int,'dct1_watermarked.bmp','bmp');
  91. % display processing time
  92. elapsed_time=cputime-start_time,
  93. % display psnr of watermarked image
  94. psnr=psnr(cover_object,watermarked_image,Nc,Mc),
  95. % display watermarked image
  96. figure(1)
  97. imshow(watermarked_image,[])
  98. title('Watermarked Image')
  99. figure(2)
  100. imshow(cover_object,[])