dec2base working differently on a matrix than on individual values
2 views (last 30 days)
Show older comments
Vincent Crasborn
on 13 Feb 2020
Commented: Vincent Crasborn
on 13 Feb 2020
I'm trying to encrypt some images (as an example, not for real use), and part of the method uses DNA encryption. For this, I have to convert the uint8 matrix that the image greyscale values are stored in (for R, G, and B, I separate them beforehand) to binary.
The code I'm running is:
message_Image = imread ('baboon.tiff'); % reads in the image
message_Image_R = message_Image(:,:,1); % Separates the red channel
message_Image_R_bin = dec2base(message_Image_R,2,8) % Converts values to binary
The value of message_Image_R(1,1) is 164 (a uint8)
The first value that message_Image_R_bin prints out in the command window upon execution is '01010100'. This, however, is 84 (164 in binary is '10100100')
When execute dec2base(164,2,8), it results in '10100100'. Thus the code works fine on the individual number, but not on the matrix.
What am I doing wrong here?
2 Comments
Giuseppe Inghilterra
on 13 Feb 2020
Hi, I tried on my own and it seems that dec2base function works. Check again your result or could you please attach your .tiff file or .mat file with message_R matrix?
Accepted Answer
Guillaume
on 13 Feb 2020
From your screenshot, it looks like you've typed:
>> message_Image_R_bin
at the command window and scrolled back up until the window stopped scrolling. If you're using matlab default settings, the command window only retain the last 5,000 lines. Considering that your char array has 262,144 lines, you're nowhere near the 1st element.
>> message_Image_R_bin(1, :) %look at 1st row
ans =
'10100100'
dec2base works correctly once you look at the correct element.
Note: you could use dec2bin instead of dec2base, which saves you from specifying the base.
1 Comment
More Answers (1)
Steven Lord
on 13 Feb 2020
That's not the first element in M. That's the first element that the Command Window scroll buffer contains. Previous rows in the output of dec2bin scrolled out of the buffer.
If you were to display dec2bin(M) the display would contain 262,144 rows, one row per element in M. The first line would look like this (which I generated using a 10-by-10 random M in release R2019b.)
>> M = randi([intmin('uint8'), intmax('uint8')], 10, 10);
>> dec2bin(M)
ans =
100×8 char array
'10100101'
You don't have such a header on your display. The default Command Window scroll buffer length is 5000 if I recall correctly; you can see this by opening up the Preferences in the Environment section of the Home tab on the Toolstrip. The scroll buffer preference is in the Command Window section under MATLAB in the Preferences page.
If we generate a larger M, store the output of dec2bin into a variable and display the first few rows we should see that it corresponds to the first elements in M.
>> M = randi([intmin('uint8'), intmax('uint8')], 512, 512);
>> D = dec2bin(M);
>> M(1)
ans =
96
>> D(1, :)
ans =
'01100000'
>> bin2dec(D(1, :))
ans =
96
Looks correct to me. We can check the whole matrix by reversing the process.
>> isequal(M, reshape(bin2dec(D), size(M)))
ans =
logical
1
2 Comments
Steven Lord
on 13 Feb 2020
As Guillaume correctly pointed out, you were using dec2base but the same explanation applies to dec2base as to dec2bin.
See Also
Categories
Find more on Logical 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!