%Name: Chris Shoemaker
%Course: EER-280 - Digital Watermarking
%Project: CDMA based using multiple PN sequences embeded into whole object
% Watermark Embeding
% 图像未作变化,在空间域上直接修改像素值。每出现一个黑色像素就把整个图像素值加一个PN矩阵,改动较大,提取水印效果不理想
% 只能处理灰度图,因为彩图为三维数据,可以嵌入相应三维PN矩阵,但提取时无法比较两个三维矩阵的相关值
clear all;
% save start time
start_time=cputime;
k=2; % set the gain factor for embeding水印强度
% read in the cover object
file_name='lena.bmp';
%cover_object=double(imread(file_name));%产生三维的随机矩阵修改原图像素值,但是提取水印时无法比较两个三维矩阵的相关系数,因此无法得到彩图
cover_object=double(rgb2gray(imread(file_name)));%处理灰度图,产生的随机矩阵是二维的
% determine size of watermarked image
Mc=size(cover_object,1); %Height
Nc=size(cover_object,2); %Width
% read in the message image and reshape it into a vector
file_name='56.bmp';
message=double(imread(file_name));
Mm=size(message,1); %Height
Nm=size(message,2); %Width
message_vector=round(reshape(message,Mm*Nm,1)./256);%归一取整变二值图
% read in key for PN generator
%file_name='PN_key.bmp';
%key=double(imread(file_name))./256;
key=30;
% reset MATLAB's PN generator to state "key"
%rand('state',key(1:35,1));
rand('state',key);
watermarked_image=cover_object;
% when message contains a '0', add pn sequence with gain k to cover image
for kk=1:length(message_vector)
%pn_sequence=round(2*(rand(Mc,Nc,3)-0.5));%对整个原图像素点加PN随机数,随机矩阵与原图大小一致
pn_sequence=round(2*(rand(Mc,Nc)-0.5));%若是灰度图,产生二维随机矩阵,无法计算两个三维矩阵的相关值
if message(kk) == 0
watermarked_image=watermarked_image+k*pn_sequence;%有多少个黑色像素点就加几次随机矩阵
end
end
% convert back to uint8
watermarked_image_uint8=uint8(watermarked_image);
% write watermarked Image to file
imwrite(watermarked_image_uint8,'cdma_watermarked.bmp','bmp');
% display processing time
elapsed_time=cputime-start_time,
% calculate the PSNR
psnr=psnr(cover_object,watermarked_image_uint8,Mc,Nc),
% display watermarked Image
figure(1)
imshow(watermarked_image_uint8,[])
title('Watermarked Image')