Monday, August 2, 2010

Simple Image Scrambling

clear all;
close all;
clc;
a = imread('cameraman.tif');
a = im2double(a);
[m n]=size(a);
% for i=1:m
%     for j=1:n
%         a(i,j)=a(i,j).*255;
%     end
% end
s = input('Enter the size to split :');
ps = input('Enter Password:','s');
cnt = length(ps);
%%%%$$$$$$$$$$$$$$FINDING THE KEY $$$$$$$$$$$$$$$$$$$$$$$$$$
T=0;
for i=1:cnt
T=T+double(ps(i));
end
%%%%$$$$$$$$$$$$$$$$$$$$$$$$$$$$ SPLITING THE IMAGE $$$$$$$$$$$$$$$
s1=m/s;
s2=n/s;
le = s1*s2;
c = cell(s1,s2);
c1 = cell(s1,s2);
c2 = cell(s1,s2);
c3 = cell(s1,s2);
i1=0;
for i=1:s:m
i1=i1+1;j1=0;
for j=1:s:n
j1=j1+1;
k1=0;
for k=i:((i+s)-1)
k1=k1+1;p1=0;
for p=j:((j+s)-1)
p1=p1+1;
if k<=m&p<=n
a2(k1,p1)=a(k,p);
end
end
end
c{i1,j1}=a2;
end
end
%%%%$$$$$$$$$$$$$$$$$$$$$$$ FINDING THE BLOCK DIFFERENCE $$$$$$$$$$$
i2=1;
for i=1:s1
for j=1:s2
e=c{i,j};
j2=j+1;
if (i2<= s1 & j2 <= s2)
e1=c{i2,j2};
elseif (i2<=s1 & j2 > s2)
i2=i2+1;
j2=1;
if (i2 <= s1)&(j2 <=s2)
e1=c{i2,j2};
end
if (i2>s1)&(j2 <=s2)
i2=1;
e1=c{i2,j2};
end
end
[m1 n1]=size(e);
for k=1:m1
for p=1:n1
blodiff(k,p)=abs(e(k,p)-e1(k,p));
end
end
c1{i,j}=blodiff;
end
end
%%%%%$$$$$$4 FIND THE BLOCK TOTAL $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
for i = 1:s1
for j=1:s2
btot = 0;
for k=1:m1
for p=1:n1
btot = btot+c1{i,j}(k,p);
end
end
btot1(i,j)=btot;
clear btot;
end
end
%%%%%$$$$$$$$$$$$$ SUBTRACT BLOCK TOTAL WITH BLOCK PIXEL VALUE $$$$$$$$$
for i=1:s1
for j=1:s2
bt=btot1(i,j);
for k=1:m1
for p=1:n1
c2{i,j}(k,p)=bt - c{i,j}(k,p);
end
end
end
end
%%%%$$$$$$$$$$$$$$$$$ GENERATE RANDOM NUMBER $$$$$$$$$$$$$$$
rand('state',T);
[h g]=sort(rand(1,le));
re = g;
co=0;
for i=1:s1
for j=1:s2
co =co+1;
sm(i,j)=re(co);
end
end
%%%$$$$$$$$$$$$ SCRAMBLE THE IMAGE $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
[s1 s2] = size(c2);
for i=1:s1
for j=1:s2
rv = sm(i,j);
co=0;
for k=1:s1
for p=1:s2
co=co+1;
if co == rv;
c3{k,p}=c2{i,j};
end
end
end
end
end
%%%%%%$$$$$$$$$$$$$$ MERGE INTO SINGLE IMAGE $$$$$$$$$$$$$$$$$$$$$$$$$$
ii=1;jj=1;
for i=1:s1
for j=1:s2
im12 = c3{i,j};
for k=1:m1
for p=1:n1
oimag(((k+ii)-1),((p+jj)-1))=im12(k,p);
end
end
jj=jj+s;
end
ii=ii+s;
jj=1;
end
figure,imshow(a),title('ORIGINAL IMAGE');
figure,imshow(oimag),title('AFTER SCRAMBLE');
% imwrite(oimag,'scrcameraman.tif');
B25 = max(max(oimag));
M25 = min(min(oimag));
opimag = scramb_rev(oimag);
% for i=1:m
%     for j=1:n
%         re45im(i,j)=oimag(i,j)/255;
%     end
% end
% figure,imshow(re45im);

No comments: