MATLAB实现(1)利用Arnold置乱一级小波分解后的低频分量,得到密文图像。 (2)利用Arnold置乱二级小波分解后的低频分量,得到密文图像。 要求:给出代码,显示原始图像、密文图像。
时间: 2024-05-15 07:14:30 浏览: 84
以下是MATLAB代码实现:
一级小波分解:
```matlab
% 读入原始图像
img = imread('lena.bmp');
% 将图像转为灰度图
img = rgb2gray(img);
% 进行一级小波分解
[cA,cH,cV,cD] = dwt2(img,'haar');
% 将低频分量进行Arnold置乱
N = 512; % 图像大小
n = 1; % 置乱次数
x = 0:N-1;
y = 0:N-1;
[X,Y] = meshgrid(x,y);
for i = 1:n
X = mod(X+Y,N);
Y = mod(X+2*Y,N);
end
% 将置乱后的低频分量转为整数
cA = uint8(cA);
X = uint8(X);
Y = uint8(Y);
% 对低频分量进行像素置乱
for i = 1:N
for j = 1:N
x = X(i,j)+1;
y = Y(i,j)+1;
cA_new(x,y) = cA(i,j);
end
end
% 将置乱后的低频分量与高频分量合并,得到密文图像
cA_new = double(cA_new);
cH = double(cH);
cV = double(cV);
cD = double(cD);
img_enc = idwt2(cA_new,cH,cV,cD,'haar');
% 显示原始图像和密文图像
subplot(1,2,1);
imshow(img);
title('原始图像');
subplot(1,2,2);
imshow(uint8(img_enc));
title('一级小波分解后的密文图像');
```
二级小波分解:
```matlab
% 读入原始图像
img = imread('lena.bmp');
% 将图像转为灰度图
img = rgb2gray(img);
% 进行二级小波分解
[cA2,cH2,cV2,cD2] = dwt2(img,'haar');
[cA1,cH1,cV1,cD1] = dwt2(cA2,'haar');
% 将低频分量进行Arnold置乱
N = 256; % 图像大小
n = 1; % 置乱次数
x = 0:N-1;
y = 0:N-1;
[X,Y] = meshgrid(x,y);
for i = 1:n
X = mod(X+Y,N);
Y = mod(X+2*Y,N);
end
% 将置乱后的低频分量转为整数
cA1 = uint8(cA1);
X = uint8(X);
Y = uint8(Y);
% 对低频分量进行像素置乱
for i = 1:N
for j = 1:N
x = X(i,j)+1;
y = Y(i,j)+1;
cA1_new(x,y) = cA1(i,j);
end
end
% 将置乱后的低频分量与高频分量合并,得到一级分解后的密文图像
cA1_new = double(cA1_new);
cH1 = double(cH1);
cV1 = double(cV1);
cD1 = double(cD1);
img_enc1 = idwt2(cA1_new,cH1,cV1,cD1,'haar');
% 将一级分解后的低频分量进行二级小波分解
[cA2_new,cH2_new,cV2_new,cD2_new] = dwt2(cA1_new,'haar');
% 将二级分解后的低频分量进行Arnold置乱
N = 128; % 图像大小
n = 1; % 置乱次数
x = 0:N-1;
y = 0:N-1;
[X,Y] = meshgrid(x,y);
for i = 1:n
X = mod(X+Y,N);
Y = mod(X+2*Y,N);
end
% 将置乱后的低频分量转为整数
cA2_new = uint8(cA2_new);
X = uint8(X);
Y = uint8(Y);
% 对低频分量进行像素置乱
for i = 1:N
for j = 1:N
x = X(i,j)+1;
y = Y(i,j)+1;
cA2_new2(x,y) = cA2_new(i,j);
end
end
% 将置乱后的低频分量与高频分量合并,得到密文图像
cA2_new2 = double(cA2_new2);
cH2_new = double(cH2_new);
cV2_new = double(cV2_new);
cD2_new = double(cD2_new);
img_enc2 = idwt2(cA2_new2,cH2_new,cV2_new,cD2_new,'haar');
% 显示原始图像和密文图像
subplot(1,2,1);
imshow(img);
title('原始图像');
subplot(1,2,2);
imshow(uint8(img_enc2));
title('二级小波分解后的密文图像');
```
注意:在进行像素置乱时,需要将低频分量转为整数类型,否则像素置乱会出现问题。
阅读全文