Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
suahnkim committed Jul 27, 2019
1 parent 2318eca commit ea7ab48
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 91 deletions.
Binary file removed Kodak images.zip
Binary file not shown.
37 changes: 37 additions & 0 deletions main.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function main
%Image
image=double(rgb2gray(imread('Kodak/kodim01.png')));

%Payload
payload_length=25000; %number of bits to be embedded
payload=randi([0,1],payload_length,1);

%Embedding
[rdh_image, ~, ~, ~,embedding_capacity_left]=mbp(image,payload);
if embedding_capacity_left < 0
disp('Failed embedding')
else
disp(['Can embed ' num2str(embedding_capacity_left) ' bits more (estimated)'])
end

%Recovery check
[payload_rec, re_image] = mbp_recovery(rdh_image);
if isequal(re_image,image)
disp('Original image recovered')
else
disp('Failed to recover the original image')
end

if isequal(payload_rec,payload)
disp('Payload recovered')
else
disp('Failed to recover the payload')
end

%show the image
close all
figure(1)
imshow(uint8(image))
figure(2)
imshow(uint8(rdh_image))
end
133 changes: 42 additions & 91 deletions mbp.m
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
function [rdh_image, image_recovery_check, iteration_max, EC_list, LM_size_list]=mbp(image,iteration_max)
function [rdh_image, iteration_max, EC_list, LM_size_list, embedding_capacy_left]=mbp(image, actual_payload, iteration_max)
switch nargin
case 1
case 2
iteration_max = 300;
end
%Preprocess Payload (length appended)
image_size=size(image);
payload_length_max=2*ceil(log2(image_size(1)*image_size(2)+1));
actual_payload=[de2bi(length(actual_payload),payload_length_max)'; actual_payload];


rng(0);
%image = image(5:end-5,5:end-5);
Expand All @@ -21,6 +26,7 @@
P_c=0;
iteration=0;
tic
payload_total=[];
while true
%Adaptive peak selection
P_s_previous=P_s;
Expand All @@ -40,7 +46,7 @@
%Embedding capacity + stop condition check
H_P_s=sum(image_hor==P_s);

if H_P_s-sum(image_hor(1:16)==P_s) < length(LM)+32 | iteration == iteration_max %Stop condition reached
if H_P_s-sum(image_hor(1:16)==P_s) < length(LM)+32 || iteration == iteration_max %Stop condition reached
%no need to update iteration
P_s=P_s_previous;
P_c=P_c_previous;
Expand All @@ -60,7 +66,20 @@
image_hor = ref_image_hor(17:end,iteration);
H_P_s=sum(image_hor==P_s);
LM=(image_hor(image_hor==P_c | image_hor==P_c-d)==P_c-d);
payload =randi([0,1],H_P_s-length(LM)-32,1);

payload_total(end-payload_length_last+1:end)=[];
if length(payload_total) < length(actual_payload)
payload_left_over=length(actual_payload)-length(payload_total);
if payload_left_over < H_P_s-length(LM)-32
synthetic_payload =randi([0,1],payload_left_over);
payload = [actual_payload(length(payload_total)+1:end) synthetic_payload];
else
payload = actual_payload(length(payload_total)+1:length(payload_total)+H_P_s-length(LM)-32);
end
else
payload =randi([0,1],H_P_s-length(LM)-32,1);
end

message=[LM ; de2bi(P_s_previous,8)'; de2bi(P_c_previous,8)';original_16_lsb;payload];
EC_list(iteration)=H_P_s-length(LM)-32;
LM_size_list(iteration)=length(LM);
Expand All @@ -87,10 +106,26 @@
EC_list(iteration_max+1:end)=[];
LM_size_list(iteration_max+1:end)=[];
ref_image_hor(:,iteration_max+1:end) = [];
payload_total=[payload_total; payload];
embedding_capacy_left=length(payload_total)-length(actual_payload);
break

else
payload =randi([0,1],H_P_s-length(LM)-16,1);
if length(payload_total) < length(actual_payload)
payload_left_over=length(actual_payload)-length(payload_total);
if payload_left_over < H_P_s-length(LM)-16
synthetic_payload =randi([0,1],H_P_s-length(LM)-16-payload_left_over,1);
payload = [actual_payload(length(payload_total)+1:end); synthetic_payload];
else
payload = actual_payload(length(payload_total)+1:length(payload_total)+H_P_s-length(LM)-16);
end
else
payload =randi([0,1],H_P_s-length(LM)-16,1);
end

payload_length_last=length(payload);
payload_total=[payload_total; payload];

message=[LM ; de2bi(P_s_previous,8)'; de2bi(P_c_previous,8)';payload];
iteration=iteration+1;
EC_list(iteration)=H_P_s-length(LM)-16;
Expand All @@ -102,7 +137,7 @@
end
message_whole=zeros(length(image_hor),1);
message_whole(image_hor==P_s)=message;


%Combine P_c with its neighbor
image_hor(image_hor==P_c-d)=image_hor(image_hor==P_c-d)+d;
Expand All @@ -121,92 +156,8 @@
toc
% figure(1);imshow(uint8(image));figure(2);imshow(uint8(reshape(ref_image_hor(:,end),image_size(1),image_size(2))))
% figure(3);histogram(image,256);figure(4);histogram(ref_image_hor(:,iteration),256)
rdh_image=reshape(image_hor,image_size(1),image_size(2));

tic
% Reverse Operation
first_16_pixels_rec=bitxor(image_hor(1:16),mod(image_hor(1:16),2));
P_s_rec=bi2de(mod(image_hor(1:8)',2));
P_c_rec=bi2de(mod(image_hor(9:16)',2));
% disp("P_s")
% isequal(P_s,P_s_rec)
% disp("P_c")
% isequal(P_c,P_c_rec)
if P_s_rec < P_c_rec %RHS
d = 1;
else %LHS
d = -1;
end

%Undo first iteration
image_hor=image_hor(17:end);
%Extract Payload + side information
message_rec=(image_hor(image_hor==P_s_rec |image_hor==P_s_rec+d)==P_s_rec+d);
LM_size=sum(image_hor==P_c_rec);
LM_rec=message_rec(1:LM_size);
% disp("LM")
% isequal(LM,LM_rec)
P_s_p_rec=bi2de(message_rec(1+LM_size:8+LM_size)');
P_c_p_rec=bi2de(message_rec(9+LM_size:16+LM_size)');
first_16_pixels_rec=bitxor(first_16_pixels_rec,message_rec(17+LM_size:32+LM_size));
payload_rec=message_rec(33+LM_size:end);

%Shift back
if d == 1
image_hor(image_hor > P_s_rec & image_hor < P_c_rec)=image_hor(image_hor > P_s_rec & image_hor < P_c_rec)-d; %RHS
else
image_hor(image_hor < P_s_rec & image_hor > P_c_rec)=image_hor(image_hor < P_s_rec & image_hor > P_c_rec)-d; %LHS
end
%Undo location map
image_hor(image_hor==P_c_rec)=image_hor(image_hor==P_c_rec)-d*LM_rec;
image_hor=[first_16_pixels_rec; image_hor];

% disp("image_hor")
% isequal(image_hor,ref_image_hor(:,iteration))

iteration=iteration-1;
%Undo rest of the iteration
P_s_rec=P_s_p_rec;
P_c_rec=P_c_p_rec;

% disp("P_s")
% isequal(P_s_rec,P_s_list(iteration+1))
% disp("P_c")
% isequal(P_c_rec,P_c_list(iteration+1))

while (P_s_rec ~= 0 | P_c_rec ~= 0)
if P_s_rec < P_c_rec %RHS
d = 1;
else %LHS
d = -1;
end

%Extract Payload + side information
message_rec=(image_hor(image_hor==P_s_rec |image_hor==P_s_rec+d)==P_s_rec+d);
LM_size=sum(image_hor==P_c_rec);
LM_rec=message_rec(1:LM_size);
P_s_p_rec=bi2de(message_rec(1+LM_size:8+LM_size)');
P_c_p_rec=bi2de(message_rec(9+LM_size:16+LM_size)');
payload_rec=message_rec(17+LM_size:end);

%Shift back
if d == 1
image_hor(image_hor > P_s_rec & image_hor < P_c_rec)=image_hor(image_hor > P_s_rec & image_hor < P_c_rec)-d; %RHS
else
image_hor(image_hor < P_s_rec & image_hor > P_c_rec)=image_hor(image_hor < P_s_rec & image_hor > P_c_rec)-d; %LHS
end
%Undo location map
image_hor(image_hor==P_c_rec)=image_hor(image_hor==P_c_rec)-d*LM_rec;
P_s_rec=P_s_p_rec;
P_c_rec=P_c_p_rec;

iteration=iteration-1;
% disp("image_hor")
% isequal(image_hor,ref_image_hor(:,iteration+1))
% [P_s_rec P_c_rec]
end
image_recovery_check=isequal(reshape(image_hor,image_size(1),image_size(2)),image);
disp("Decoding time")
toc
end

function P_c=find_P_c(table,P_s)
Expand Down
92 changes: 92 additions & 0 deletions mbp_recovery.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
function [payload_rec, re_image]=mbp_recovery(rdh_image)
tic
image_size = size(rdh_image);
image_hor = reshape(rdh_image,image_size(1)*image_size(2),1);
% ref_image_hor(:,1) = reshape(rdh_image,image_size(1)*image_size(2),1);

% Reverse Operation
first_16_pixels_rec=bitxor(image_hor(1:16),mod(image_hor(1:16),2));
P_s_rec=bi2de(mod(image_hor(1:8)',2));
P_c_rec=bi2de(mod(image_hor(9:16)',2));
% disp("P_s")
% isequal(P_s,P_s_rec)
% disp("P_c")
% isequal(P_c,P_c_rec)
if P_s_rec < P_c_rec %RHS
d = 1;
else %LHS
d = -1;
end

%Undo first iteration
image_hor=image_hor(17:end);
%Extract Payload + side information
message_rec=(image_hor(image_hor==P_s_rec |image_hor==P_s_rec+d)==P_s_rec+d);
LM_size=sum(image_hor==P_c_rec);
LM_rec=message_rec(1:LM_size);
% disp("LM")
% isequal(LM,LM_rec)
P_s_p_rec=bi2de(message_rec(1+LM_size:8+LM_size)');
P_c_p_rec=bi2de(message_rec(9+LM_size:16+LM_size)');
first_16_pixels_rec=bitxor(first_16_pixels_rec,message_rec(17+LM_size:32+LM_size));
payload_rec=message_rec(33+LM_size:end);

%Shift back
if d == 1
image_hor(image_hor > P_s_rec & image_hor < P_c_rec)=image_hor(image_hor > P_s_rec & image_hor < P_c_rec)-d; %RHS
else
image_hor(image_hor < P_s_rec & image_hor > P_c_rec)=image_hor(image_hor < P_s_rec & image_hor > P_c_rec)-d; %LHS
end
%Undo location map
image_hor(image_hor==P_c_rec)=image_hor(image_hor==P_c_rec)-d*LM_rec;
image_hor=[first_16_pixels_rec; image_hor];

% disp("image_hor")
% isequal(image_hor,ref_image_hor(:,iteration))

%Undo rest of the iteration
P_s_rec=P_s_p_rec;
P_c_rec=P_c_p_rec;

% disp("P_s")
% isequal(P_s_rec,P_s_list(iteration+1))
% disp("P_c")
% isequal(P_c_rec,P_c_list(iteration+1))

while (P_s_rec ~= 0 || P_c_rec ~= 0)
if P_s_rec < P_c_rec %RHS
d = 1;
else %LHS
d = -1;
end

%Extract Payload + side information
message_rec=(image_hor(image_hor==P_s_rec |image_hor==P_s_rec+d)==P_s_rec+d);
LM_size=sum(image_hor==P_c_rec);
LM_rec=message_rec(1:LM_size);
P_s_p_rec=bi2de(message_rec(1+LM_size:8+LM_size)');
P_c_p_rec=bi2de(message_rec(9+LM_size:16+LM_size)');
payload_rec=[ message_rec(17+LM_size:end); payload_rec];

%Shift back
if d == 1
image_hor(image_hor > P_s_rec & image_hor < P_c_rec)=image_hor(image_hor > P_s_rec & image_hor < P_c_rec)-d; %RHS
else
image_hor(image_hor < P_s_rec & image_hor > P_c_rec)=image_hor(image_hor < P_s_rec & image_hor > P_c_rec)-d; %LHS
end
%Undo location map
image_hor(image_hor==P_c_rec)=image_hor(image_hor==P_c_rec)-d*LM_rec;
P_s_rec=P_s_p_rec;
P_c_rec=P_c_p_rec;
% disp("image_hor")
% isequal(image_hor,ref_image_hor(:,iteration+1))
% [P_s_rec P_c_rec]
end
re_image=reshape(image_hor,image_size(1),image_size(2));
payload_length_max=2*ceil(log2(image_size(1)*image_size(2)+1));
payload_length=bi2de(payload_rec(1:payload_length_max)');
payload_rec(1:payload_length_max)=[];
payload_rec(payload_length+1:end)=[];
toc
end

0 comments on commit ea7ab48

Please sign in to comment.