??? Error using ==> bin2dec at 54 Binary string may consist only of characters 0 and 1

6 views (last 30 days)
function[stegoimage,insarray,er,psnr]=embedding(coverimage,wr) text=readsecret(); wr=.45 coverimage=imread('print2.jpg'); stegoimage=coverimage; imshow(coverimage); secret=text2binary(text); secretmessage=str2matrix(secret); numberofelements=numel(coverimage); lengthofsecret=length(secretmessage); k=numberofelements/7; k=round(k); if((k*7)>(numberofelements)); k=k-1; end insarray=zeros(1,k); [rows,cols]=size(coverimage); wetmatrix=findwetpixels(rows,cols,wr); [r,g]=classifications(); arr=1; i=1; j=1; while(i<=lengthofsecret); if((i+6)<=lengthofsecret) s=secretmessage(i:i+6); else lastbits=secretmessage(i:lengthofsecret); len=length(lastbits); for x=(len+1):7; lastbits(x)=0; end s=lastbits; end if(j+6<=numberofelements); pixelgroup=stegoimage(j:j+6); [class2status,rab]=class2(pixelgroup,wetmatrix(j:j+6),r,s); if(class2status); [class2status,gab]=class1(pixelgroup,wetmatrix(j:j+6),g,s); if(class1status); arr=arr+1; j=j+7; continue; else modifiedpixelgroup=changeLSB(pixelgroup,gab); stegoimage(j:j+6)=modifiedpixelgroup; insarray(arr)=1; arr=arr+1; j=j+7; i=i+3; if(i>lengthofsecret); insarray(arr-1)=insarray(arr-1)*-1; break; end end else modifiedpixelgroup=changeLSB(pixelgroup,Rab); stegoimage(j:j+6)=modifiedpielgroup; insarray(arr)=2; arr=arr+1; j=j+7; i=i+7; if(i>lengthofsecret); insarray(arr-1)=insarray(arr-1)*-1; break; end end else if(insarray(arr-1)==1) insarray(arr-1)=-1; elseif(insarray(arr-1)==2) insarray(arr-1)=-2; elseif(insarray(arr-1)==1) insarray(arr-1)=-1; elseif(insarray(arr-1)==2) insarray(arr-1)=-2; elseif(insarray(arr-1)==0) insarray(arr-1)=-99; end break; end end er=embeddedrate(i,numberofelements); psnr=psnr(coverimage,stegoimage); fprintf('number of bits embedded:%d\n',i); fprintf('------------------------------\n'); fprintf('wet rate: %f\n',wr); fprintf('embedded rate: %f\n',er); fprintf('psnr: %f\n',psnr); function secretmessage=str2matrix(secretmessage) secretmessage=double(secretmessage); lengthofsecret=length(secretmessage); for i=1:lengthofsecret; if(secretmessage(i)==48); secremessage(i)=0; else secretmessage(i)=1; end end end function a=findA(s) a=[s(3) s(5) s(6) s(7)]; a=mat2string(a); a=bin2dec(a); end function b=findB(s) b=[s(1) s(2) s(4)]; b=mat2string(b); b=bin2dec(b); end function g=findgroup(s) g=[s(1) s(2) s(3)]; g=mat2string(g); g=bin2dec(g); end function [changed]=comparethree(a,b,c) changed=false; if(a==b); if(b==c); changed=true; end end end function [pixelgroup]=changeLSB(pixelgroup,c) if(size(pixelgroup)==size(c)) for i=1:7 value=pixelgroup(i); if(rem(value,2)==0) if(c(i)==1) value=value+1; end else if(c(i)==0) value=value-1; end end pixelgroup(i)=value; end end end function[changed,rab]=class2(pixelgroup,wetgroup,r,s) c=LSBfunc(pixelgroup); a=findA(s); b=findB(s); a=a+1; b=b+1; rab=r(a,b); rab=dec2bin(rab,7); rab=str2matrix(rab); XOR=bitxor(c,rab); changed=false; for loop=1:7; if(comparethree(XOR(loop),wetgroup(loop),1)); changed=true; rturn; end end end function [channged,gab]=class1(pixelgroup,wetgroup,g,s) c=LSBfunc(pixelgroup); g=findgroup(s) g=g+1; for loop=1:16; changed=false; gab=g(g,loop); gab=dec2bin(gab,7); gab=str2matrix(gab); XOR=bitxor(c,gab); for i=1:7; if(comparethree(XOR(i),wetgroup(i),1)); changed=true; break; end end if(~changed) break; end end end function[t]=readsecret() t=''; fid=fopen('secret.txt'); tline=fgets(fid); t=horzcat(t,tline); while ischar(tline) tline=fgets(fid); t=horzcat(t,tline); end fclose(fid); end function[B]=text2binary(t) B=''; lengthoftext=length(t); for i=1:lengthoftext; t(i); b=dec2bin(double(t(i))); B=strcat(B,b); end end function[wetmatrix]=findwetpixels(rows,cols,wr) numberofelements=rows*cols; numberofwetpixels=round(numberofelements*wr); randompixels=randi(numberofelements,1,numberofwetpixels); wetpixelvalues=unique(randompixels); wetmatrix=zeros(rows,cols); %wetpixelvalues=zeros(rows,cols); loop=length(wetpixelvalues); if(loop==0) return; end for i=1:loop; wetmatrix(wetpixelvalues(i))=1; end end function[r,g]=classifications() r=[ 0 64 32 96 8 72 40 104; 105 41 73 9 97 33 65 1; 42 106 10 74 34 98 2 66; 67 3 99 35 75 11 107 43; 76 12 108 44 68 4 100 36; 37 101 5 69 45 109 13 77; 102 30 78 6 110 46 78 14; 15 79 47 111 7 71 39 103; 112 48 80 16 120 56 88 24; 25 89 57 121 17 81 49 113; 90 26 122 58 82 18 114 50; 51 115 19 83 59 123 27 91; 60 124 28 92 52 116 20 84; 85 21 117 53 93 29 125 61; 22 86 54 118 30 94 62 126; 127 63 95 31 119 55 87 23 ]; g=[ 0 105 42 67 76 37 102 15 112 25 90 51 60 85 22 127; 64 41 106 3 12 101 38 79 48 89 26 115 124 21 86 63; 32 73 10 99 108 5 70 47 80 57 122 19 28 117 54 95; 96 9 74 35 44 69 6 111 16 121 58 83 92 53 118 31; 8 97 34 75 68 45 110 7 120 17 82 59 52 93 30 111; 72 33 98 11 4 109 46 71 56 81 18 123 116 29 94 55; 40 65 2 107 100 13 78 39 88 49 114 27 20 125 62 87; 104 1 66 43 36 77 14 103 24 113 50 91 84 61 126 23 ]; disp(r); disp(g); end function lsbgroup=LSBfunc(pixelgroup) n=length(pixelgroup); lsbgroup=zeros(1,n); for i=1:n; lsbgroup(i)=rem(pixelgroup(i),2); end end function[string]=mat2string(matrix) temp=mat2str(matrix); string=''; loop=length(temp)-1; for i=2:loop; if(rem(i,2)==0); string=strcat(string,temp(i)); end end end
end
i have created aome tet file but it is showing the above error. how to rectify it the program is still incimplete
  1 Comment
Andreas Goser
Andreas Goser on 25 Feb 2014
Please format the code. Consider if you provide all the data to reproduce. Ideally debug down to a couple of lines including example data.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 25 Feb 2014
If you have a vector of decimal 0 and decimal 1, then use
char(YourVector + '0')
to get the characters '0' and '1'

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!