I want to convert 183x275x3 matrix of binary bits to decimal bits...bin2dec can not be use..so how should I proceed?

1 view (last 30 days)
[filename,filepath]=uigetfile({'matlab.jpg'},'C:\Users\anush\OneDrive\Desktop');
selectedImage2 = imread(strcat(filepath, filename));
imshow(selectedImage2);
B2 = dec2bin(selectedImage2);
C2 = reshape(B2',1,numel(B2));
b2= round(selectedImage2./256);
X2 = xor(b2, B2(1:length(b2)));
Y2 = xor(b2, X2);
this is my code for encryption and dcryption of an image
  4 Comments
Walter Roberson
Walter Roberson on 25 Mar 2020
B2 = dec2bin(selectedImage2, 8);
Now B2 would be (183*275*3) x 8 and would consist of '0' and '1' (the characters, not the numeric values 0 and 1) .
Your X2 and Y2 would both be class logical: is that your intention? The xor() operation is
xor = @(A,B) (A~=0|B~=0) & ~(A~=0&B~=0)
and neither '0' nor '1' are equal to 0, so both '0' and '1' are considered the same for the purpose of xor. You might perhaps have wanted a bitwise xor. You have some datatype issues to work through as you do the calculation.
Anushree Gupta
Anushree Gupta on 25 Mar 2020
basically what i want is to convert Y2 in image as its an image bits...but i m getting idea how i proceed
this Xor() operation should i use this elaborately on my code.
i want to convert Y2 from binary to decimal...which is not possible by bin2dec as it works only till 53 digits.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 25 Mar 2020
B2 = dec2bin(selectedImage2, 8) - '0';
to get numeric values. Use bitxor() instead of xor() .
back_to_numeric = reshape(bin2dec(char(reshape(Y2, 8, []) + '0').'), size(selectedImage2));
back_to_numeric = cast(back_to_numeric, class(selectedImage2));
  2 Comments
Anushree Gupta
Anushree Gupta on 26 Mar 2020
I did all this...but its showing error:
Product of known dimensions, 8, not divisible into total number of elements, 150975.
I used bitxor() also in place of xor()
Walter Roberson
Walter Roberson on 26 Mar 2020
Your encryption algorithm is faulty.
b2= round(selectedImage2./256);
gives one bit for each array element in selectedImage2, but B2 has 8 bits for each selected element in selectedImage2, so you have a mismatch in sizes when you go to do the xor. Your code gets around that by taking only a subset of B2 -- note that length(B2) means:
if isempty(B2)
length is 0
else
length is max(size(B2))
end
so indexing B2(1:length(B2)) does not extract all of B2. It happens to get the first column of B2 in this case.
Then with you working with only a portion of B2, you do not have enough bits to reconstruct the image.

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!