How to convert an image to binary matrix

2 visualizzazioni (ultimi 30 giorni)
I have an image P with dimension MxN. How to convert into dimension Mx8N ?
I have used P1 = dec2bin(P);
I am getting 65336x8 but I want 256x2048 how to do so ?

Risposta accettata

Walter Roberson
Walter Roberson il 25 Apr 2020
P1 = reshape( permute(reshape( (dec2bin(P, 8) - '0').', 8, size(P,1), size(P,2)), [2 1 3]), size(P,1), size(P,2)*8);
This preserves rows and expands each column to 8 columns -- so like
P(1,1)bit1 P(1,1)bit2 P(1,1)bit3 P(1,1)bit4 P(1,1)bit5 P(1,1)bit6 P(1,1)bit7 P(1,1)bit8 P(1,2)bit1 P(1,2)bit2 and so on
Another approach would be:
P1 = reshape( cat(3, bitget(P,8), bitget(P,7), bitget(P,6), bitget(P,5), bitget(P,4), bitget(P,3), bitget(P,2), bitget(P,1)), size(P,1), size(P,2)*8);
This preserves rows, and places all of the most significant bits together, then the second most significant, then the third most, and so on,
P(1,1)bit1 P(1,2)bit1 P(1,3)bit1 ... P(1,256)bit1 P(1,1)bit2 P(1,2)bit2 .. P(1,256)bit2 P(1,1)bit3 P(1,2)bit3 and so on

Più risposte (1)

KALYAN ACHARJYA
KALYAN ACHARJYA il 25 Apr 2020
Modificato: KALYAN ACHARJYA il 25 Apr 2020
>> ima=magic(3) % Lets suppose ima is a Image
ima =
8 1 6
3 5 7
4 9 2
>> result=dec2bin(ima,8)
result =
9×8 char array
'00001000'
'00000011'
'00000100'
'00000001'
'00000101'
'00001001'
'00000110'
'00000111'
'00000010'
>> data2=cellstr(result)
data2 =
9×1 cell array
'00001000'
'00000011'
'00000100'
'00000001'
'00000101'
'00001001'
'00000110'
'00000111'
'00000010'
>> result3=reshape(data2,[3,3])
result3 =
3×3 cell array
'00001000' '00000001' '00000110'
'00000011' '00000101' '00000111'
'00000100' '00001001' '00000010'
#If you further go for cell to array, then '00000010' represents as 10 only, hence I showed with cell array

Categorie

Scopri di più su Data Type Conversion in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by