for i = 1:count if str2num(msg(i,1)) == 1 wx(row(i),col(i)) = x(row(i),col(i)) + oplength; end end imwrite(wx,map,'lenaindex1.bmp','bmp'); figure; subplot(1,2,1);imshow('lenaindex.bmp');title('原始图像'); subplot(1,2,2);imshow('lenaindex1.bmp');title('携密图像');
%读入载体图像 [x,map] = imread('lenaindex1.bmp','bmp'); key = 1234; count = 96; for i = 1:count msg(i, 1) = 0; end
% 找到扩展起始颜色位置 oplength = 64;
[row col] = randselect(x,count,key); for i = 1:count if x(row(i),col(i))>oplength msg(i,1) = 1; else msg(i,1) = 0; end end out = bit2str(msg); fid = fopen('message.txt','wt'); fwrite(fid,out); fclose(fid);
function [row col] = randselect(x,count,key) [m,n] = size(x); distance1 = ceil(m*n/count); distance2 = distance1 - 2; if distance2 == 2 error('载体太小'); end rand('state',key); a = rand(1,count); row = zeros([1 count]); col = zeros([1 count]); r = 1; c = 1; row(1,1) = r; col(1,1) = c; for i=2:count if a(i)>=0.5 c=c+distance1; else c=c+distance2; end if c>n r=r+1; if r>m error('载体太小'); end c = mod(c,n); if c==0 c=1; end end row(1,i)=r; col(1,i)=c; end
4、bit2str
1 2 3 4 5 6 7
function [msg] = bit2str(bin) [row col] = size(bin); bin = reshape(bin,[8 row/8]); bin = bin'; bin = num2str(bin); msg = bin2dec(bin); end
5、str2bit
1 2 3 4 5
function [bin] = str2bit(msg) bin = dec2base(msg,2,8); [row col] = size(bin); bin = reshape(bin.',[1 row*col]); end