MATLAB 水印嵌入函数 - wm(I,w,g)
MATLAB 水印嵌入函数 - wm(I,w,g)
此函数为嵌入过程,使用小波变换将水印信息嵌入图像。
输入:
- I:原始图像
- w:水印信息(二进制矩阵)
- g:水印嵌入的阈值
输出:
- J:嵌入水印的图像
代码:
function J = wm(I,w,g)
% 此函数为嵌入过程
% 输入:I为原始图像;w为水印信息;g为水印嵌入的阈值
% 输出:J为嵌入水印的图像
%%
% 参数设置
step = 0.5; % 小波系数增长步长
%%
[mm,nn] = size(I);
% 一层小波分解
[LL,LH,HL,HH] = dwt2(I,'haar');
%=======计算avr_LH======
[m0,n0] = size(LH); avr_LH = 0;
%=======计算avr_HL======
[m1,n1] = size(HL); avr_HL = 0;
% 判断嵌入的子带
if avr_LH <= avr_HL
num = 0; loca = LH;
else
num = 1; loca = HL;
end
%%
% 嵌入水印
if num == 0
loca = wm_type_0(loca,w,step,g); % LH是横向,写出嵌入子函数
else
loca = wm_type_1(loca,w,step,g); % HL是纵向,写出嵌入子函数
end
%%
if num == 0
LH = loca;
else
HL = loca;
end
J = idwt2(LL,LH,HL,HH,'haar',[mm,nn]); % 小波逆变换,还回原始图像
已知代码wm_type_0:
function loca = wm_type_0(loca,w,step,g)
% 此函数用于在HL子带嵌入水印
% 输入:loca为要嵌入水印的子带
% 输出:loca为嵌入水印后的子带
[m2,n2] = size(loca);
index = 1;
% 取出2*2的块
for k = 1:2:m2-1
for kk = 1:2:n2-1
tem = loca(k:k+1,kk:kk+1);
a11 = tem(1,1);
a12 = tem(1,2);
a21 = tem(2,1);
a22 = tem(2,2);
if 0 == w(index)
for s = step:step:100*step
if a11-a12<=g % 如果满足条件,a11增,a21减
a11 = a11 + s;
a12 = a12 - s;
else
break; % 不满足条件,跳出此循环
end
end
%-----如果a21和a22不满足条件,则也要调整------------
if a21-a22<=g
a21 = a21 + s;
a22 = a22 - s;
end
tem(1,1) = a11;
tem(1,2) = a12;
tem(2,1) = a21;
tem(2,2) = a22;
loca(k:k+1,kk:kk+1) = tem;
end
if 1 == w(index)
for s = step:step:100*step
if a11-a12>=-g % 如果满足条件,a11增,a21减
a11 = a11 - s;
a12 = a12 + s;
else
break; % 不满足条件,跳出此循环
end
end
%-----如果a21和a22不满足条件,则也要调整------------
if a21-a22<=g
a21 = a21 + s;
a22 = a22 - s;
end
tem(1,1) = a11;
tem(1,2) = a12;
tem(2,1) = a21;
tem(2,2) = a22;
loca(k:k+1,kk:kk+1) = tem;
end
index = index + 1;
tem(1,1) = a11;
tem(1,2) = a12;
tem(2,1) = a21;
tem(2,2) = a22;
loca(k:k+1,kk:kk+1) = tem;
end
end
给出wm_type_1代码:
function loca = wm_type_1(loca,w,step,g)
% 此函数用于在HL子带嵌入水印
% 输入:loca为要嵌入水印的子带
% 输出:loca为嵌入水印后的子带
[m2,n2] = size(loca);
index = 1;
% 取出2*2的块
for k = 1:2:m2-1
for kk = 1:2:n2-1
tem = loca(k:k+1,kk:kk+1);
a11 = tem(1,1);
a12 = tem(1,2);
a21 = tem(2,1);
a22 = tem(2,2);
if 0 == w(index)
for s = step:step:100*step
if a21-a11<=g % 如果满足条件,a21增,a22减
a21 = a21 + s;
a22 = a22 - s;
else
break; % 不满足条件,跳出此循环
end
end
%-----如果a12和a22不满足条件,则也要调整------------
if a12-a22<=g
a12 = a12 + s;
a22 = a22 - s;
end
tem(1,1) = a11;
tem(1,2) = a12;
tem(2,1) = a21;
tem(2,2) = a22;
loca(k:k+1,kk:kk+1) = tem;
end
if 1 == w(index)
for s = step:step:100*step
if a21-a11>=-g % 如果满足条件,a21减,a22增
a21 = a21 - s;
a22 = a22 + s;
else
break; % 不满足条件,跳出此循环
end
end
%-----如果a12和a22不满足条件,则也要调整------------
if a12-a22<=g
a12 = a12 + s;
a22 = a22 - s;
end
tem(1,1) = a11;
tem(1,2) = a12;
tem(2,1) = a21;
tem(2,2) = a22;
loca(k:k+1,kk:kk+1) = tem;
end
index = index + 1;
tem(1,1) = a11;
tem(1,2) = a12;
tem(2,1) = a21;
tem(2,2) = a22;
loca(k:k+1,kk:kk+1) = tem;
end
end
原文地址: https://www.cveoy.top/t/topic/jpOA 著作权归作者所有。请勿转载和采集!