实际上,在产品里能够设置密码并且在产品中添加水印是最理想的防盗措施,因为设置密码是为了防止别人剽窃技术或使用产品,而添加水印更多的是一种补救措施,是在他人成功盗取自己技术的前提下,在法律上夺取主动权的砝码。下面来介绍一下如何在MATLAB中实现往数字图像中添加水印效果,具体如下:
1、在MATLAB主界面的编辑器中写入下列代码:
%此程序实现数字图像的水印嵌入
function dwtgl
clc;clear all;close all;
disp('计算机正在准备输出数字水印图像,请耐心等待……');
input=imread('H:\2018秋资料\mok\a7.jpg');%从H盘里读出原始图像
input_add=input;
water=imread('H:\2018秋资料\mok\a6.jpg');%读出水印图像
water_add=water;
%将原始图像和水印的RGB三色分离
input=double(input);water=double(water);inputr=input(:,:,1);waterr=water(:,:,1);
inputg=input(:,:,2);waterg=water(:,:,2);inputb=input(:,:,3);waterb=water(:,:,3);
%系数r大,增加鲁棒性,不易随几何变形和物理变形失去水印,但是水印的透明性较差
r=0.06;
[Cwr,Swr]=wavedec2(waterr,1,'haar');%水印R分解
[Cr,Sr]=wavedec2(inputr,2,'haar');%图像R分解
%水印的嵌入
Cr(1:size(Cwr,2)/16)=Cr(1:size(Cwr,2)/16)+r*Cwr(1:size(Cwr,2)/16);
k=0;
while k<=size(Cr,2)/size(Cwr,2)-1Cr(1+size(Cr,2)/4+k*size(Cwr,2)/4:size(Cr,2)/4+...(k+1)*size(Cwr,2)/4)=Cr(1+size(Cr,2)/4+...k*size(Cwr,2)/4:size(Cr,2)/4+(k+1)*size(Cwr,2)/4)+...r*Cwr(1+size(Cwr,2)/4:size(Cwr,2)/2); Cr(1+size(Cr,2)/2+k*size(Cwr,2)/4:size(Cr,2)/2+...(k+1)*size(Cwr,2)/4)=Cr(1+size(Cr,2)/2+...k*size(Cwr,2)/4:size(Cr,2)/2+(k+1)*size(Cwr,2)/4)+...r*Cwr(1+size(Cwr,2)/2:3*size(Cwr,2)/4); Cr(1+3*size(Cr,2)/4+k*size(Cwr,2)/4:3*size(Cr,2)/4+...(k+1)*size(Cwr,2)/4)=Cr(1+3*size(Cr,2)/4+...k*size(Cwr,2)/4:3*size(Cr,2)/4+(k+1)*size(Cwr,2)/4)+...r*Cwr(1+3*size(Cwr,2)/4:size(Cwr,2)); k=k+1;
end;
Cr(1:size(Cwr,2)/4)=Cr(1:size(Cwr,2)/4)+r*Cwr(1:size(Cwr,2)/4);
g=0.03;
[Cwg,Swg]=wavedec2(waterg,1,'haar');%水印G分解
[Cg,Sg]=wavedec2(inputg,2,'haar');%图像G分解
%水印的嵌入
Cg(1:size(Cwg,2)/16)= Cg(1:size(Cwg,2)/16)+r*Cwg(1:size(Cwg,2)/16);
k=0;
while k<=size(Cg,2)/size(Cwg,2)-1Cg(1+size(Cg,2)/4+k*size(Cwg,2)/4:size(Cg,2)/4+...(k+1)*size(Cwg,2)/4)=Cg(1+size(Cg,2)/4+...k*size(Cwg,2)/4:size(Cg,2)/4+(k+1)*size(Cwg,2)/4)+...r*Cwg(1+size(Cwg,2)/4:size(Cwg,2)/2); Cg(1+size(Cg,2)/2+k*size(Cwg,2)/4:size(Cg,2)/2+...(k+1)*size(Cwg,2)/4)=Cg(1+size(Cg,2)/2+...k*size(Cwg,2)/4:size(Cg,2)/2+(k+1)*size(Cwg,2)/4)+...r*Cwg(1+size(Cwg,2)/2:3*size(Cwg,2)/4); Cg(1+3*size(Cg,2)/4+k*size(Cwg,2)/4:3*size(Cg,2)/4+...(k+1)*size(Cwg,2)/4)=Cg(1+3*size(Cg,2)/4+...k*size(Cwg,2)/4:3*size(Cg,2)/4+(k+1)*size(Cwg,2)/4)+...r*Cwg(1+3*size(Cwg,2)/4:size(Cwg,2)); k=k+1;
end;
Cg(1:size(Cwg,2)/4)=Cg(1:size(Cwg,2)/4)+g*Cwg(1:size(Cwg,2)/4);
b=0.12;
[Cwb,Swb]=wavedec2(waterb,1,'haar');%水印B分解
[Cb,Sb]=wavedec2(inputb,2,'haar');%图像B分解
%水印的嵌入
Cb(1:size(Cwb,2)/16)=Cb(1:size(Cwb,2)/16)+r*Cwb(1:size(Cwb,2)/16);
k=0;
while k<=size(Cb,2)/size(Cwb,2)-1Cb(1+size(Cb,2)/4+k*size(Cwb,2)/4:size(Cb,2)/4+...(k+1)*size(Cwb,2)/4)=Cb(1+size(Cb,2)/4+...k*size(Cwb,2)/4:size(Cb,2)/4+(k+1)*size(Cwb,2)/4)+...r*Cwb(1+size(Cwb,2)/4:size(Cwb,2)/2); Cb(1+size(Cb,2)/2+k*size(Cwb,2)/4:size(Cb,2)/2+...(k+1)*size(Cwb,2)/4)=Cb(1+size(Cb,2)/2+...k*size(Cwb,2)/4:size(Cb,2)/2+(k+1)*size(Cwb,2)/4)+...r*Cwb(1+size(Cwb,2)/2:3*size(Cwb,2)/4); Cb(1+3*size(Cb,2)/4+k*size(Cwb,2)/4:3*size(Cb,2)/4+...(k+1)*size(Cwb,2)/4)=Cb(1+3*size(Cb,2)/4+...k*size(Cwb,2)/4:3*size(Cb,2)/4+(k+1)*size(Cwb,2)/4)+...r*Cwb(1+3*size(Cwb,2)/4:size(Cwb,2)); k=k+1;
end;
Cb(1:size(Cwb,2)/4)=Cb(1:size(Cwb,2)/4)+b*Cwb(1:size(Cwb,2)/4);
%用哈尔小波重构图像,亦可用mexh等小波重构
inputr=waverec2(Cr,Sr,'haar');
inputg=waverec2(Cg,Sg,'haar');
inputb=waverec2(Cb,Sb,'haar');
%三色的叠加
temp=size(inputr);
pic=zeros(temp(1),temp(2),3);
for i=1:temp(1);for j=1:temp(2);pic(i,j,1)=inputr(i,j);pic(i,j,2)=inputg(i,j);pic(i,j,3)=inputb(i,j);end
end
output=uint8(round(pic));
watermarked_image_uint8=uint8(output);%转化为uint8
imwrite(watermarked_image_uint8,'watermarked.jpg','jpg');
%画出水印图像
figure(1),imshow(input_add);
t1=title('原始图像');
set(t1,'fontsize',15,'color',[0 0 0],'fontname','隶书');
figure(2),imshow(water_add);
t2=title('水印图像');
set(t2,'fontsize',15,'color',[0 0 0],'fontname','隶书');
figure(3),imshow(watermarked_image_uint8)
;t3=title('嵌入水印后的图像');
set(t3,'fontsize',15,'color',[0 0 0],'fontname','隶书');
2、命名保存至自定义路径下,点击运行,结果如下:
如图所示,分别输出了原始图像,水印图以及嵌入水印后的图,可以明显看到嵌入水印后还是存在区别的,同时我们也可以设置标题的格式等,基本介绍完毕,请大家继续关注!!!