13. matlab图像处理基础——图像加密、数组方式处理图像、图像置乱

1、图像加密

(1)异或的基本概念

  函数: c = xor(a, b)

(2)按位异或

  函数: c = bitxor(a, b)

%% 按位异或
a = 5;
b = 4;
c = bitxor(a,b);

(3)二值图像异或图像加密/解密

%% 二值图像按位异或
I = imread('lena.jpg');
I = im2bw(I); % 彩色图像转为二值图像
subplot(2,2,1),imshow(I),title('原始图像');
key = creatBI(0.98);
subplot(2,2,2),imshow(key),title('密钥图像');
secret_image = bitxor(I,key);
subplot(2,2,3),imshow(secret_image),title('加密图像');
recv_image = bitxor(secret_image,key);
subplot(2,2,4),imshow(recv_image),title('解密图像');

(4)灰度图像异或加密/解密

%% 灰度图像按位异或
I = imread('lena.jpg');
I = rgb2gray(I); % 彩色图像转为灰度图像
subplot(2,2,1),imshow(I),title('原始图像');
key = grayImage(0.98);
subplot(2,2,2),imshow(key),title('密钥图像');
secret_image = bitxor(I,key);
subplot(2,2,3),imshow(secret_image),title('加密图像');
recv_image = bitxor(secret_image,key);
subplot(2,2,4),imshow(recv_image),title('解密图像');

(5)彩色图像异或加密/解密

%% 彩色图像按位异或
I = imread('secret.png');
subplot(2,2,1),imshow(I),title('原始图像');
key = RgbImage(0.98,0.56,0.23);
subplot(2,2,2),imshow(key),title('密钥图像');
secret_image = bitxor(I,key);
subplot(2,2,3),imshow(secret_image),title('加密图像');
recv_image = bitxor(secret_image,key);
subplot(2,2,4),imshow(recv_image),title('解密图像');

 2、数组方式处理图像

(1)几何变换——自定义截取图像

%% 自定义截取子图像
I = imread('lena.jpg');
m = 100:300;
n = 100:300;
J = I(m,n);
subplot(1,2,1),imshow(I);
subplot(1,2,2),imshow(J);

(2)几何变换——移动

  新图像(X,Y) = 原图像(X+M, Y+N)

     

%% 移动
I = imread('lena.jpg');
I = rgb2gray(I);
[m,n] = size(I);
x = 201:m;
y = 101:n;
new = zeros(m,n);
new(x,y) = I(x-200,y-100);
subplot(1,2,1),imshow(I,[]);
subplot(1,2,2),imshow(new,[]);

(3)自定义均值去噪

%% 自定义均值去噪函数调用
I = imread('lena.jpg');
I = rgb2gray(I); % 彩色图像转换为灰度图像
J = imnoise(I,'salt & pepper');
subplot(1,2,1),imshow(J,[]);
[m,n] = size(J);
x = 2:m-1;
y = 2:n-1;
new = zeros(m,n);
o = double(J);
new(x,y) = (o(x-1,y)+o(x-1,y-1)+o(x-1,y+1)+o(x+1,y)+...
    o(x+1,y+1)+o(x+1,y-1)+o(x,y+1)+o(x,y-1))/8;
subplot(1,2,2),imshow(new,[]);

(4)自定义二值化去噪

  符合要求的下标 = find(条件)

%% 自定义二值化去噪
I = imread('lena.jpg');
I = rgb2gray(I); % 彩色图像转换为灰度图像
subplot(1,2,1),imshow(I,[]);
[m,n] = size(I);
new = zeros(m,n);
index = find(I>128);
new(index) = 255;
subplot(1,2,2),imshow(new,[]);

(5)自定义获取边缘

%% 自定义获取边缘
I = imread('cameraman.tif');
subplot(1,2,1),imshow(I,[]);
[m,n] = size(I);
x = 2:m-1;
y = 2:n-1;
key = 20; % 阈值
index = find(abs(I(x,y)-I(x-1,y))>key...
          | abs(I(x,y)-I(x,y-1))>key...
          | abs(I(x,y)-I(x+1,y))>key...
          |abs(I(x,y)-I(x,y+1))>key);
edge = ones(m-2,n-2);
edge(index) = 0;
subplot(1,2,2),imshow(edge,[]);

3、图像置乱

(1)置乱序列的生成

  ① 生成数据序列:混沌系统

  ② 生成置乱序列:

  

%% 置乱序列的生成
clc;
clear all;
x = linspace(0,0,20);
x(1) = 0.98;
for i = 2:20
    x(i) = 1-2*x(i-1)*x(i-1);
end
[a,b] = sort(x); % 生成置乱序列

(2)置乱序列的编码实现

%% 置乱序列的编码实现
clc;
clear all;
a = linspace(55,64,10);
c = linspace(0,0,10);
c(1) = 0.56;
for i = 2:10
    c(i) = 1-2*c(i-1)*c(i-1);
end
[cI,b] = sort(c);
i = 1:10;
ax(i) = a(b(i)); % b(i)是乱序序列,置乱
ay(b(i)) = ax(i); % 还原

(3)图像置乱

%% 图像置乱
a = imread('lena.jpg');
% a = im2bw(a); % 转成二值图像
[m,n] = size(a);
x = linspace(0,0,m*n);
x(1) = 0.23;
for i=2:m*n
    x(i) = 1-2*x(i-1)*x(i-1);
end
[xs,xIndex] = sort(x);
i = 1:m*n;
aSecret = a; % 设置初始大小
aDe = a; % 设置初始大小
aSecret(i) = a(xIndex(i));
aDe(xIndex(i)) = aSecret(i);
subplot(1,3,1),imshow(a);title('原始图像');
subplot(1,3,2),imshow(aSecret);title('置乱图像');
subplot(1,3,3),imshow(aDe);title('逆置乱图像');

posted @ 2020-05-17 10:57  红叶楠木  阅读(1758)  评论(2编辑  收藏  举报