彩色图像像素置换不可逆

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (109)

我使用二维正弦图对RGB图像进行彩色图像置换。该代码使用此映射为图像中的每个RGB通道执行像素位置排列。然后,我使用具有相同初始参数的相同映射来反转排列。问题是代码只对一个通道执行正确的反向置换,而其他通道未被恢复。查看排列图像和恢复图像的附加图像。

%% Image Encryption Demo - Encryption and Decryption
clear all
close all
clc

%% 1. Load plaintext images
% Image 1
I = imread('D:\1\1.jpg');
Ir = I(:,:,1);
Ig = I(:,:,2);
Ib = I(:,:,3);
K=1;
%% 2. Encryption
%[CI,K] = Logistic2D_ImageCipher(I,'encryption');
[CIb,K] = Logistic2D_ImageCipher(Ib,'encryption');
clearvars -except CIb Ir Ig Ib K I;
[CIr,K] = Logistic2D_ImageCipher(Ir,'encryption');
clearvars -except CIb CIr Ir Ig Ib K I;
[CIg,K] = Logistic2D_ImageCipher(Ig,'encryption');
clearvars -except CIb CIr CIg Ir Ig Ib K I;

CI = cat(3,CIr,CIg,CIb);
%% 3. Decryption
%DI = Logistic2D_ImageCipher(CI,'decryption',K);
DIb = Logistic2D_ImageCipher(CIb,'decryption',K);
clearvars -except CIb CIr CIg Ir Ig Ib K I DIb CI;
DIg = Logistic2D_ImageCipher(CIg,'decryption',K);
clearvars -except CIb CIr CIg Ir Ig Ib K I DIb DIg CI;
DIr = Logistic2D_ImageCipher(CIr,'decryption',K);
clearvars -except CIb CIr CIg Ir Ig Ib K I DIb DIg DIr CI;
DI = cat(3,DIr,DIg,DIb);
%% 4. Analaysis
% Histogram
%title('aaa');
figure,subplot(221),imshow(I,[]),subplot(222),imshow(CI,[])
subplot(223),imhist(I),subplot(224),imhist(CI)
title('aaa2');
figure,subplot(221),imshow(DI,[])

function varargout = Logistic2D_ImageCipher(P,para,K)

%% 1. Initialization
% 1.1. Genereate Random Key if K is not given
if ~exist('K','var') && strcmp(para,'encryption') 
    K = round(rand(1,256));
    varOutN = 2;
elseif ~exist('K','var') && strcmp(para,'decryption') 
    error('Cannot Complete Decryption without Encryption Key')
else
    varOutN = 1;
end
% 1.2. Translate K to map formats
transFrac = @(K,st,ed) sum(K(st:ed).*2.^(-(1:(ed-st+1))));
x0 = transFrac(K,1,52);
y0 = transFrac(K,53,104);
a=0.8;
b =0.3;

r = transFrac(K,105,156)*.08+1.11;
T = transFrac(K,157,208);
turb = blkproc(K(209:256),[1,8],@(x) bi2de(x));

MN = numel(P);

Logistic2D = @(x,y,a) [sin(pi*a*(y+3)*x*(1-x)), sin(pi*a*(x+3)*y*(1-y))];
format long eng
%% 2. Estimate cipher rounds
if max(P(:))>1
    F = 256;
    S = 4;
else
    F = 2;
    S = 32;
end
P = double(P);
iter = 1;
%iter = ceil(log2(numel(P))/log2(S));

%% 3. Image Cipher
C = double(P);
switch para
    case 'encryption'
        for i = 1:iter
            tx0 = mod(log(turb(mod(i-1,6)+1)+i)*x0+T,1);
            ty0 = mod(log(turb(mod(i-1,6)+1)+i)*y0+T,1);
            xy = zeros(MN,2);
            for n = 1:MN
                if n == 1
                    xy(n,:) = (Logistic2D(tx0,ty0,a)); 
                else
                    xy(n,:) = (Logistic2D(xy(n-1,1),xy(n-1,2),a));
                end
            end
            R = cat(3,reshape(xy(:,1),size(P,1),size(P,2)),reshape(xy(:,2),size(P,1),size(P,2)));
            C = LogisticPermutation(C,R,'encryption');
        end
    case 'decryption'
        for i = iter:-1:1
            tx0 = mod(log(turb(mod(i-1,6)+1)+i)*x0+T,1);
            ty0 = mod(log(turb(mod(i-1,6)+1)+i)*y0+T,1);
            xy = zeros(MN,2);
            for n = 1:MN
                if n == 1
                    xy(n,:) = (Logistic2D(tx0,ty0,a)); 
                else
                    xy(n,:) = (Logistic2D(xy(n-1,1),xy(n-1,2),a));
                end
            end
            R = cat(3,reshape(xy(:,1),size(P,1),size(P,2)),reshape(xy(:,2),size(P,1),size(P,2)));
            C = LogisticPermutation(C,R,'decryption'); 
        end
end

%% 4. Output
switch F
    case 2
        C = logical(C);
    case 256
        C = uint8(C);
end

switch varOutN
    case 1
        varargout{1} = C;
    case 2
        varargout{1} = C;
        varargout{2} = K;
end

function C = LogisticPermutation(P,R,para)

C0 = zeros(size(P));
C = C0;
switch para
    case 'encryption'
        % 1. Shuffling within a Column
        [v,Epix] = sort(R(:,:,1),1);
        for i = 1:size(R,1)
            C0(:,i) = P(Epix(:,i),i);
        end
        % 2. Shuffling within a Row
        [v,Epiy] = sort(R(:,:,2),2);
        for j = 1:size(R,2)
            C(j,:) = C0(j,Epiy(j,:));
        end
    case 'decryption'
        % 1. Shuffling within a Row
        [v,Epiy] = sort(R(:,:,2),2);
        for j = 1:size(R,2)
            C0(j,Epiy(j,:)) = P(j,:);
        end
        % 2. Shuffling within a Column
        [v,Epix] = sort(R(:,:,1),1);
        for i = 1:size(R,1)
            C(Epix(:,i),i) = C0(:,i);
        end
end
提问于
用户回答回答于
function q50823167
%% 1. Load plaintext images
% Image 1
I = imread(fullfile(matlabroot, 'examples', 'wavelet', 'mandrill.jpg'));
Ir = I(:,:,1);
Ig = I(:,:,2);
Ib = I(:,:,3);
%% 2. Encryption
%[CI,K] = Logistic2D_ImageCipher(I,'encryption');
[CIb,Kb] = Logistic2D_ImageCipher(Ib,'encryption');
[CIr,Kr] = Logistic2D_ImageCipher(Ir,'encryption');
[CIg,Kg] = Logistic2D_ImageCipher(Ig,'encryption');

CI = cat(3,CIr,CIg,CIb);
%% 3. Decryption
%DI = Logistic2D_ImageCipher(CI,'decryption',K);
DIb = Logistic2D_ImageCipher(CIb,'decryption',Kb);
DIg = Logistic2D_ImageCipher(CIg,'decryption',Kg);
DIr = Logistic2D_ImageCipher(CIr,'decryption',Kr);
DI = cat(3,DIr,DIg,DIb);
%% 4. Analaysis
% Histogram
%title('aaa');
figure,subplot(221),imshow(I,[]),subplot(222),imshow(CI,[])
subplot(223),imhist(I),subplot(224),imhist(CI)
title('aaa2');
figure,subplot(221),imshow(DI,[])

扫码关注云+社区

领取腾讯云代金券