-
Notifications
You must be signed in to change notification settings - Fork 3
/
MarkovLSBMAMG.m
117 lines (108 loc) · 2.95 KB
/
MarkovLSBMAMG.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
%%嵌入算法的核心部分
%BinaryList指二进制文件,Position是指待嵌入的像素点位置向量,GrayMatrix指灰度图像矩阵
%EMPictureMatrix嵌入二进制文件后的图片二维矩阵
function [EMPictureMatrix,R] = MarkovLSBMAMG(BinaryList,Position,GrayMatrix)
B = BinaryList;
M = getMC(GrayMatrix);%经验矩阵的获取
R = zeros(256,256);%记录矩阵的生成
P = GrayMatrix+1;%用以记录矩阵
PO = GrayMatrix;
NP = PO;
if length(B)>length(Position)
disp('error:');
disp('The length of binary list is larger than picture matrix!');
else
for k = 1:length(B)
t = Position(k);%待嵌入像素点的位置
%%记录矩阵边界设置
if P(t)<=254&&P(t+1)<=255
m = R(P(t+1)+1,P(t));
n = R(P(t+1)+1,P(t)+2);
elseif P(t)>254&&P(t+1)<=255
m = R(P(t+1)+1,P(t));
n = 0;
elseif P(t)<=254&&P(t+1)>255
m = 0;
n = 0;
end
if m>n
NP(t) = Embedder1(PO(t),B(k));
elseif m<n
NP(t) = Embedder2(PO(t),B(k));
else
NP(t) = Embedder3(PO(t),B(k));
end
if(NP(t)-PO(t)==1&&t>1)
M(P(t+1)+1,P(t)+1) = M(P(t+1)+1,P(t)+1) - 1;
M(P(t)+1,P(t-1)+1) = M(P(t)+1,P(t-1)+1) - 1;
M(P(t+1)+1,P(t)+2) = M(P(t+1)+1,P(t)+2) + 1;
M(P(t)+2,P(t-1)+1) = M(P(t)+2,P(t-1)+1) + 1;
R(P(t+1)+1,P(t)+1) = R(P(t+1)+1,P(t)+1) - 1;
R(P(t)+1,P(t-1)+1) = R(P(t)+1,P(t-1)+1) - 1;
R(P(t+1)+1,P(t)+2) = R(P(t+1)+1,P(t)+2) + 1;
R(P(t)+2,P(t-1)+1) = R(P(t)+2,P(t-1)+1) + 1;
end
if(NP(t)-PO(t)==-1&&t>1)
M(P(t+1)+1,P(t)+1) = M(P(t+1)+1,P(t)+1) - 1;
M(P(t)+1,P(t-1)+1) = M(P(t)+1,P(t-1)+1) - 1;
M(P(t+1)+1,P(t)) = M(P(t+1)+1,P(t)) + 1;
M(P(t),P(t-1)+1) = M(P(t),P(t-1)+1) + 1;
R(P(t+1)+1,P(t)+1) = R(P(t+1)+1,P(t)+1) - 1;
R(P(t)+1,P(t-1)+1) = R(P(t)+1,P(t-1)+1) - 1;
R(P(t+1)+1,P(t)) = R(P(t+1)+1,P(t)) + 1;
R(P(t),P(t-1)+1) = R(P(t),P(t-1)+1) + 1;
end
end
EMPictureMatrix = NP;
end
end
%%经验矩阵的构建
function MC_Matrix = getMC(GrayMatrix)
P = GrayMatrix+1;%matlab中位的记位是从1开始,以防角标出现0的情况
MC_Matrix = zeros(256,256);%%1-MC经验矩阵
for k=1:length(GrayMatrix)-1
MC_Matrix(P(k),P(k+1)) = MC_Matrix(P(k),P(k+1)) + 1 ;
end
end
%%3个嵌入器
function result = Embedder1(im,bin)
%%关键的一步,是开始就赋值
result = im;%%开始就赋值以保证parity(im)==bin时,不用改变
if(mod(im,2)~=bin)
if(im>=0&&im<255)
result = im + 1;
end
if(im==255)
result = im - 1;
end
end
end
function result = Embedder2(im,bin)
result = im;
if(mod(im,2)~=bin)
if(im>0&&im<=255)
result = im - 1;
end
if(im==0)
result = im + 1;
end
end
end
function result = Embedder3(im,bin)
result = im;
if(mod(im,2)~=bin)
if(im==255)
result = im - 1;
end
if(im==0)
result = im + 1;
end
if(im>0&&im<255)
if(randi([0,1],1)==1)
result = im - 1;
else
result = im + 1;
end
end
end
end