📄 dct88.m
字号:
function []=dct88(o,c,w)
%Name: Digital Watermarking Embeding Algorithms Based On 8*8 Block DCT
%Programmer: Chen Ning
%Algorithms Description: 8*8 Block DCT Based Method:using comparision between mid-band coeffcients
% Watermark Embeding
dif=50; % set minimum coefficient difference
blocksize=8; % set the size of the block in order to be used for each bit in watermark
% read in the original image
file_name = o ;
ori_img=double(imread(file_name));
% calculate the size of original image
Mc=size(ori_img,1); %height of original image
Nc=size(ori_img,2); %width of original image
% calculate the maximum size of message that can be embeded in the original image
max_message=Mc*Nc/(blocksize^2);
% read in the original watermarking image
file_name = c;
message=double(imread(file_name));
% calculate the size of original watermarking image
Mm=size(message,1); %height of watermarking image
Nm=size(message,2); %width of watermarking image
% reshape the message to a vector with one row and Mm*Nm columns
message=message';
message=reshape(message,1,Mm*Nm);
message_pad=message;
% check if the message is too large for original image
if (length(message) > max_message)
error('Message too large for original image!')
end
% pad the message out to the maximum message size with ones
message=(ones(1,max_message))';
% generate shell of watermarked image
watermarked_image=ori_img;
% divide the original image into blocks with size of 8*8
% if message(k)=0 adjust the DCT coefficient to make sure that (5,2)>(4,3)
% if message(k)=1 adjust the DCT coefficient to make sure that (5,2)<(4,3)
i=1;
j=1;
for (k = 1:max_message)
% make DCT transformation for each 8*8 blocks
dct_block=dct2(ori_img(j:j+blocksize-1,i:i+blocksize-1));
% if message bit is black, make sure that (5,2)>(4,3)
if (message_pad(k) == 0)
if (dct_block(5,2) < dct_block(4,3))
temp=dct_block(4,3);
dct_block(4,3)=dct_block(5,2);
dct_block(5,2)=temp;
end
% if message bit is white, make sure that (5,2)<(4,3)
elseif (message_pad(k) == 1)
if (dct_block(5,2) >= dct_block(4,3))
temp=dct_block(4,3);
dct_block(4,3)=dct_block(5,2);
dct_block(5,2)=temp;
end
end
% adjust the two values to make such that their difference is larger than 'dif'
if dct_block(5,2) > dct_block(4,3)
if dct_block(5,2) - dct_block(4,3) < dif
dct_block(5,2)=dct_block(5,2)+(dif/2);
dct_block(4,3)=dct_block(4,3)-(dif/2);
end
else
if dct_block(4,3) - dct_block(5,2) < dif
dct_block(4,3)=dct_block(4,3)+(dif/2);
dct_block(5,2)=dct_block(5,2)-(dif/2);
end
end
% transform 8*8 DCT block back into spatial domain
watermarked_image(j:j+blocksize-1,i:i+blocksize-1)=idct2(dct_block);
% at the end of a row ,move on to the next row
if (i+blocksize) >= Nc
i=1;
j=j+blocksize;
% move on to the next 8*8 block in the same row
else
i=i+blocksize;
end
end
% convert to uint8 and write the watermarked image out to a file
watermarked_image_int=uint8(watermarked_image);
imwrite(watermarked_image_int,w,'bmp');
%display watermarked image
figure(1)
imshow(watermarked_image,[])
title('Watermarked Image')
figure(2)
imshow(ori_img,[])
title('Original Image')
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -