【图像加密】Arnold置乱和混沌加密-MATLAB代码

Arnold

Arnold.m

function [img_new] = Arnold(a,b,h,w,n,img)
%     h=64;
%     w=64;
    img_new = zeros(h,w) ;    %[h, w]=size(img)
%     a = 5;
%     b = 6;
%     n = 16; %n为置乱轮数
    N = h;
    for i=1:n
        for y=1:h
            for x=1:w
                xx=mod((x-1)+b*(y-1),N)+1;  %a,b可提前指定
                yy=mod(a*(x-1)+(a*b+1)*(y-1),N)+1;
                img_new(yy,xx)=img(y,x);
            end
        end
    end
end

reverse_Arnold.m

function [img_new] = reverse_Arnold(a,b,h,w,n,img)
    img_new = zeros(h,w) ;    %[h, w]=size(img)
    N = h;
    for i=1:n
        for y=1:h
            for x=1:w            
                xx=mod((a*b+1)*(x-1)-b*(y-1),N)+1;
                yy=mod(-a*(x-1)+(y-1),N)+1  ;        
                img_new(yy,xx)=img(y,x);                   
            end
        end
    end
end

test_Arnold.m

%% 测试Arnold变换
A=imread('13.png'); 
h = size(A,1);
w = h;
a = 5;
b = 6;
n = 16; %n为置乱轮数

embed_temp = Arnold(a,b,h,w,n,A); %Arnold 置乱

% 反Arnold置乱
re = reverse_Arnold(a,b,h,w,n,embed_temp);

figure,imshow(uint8(embed_temp));title('加密图像')  %输出得到的加密图像 
figure,imshow(uint8(re));title('解密图像')  %输出得到的解密图像

Logistic

logistic.m

function [A1,sumA] = logistic(img)
    A=img; 
    [M,N]=size(A);%原始图像A的尺寸一MxN 
    A1 = zeros(M,N);
    
    u1=4;
    u2=4;
    x1(1)=0.2;
    x2(1)=0.7; 
    sumA=sum(A(:));  
    k=mod(sumA,256)*1.0/255; 
    x1(1)=(x1(1)+k)/2; 
    x2(1)=(x2(1)+k)/2;  
    y1(1)=(1/3.1415926)*asin(sqrt(x1(1))); 
    y2(1)=(1/3.1415926)*asin(sqrt(x2(1)));  
    for i=1:1:M*N-1 %产生2个Logistic混沌序列   
        x1(i+1)=u1*x1(i)*(1-x1(i));   
        x2(i+1)=u2*x2(i)*(1-x2(i)); 
    end
    for i=1:1:M*N  %改造2个Logistic,得到两个y序列
        y1(i)=(1/3.1415926)*asin(sqrt(x1(i)));   
        y2(i)=(1/3.1415926)*asin(sqrt(x2(i))); 
    end
    
    n=1;  
    for i=1:1:M   %由yl和y2序列对原图像进行值替代加密     
        for j=1:1:N        
            if mod(n,1)==0           
                k(n)=mod(floor(y1(n)*10^15),256);       
            else
                k(n)=mod(floor(y2(n)*10^15),256);       
            end
            A1(i,j)=bitxor(A(i,j),k(n));  %得到加密像素      A1混沌加密图像
            n=n+1;     
        end
    end
    A1 = uint8(A1);
end

relogistic.m

function [A2] = relogistic(A,sumA)
    [M,N]=size(A);%原始图像A的尺寸一MxN 
    u1=4;
    u2=4;
    x1(1)=0.2;
    x2(1)=0.7; 
%     sumA=sum(sum(A));  % 这里需要用原始图像的像素值和
%     sumA=19548964;
    k=mod(sumA,256)*1.0/255; 
    x1(1)=(x1(1)+k)/2; 
    x2(1)=(x2(1)+k)/2;  
    y1(1)=(1/3.1415926)*asin(sqrt(x1(1))); 
    y2(1)=(1/3.1415926)*asin(sqrt(x2(1)));  

    for i=1:1:M*N-1 %产生2个Logistic混沌序列   
        x1(i+1)=u1*x1(i)*(1-x1(i));   
        x2(i+1)=u2*x2(i)*(1-x2(i)); 
    end
    for i=1:1:M*N  %改造2个Logistic,得到两个y序列
          y1(i)=(1/3.1415926)*asin(sqrt(x1(i)));   
          y2(i)=(1/3.1415926)*asin(sqrt(x2(i))); 
    end

    n=1;  
    for i=1:1:M     
        for j=1:1:N          
            if mod(n,1)==0             
                k(n)=mod(floor(y1(n)*10^15),256);         
            else
                k(n)=mod(floor(y2(n)*10^15),256);         
            end
            A2(i,j)=bitxor(A(i,j),k(n));  %得到加密像素      
            n=n+1;     
        end
    end
%     figure,imshow(A2);title('解密图像')  %输出得到的解密图像
end

test_logistic.m

%加密方法,混沌序列  https://www.cnblogs.com/fanglijiao/p/11206151.html 
% 含有两个自变量的混沌系统是二维的

% clc,clear all,
A=imread('13.png'); 
imshow(A); title('原图') 

[A1,sumA] = logistic(A);
figure,imshow(A1);title('混沌加密图像')  %输出得到的加密图像 

decry = relogistic(A1,sumA);
% % A2 = uint8(A2);

% figure,imshow(encry);title('加密图像')  %输出得到的加密图像 
figure,imshow(decry);title('解密图像')  %输出得到的解密图像

你可能感兴趣的:(图像隐写,matlab,计算机视觉,图像处理)