www.pudn.com > Watermark.rar > 方案一.m, change:2009-06-25,size:11951b


bjay_addr='小波神经网络程序图片\bjay.bmp'; 
bjay=imread(bjay_addr,'bmp'); 
%把RGB图片转换为灰度图片 
map=rgb2gray(bjay); 
bjay=double(map)/255; 
%把灰度图转换为二值图片 
%阈值为0.6 
bjay=im2bw(bjay,0.6); 
%imshow(bjay); 
[row1,col1]=size(bjay); 
 
%把逻辑值变为数值,并且把0转为-1 
bjay0=zeros(row1,col1); 
for i=1:row1 
    for j=1:col1 
        if bjay(i,j)==1 
            bjay0(i,j)=1; 
        end 
        if bjay(i,j)==0 
            bjay0(i,j)=-1; 
        end 
    end 
end 
 
 
  
  
 %通过坐标进行矩阵变换 
 m1=4; 
 n1=4; 
 m=fix(row1/m1); 
 n=fix(col1/n1); 
 med=zeros(m1,n1); 
 P0=zeros(m1*n1,m*n); 
 for i=1:m 
     for j=1:n 
         for i1=1:m1; 
             for j1=1:n1; 
         med(i1,j1)=bjay0(4*i-3+i1-1,4*j-3+j1-1); 
             end 
         end 
         %把矩阵med变换成1列 
        P0(:,n*(i-1)+j)=reshape(med,n1*m1,1); 
     end 
 end 
         
    %设计hipfield网络 
      net=newhop(P0); 
       
      %水印图像置乱 
      T=[1,1;1,2]; 
W=zeros(row1,col1,4); 
for k=1:4 
    bjay0_temp=bjay0; 
   for i=1:row1 
    for j=1:col1 
        ss=[i;j]; 
        ss1=T^k*ss; 
        ss1=mod(ss1,row1); 
       % ss1=ss1+1; 
        if ss1(1,1)==0 
      ss1(1,1)=48; 
  end 
   if ss1(2,1)==0 
      ss1(2,1)=48; 
  end 
        bjay0_temp(ss1(1,1),ss1(2,1))=bjay0(i,j); 
    end 
   end 
   W(:,:,k)=bjay0_temp; 
end 
 
 
% W1=reshape(W(:,:,1),1,row1*col1); 
W1=W(:,:,1); 
% W2=reshape(W(:,:,2),1,row1*col1); 
W2=W(:,:,2); 
% W3=reshape(W(:,:,3),1,row1*col1); 
W3=W(:,:,3); 
% W4=reshape(W(:,:,4),1,row1*col1); 
W4=W(:,:,4); 
 
%测试置乱效果  
%for k=1:4 
 %   for i=1:row1 
  %    for j=1:col1 
   %      if W(i,j,k)==-1 
    %        W(i,j,k)=0; 
    %    end 
    %  end 
   % end 
% end 
 
%对原始图像进行2尺度小波分解 
image1_addr='F:\zwgzwg\www\小波神经网络程序图片\lenna.bmp'; 
image1=imread(image1_addr,'bmp'); 
wavelet='db1'; 
level=2; 
image1=double(image1)/255; 
image1=image1*255; 
[C,S]=wavedec2(image1,level,wavelet); 
CA2=appcoef2(C,S,wavelet,level); 
CH2=detcoef2('h',C,S,level); 
CV2=detcoef2('v',C,S,level); 
CD2=detcoef2('d',C,S,level); 
 
%低频嵌入 
[CA2_row,CA2_col]=size(CA2); 
[CH2_row,CH2_col]=size(CH2); 
[CV2_row,CV2_col]=size(CV2); 
[CD2_row,CD2_col]=size(CD2); 
 
 CA2r=fix(CA2_row/row1); 
 CA2c=fix(CA2_col/col1); 
% CA2=sort(reshape(CA2,1,CA2r*CA2c),'descend'); 
 
%低频CA处理 
a1=0.5; 
CA2r=2; 
CA2c=2; 
 
CA20=zeros(1,CA2r*CA2c); 
for k1=1:row1 
   for k2=1:col1 
     for i=1:CA2r 
        for j=1:CA2c 
           % CA20(CA2c((CA2c*k-1+i-1)-1)+j)=CA2(CA2r*k-1+i-1,CA2c*k-1+j-1); 
            CA20(CA2c*(i-1)+j)=CA2(CA2r*k1-1+i-1,CA2c*k2-1+j-1); 
        end 
     end 
    [ca20,ind] =sort(CA20,'descend'); 
    index=0; 
    for i=1:CA2r 
        for j=1:CA2c 
            if CA2(CA2r*k1-1+i-1,CA2c*k2-1+j-1)== CA20(ind(1)) 
                % CA2(CA2r*k1-1+i-1,CA2c*k2-1+j-1)=CA2(CA2r*k1-1+i-1,CA2c*k2-1+j-1)+a1*W1((k1-1)*col1+k2); 
               CA2(CA2r*k1-1+i-1,CA2c*k2-1+j-1)=CA2(CA2r*k1-1+i-1,CA2c*k2-1+j-1)+a1*W1(k1,k2); 
                index=1; 
                break 
            end 
        end 
            if index==1 
                break 
           end 
    end 
  
   end 
end 
 
 
     
%高频CH处理 
a2=0.7; 
CH2r=2; 
CH2c=2; 
 
CH20=zeros(1,CH2r*CH2c); 
for k1=1:row1 
 for k2=1:col1 
     for i=1:CH2r 
        for j=1:CH2c 
           % CH20(CH2c((CH2c*k-1+i-1)-1)+j)=CH2(CH2r*k-1+i-1,CH2c*k-1+j-1); 
            CH20(CH2c*(i-1)+j)=CH2(CH2r*k1-1+i-1,CH2c*k2-1+j-1); 
        end 
     end 
    [ch20,ind] =sort(CH20,'descend'); 
    index=0; 
    for i=1:CH2r 
        for j=1:CH2c 
            if CH2(CH2r*k1-1+i-1,CH2c*k2-1+j-1)== CH20(ind(2)) 
                CH2(CH2r*k1-1+i-1,CH2c*k2-1+j-1)=CH2(CH2r*k1-1+i-1,CH2c*k2-1+j-1)+a2*W2(k1,k2); 
                index=1; 
                break 
            end 
        end 
        if index==1 
            break 
        end 
    end 
 end 
end 
     
 
 %高频CV处理 
a3=0.7; 
CV2r=2; 
CV2c=2; 
 
CV20=zeros(1,CV2r*CV2c); 
for k1=1:row1 
 for k2=1:col1 
     for i=1:CV2r 
        for j=1:CV2c 
           % CV20(CV2c((CV2c*k-1+i-1)-1)+j)=CV2(CV2r*k-1+i-1,CV2c*k-1+j-1); 
            CV20(CV2c*(i-1)+j)=CV2(CV2r*k1-1+i-1,CV2c*k2-1+j-1); 
        end 
     end 
    [cv20,ind] =sort(CV20,'descend'); 
    index=0; 
    for i=1:CV2r 
        for j=1:CV2c 
            if CV2(CV2r*k1-1+i-1,CV2c*k2-1+j-1)== CV20(ind(3)) 
                CV2(CV2r*k1-1+i-1,CV2c*k2-1+j-1)=CV2(CV2r*k1-1+i-1,CV2c*k2-1+j-1)+a3*W3(k1,k2); 
                index=1; 
                break 
            end 
        end 
        if index==1 
            break 
        end 
    end 
 end 
end 
  
     
  %高频CD处理 
a4=0.9; 
CD2r=2; 
CD2c=2; 
 
CD20=zeros(1,CD2r*CD2c); 
for k1=1:row1 
 for k2=1:col1 
     for i=1:CD2r 
        for j=1:CD2c 
           % CD20(CD2c((CD2c*k-1+i-1)-1)+j)=CD2(CD2r*k-1+i-1,CD2c*k-1+j-1); 
            CD20(CD2c*(i-1)+j)=CD2(CD2r*k1-1+i-1,CD2c*k2-1+j-1); 
        end 
     end 
    [cd20,ind] =sort(CD20,'descend'); 
    index=0; 
    for i=1:CD2r 
        for j=1:CD2c 
            if CD2(CD2r*k1-1+i-1,CD2c*k2-1+j-1)== CD20(ind(4)) 
                CD2(CD2r*k1-1+i-1,CD2c*k2-1+j-1)=CD2(CD2r*k1-1+i-1,CD2c*k2-1+j-1)+a4*W4(k1,k2); 
                index=1; 
                break 
            end 
        end 
        if index==1 
            break 
        end 
    end 
 end 
end 
     
CA2=reshape(CA2,CA2_row*CA2_col,1); 
CA2=CA2'; 
C(1:CA2_row*CA2_col)=CA2; 
 
CH2=reshape(CH2,CH2_row*CH2_col,1); 
CH2=CH2'; 
C(CA2_row*CA2_col+1:2*CH2_row*CH2_col)=CH2; 
 
CV2=reshape(CV2,CV2_row*CV2_col,1); 
CV2=CV2'; 
C(2*CA2_row*CA2_col+1:3*CV2_row*CV2_col)=CV2; 
 
CD2=reshape(CD2,CD2_row*CD2_col,1); 
CD2=CD2'; 
C(3*CA2_row*CA2_col+1:4*CD2_row*CD2_col)=CD2; 
 
%小波逆比变 
Xr=waverec2(C,S,wavelet); 
 
 
 
 
 
 
 
 
 
for i=1:384 
    for j=1:384 
        if Xr(i,j)>image1(i,j) 
            Xr(i,j)=ceil(Xr(i,j)); 
        else  
            if Xr(i,j)<image1(i,j) 
                  Xr(i,j)=fix(Xr(i,j)); 
            end 
        end 
    end 
end 
 
 
 % Xr=uint8(Xr); 
% imwrite(Xr,'F:\zwgzwg\www\小波神经网络程序图片\水印lenna.bmp','bmp') 
%jimage2_addr='F:\zwgzwg\www\小波神经网络程序图片\水印lenna.bmp'; 
%jimage2=imread(jimage2_addr,'bmp'); 
%wavelet='db1'; 
%level=2; 
%jimage2=double(jimage2)/255; 
%jimage2=jimage2*255; 
%Xr=jimage2; 
 
 
 
[C1,S1]=wavedec2(Xr,level,wavelet); 
jCA2r=appcoef2(C1,S1,wavelet,level); 
jCH2r=detcoef2('h',C1,S1,level); 
jCV2r=detcoef2('v',C1,S1,level); 
jCD2r=detcoef2('d',C1,S1,level); 
 
 jimage1_addr='F:\zwgzwg\www\小波神经网络程序图片\lenna.bmp'; 
jimage1=imread(jimage1_addr,'bmp'); 
wavelet='db1'; 
level=2; 
jimage1=double(jimage1)/255; 
jimage1=jimage1*255; 
[C2,S2]=wavedec2(jimage1,level,wavelet); 
jCA2=appcoef2(C2,S2,wavelet,level); 
jCH2=detcoef2('h',C2,S2,level); 
jCV2=detcoef2('v',C2,S2,level); 
jCD2=detcoef2('d',C2,S2,level); 
 
  
CA2r=2; 
CA2c=2; 
 
row1=48; 
col1=48; 
 
jCA20=zeros(1,CA2r*CA2c); 
jW1=zeros(row1,col1); 
for k1=1:row1 
   for k2=1:col1 
     for i=1:CA2r 
        for j=1:CA2c 
           % CA20(CA2c((CA2c*k-1+i-1)-1)+j)=CA2(CA2r*k-1+i-1,CA2c*k-1+j-1); 
            jCA20(CA2c*(i-1)+j)=jCA2(CA2r*k1-1+i-1,CA2c*k2-1+j-1); 
        end 
     end 
    [jca20,ind] =sort(jCA20,'descend'); 
    index=0; 
    for i=1:CA2r 
        for j=1:CA2c 
            if jCA2(CA2r*k1-1+i-1,CA2c*k2-1+j-1)== jCA20(ind(1)) 
                jW1(k1,k2)=jCA2r(CA2r*k1-1+i-1,CA2c*k2-1+j-1)-jCA2(CA2r*k1-1+i-1,CA2c*k2-1+j-1); 
                index=1; 
                break 
            end 
        end 
            if index==1 
                break 
           end 
    end 
  
   end 
end 
 
CH2r=2; 
CH2c=2; 
jCH20=zeros(1,CH2r*CH2c); 
jW2=zeros(row1,col1); 
for k1=1:row1 
   for k2=1:col1 
     for i=1:CH2r 
        for j=1:CH2c 
           % CH20(CH2c((CH2c*k-1+i-1)-1)+j)=CH2(CH2r*k-1+i-1,CH2c*k-1+j-1); 
            jCH20(CH2c*(i-1)+j)=jCH2(CH2r*k1-1+i-1,CH2c*k2-1+j-1); 
        end 
     end 
    [jch20,ind] =sort(jCH20,'descend'); 
    index=0; 
    for i=1:CH2r 
        for j=1:CH2c 
            if jCH2(CH2r*k1-1+i-1,CH2c*k2-1+j-1)== jCH20(ind(2)) 
                jW2(k1,k2)=jCH2r(CH2r*k1-1+i-1,CH2c*k2-1+j-1)-jCH2(CH2r*k1-1+i-1,CH2c*k2-1+j-1); 
                index=1; 
                break 
            end 
        end 
            if index==1 
                break 
           end 
    end 
  
   end 
end 
 
 
CV2r=2; 
CV2c=2; 
jCV20=zeros(1,CV2r*CV2c); 
jW3=zeros(row1,col1); 
for k1=1:row1 
   for k2=1:col1 
     for i=1:CV2r 
        for j=1:CV2c 
           % CV20(CV2c((CV2c*k-1+i-1)-1)+j)=CV2(CV2r*k-1+i-1,CV2c*k-1+j-1); 
            jCV20(CV2c*(i-1)+j)=jCV2(CV2r*k1-1+i-1,CV2c*k2-1+j-1); 
        end 
     end 
    [jcv20,ind] =sort(jCV20,'descend'); 
    index=0; 
    for i=1:CV2r 
        for j=1:CV2c 
            if jCV2(CV2r*k1-1+i-1,CV2c*k2-1+j-1)== jCV20(ind(3)) 
                jW3(k1,k2)=jCV2r(CV2r*k1-1+i-1,CV2c*k2-1+j-1)-jCV2(CV2r*k1-1+i-1,CV2c*k2-1+j-1); 
                index=1; 
                break 
            end 
        end 
            if index==1 
                break 
           end 
    end 
  
   end 
end 
 
CD2r=2; 
CD2c=2; 
jCD20=zeros(1,CD2r*CD2c); 
jW4=zeros(row1,col1); 
for k1=1:row1 
   for k2=1:col1 
     for i=1:CD2r 
        for j=1:CD2c 
           % CD20(CD2c((CD2c*k-1+i-1)-1)+j)=CD2(CD2r*k-1+i-1,CD2c*k-1+j-1); 
            jCD20(CD2c*(i-1)+j)=jCD2(CD2r*k1-1+i-1,CD2c*k2-1+j-1); 
        end 
     end 
    [jcd20,ind] =sort(jCD20,'descend'); 
    index=0; 
    for i=1:CD2r 
        for j=1:CD2c 
            if jCD2(CD2r*k1-1+i-1,CD2c*k2-1+j-1)== jCD20(ind(4)) 
                jW4(k1,k2)=jCD2r(CD2r*k1-1+i-1,CD2c*k2-1+j-1)-jCD2(CD2r*k1-1+i-1,CD2c*k2-1+j-1); 
                index=1; 
                break 
            end 
        end 
            if index==1 
                break 
           end 
    end 
  
   end 
end 
 
a1=0.5; 
a2=0.7; 
a3=0.7; 
a4=0.9; 
 
 
 
 
 
 
 
  T=[1,1;1,2]; 
jW=zeros(row1,col1,4); 
jW(:,:,1)=jW1; 
jW(:,:,2)=jW2; 
jW(:,:,3)=jW3; 
jW(:,:,4)=jW4; 
jW_temp=jW; 
 
 
%nbh=mod(mod(mod(mod(T^20,48)^3,48)^3,48)*T^7,48); 
for k=1:4 
    switch k 
        case 1 
            k1=0; 
            k2=0; 
            k3=0; 
            k4=1; 
        case 2 
            k1=0; 
            k2=0; 
            k3=1; 
            k4=1; 
        case 3 
            k1=1; 
            k2=1; 
            k3=0; 
            k4=1; 
        otherwise 
            k1=1; 
            k2=1; 
            k3=1; 
            k4=1; 
    end 
     
   for i=1:row1 
     for j=1:col1 
        ss=[i;j]; 
        ss1=T^(-k1)*T^(-k2)*T^(-k3)*T^(-k4)*ss; 
        ss1=mod(ss1,row1); 
        %ss1=ss1+1; 
         if ss1(1,1)==0 
            ss1(1,1)=48; 
         end 
            if ss1(2,1)==0 
               ss1(2,1)=48; 
            end 
       jW(ss1(1,1),ss1(2,1),k)=jW_temp(i,j,k); 
    end 
   end 
end     
 
jW(:,:,1)=jW(:,:,1)/0.5; 
jW(:,:,2)=jW(:,:,2)/0.7; 
jW(:,:,3)=jW(:,:,3)/0.7; 
jW(:,:,4)=jW(:,:,4)/0.9; 
 
for k=1:4 
   for i=1:48 
      for j=1:48 
        if jW(i,j,k)<0 
           jW(i,j,k)=-1; 
        else 
            jW(i,j,k)=1; 
        end 
      end 
   end 
end 
 
 
 
jW1=jW(:,:,1); 
jW2=jW(:,:,2); 
jW3=jW(:,:,3); 
jW4=jW(:,:,4); 
 
 
 JW=(0.5*jW1+0.7*jW2+0.7*jW3+0.9*jW4)/4;  
         for i=1:48 
             for j=1:48 
                 if JW(i,j)>0 
                      JW(i,j)=1; 
                 else 
                       
                         JW(i,j)=-1; 
                      
                 end 
             end 
         end 
          
          
net1=newhop(bjay0); 
net2=newhop(bjay0'); 
 JWr=sim(net1,{48,10},{},{JW}); 
  XX0=cell2mat(JWr(10)); 
  JWr=sim(net2,{48,10},{},{XX0'}); 
   XX=cell2mat(JWr(10))'; 
   
  for i=1:48 
             for j=1:48 
                 if XX(i,j)>0 
                      XX(i,j)=1; 
                 else 
                       
                       XX(i,j)=0; 
                      
                 end 
             end 
  end 
         imshow(XX);