Effacer les filtres
Effacer les filtres

How to convert an image to binary matrix

2 vues (au cours des 30 derniers jours)
Abhishek Bakhla
Abhishek Bakhla le 25 Avr 2020
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 ?

Réponse acceptée

Walter Roberson
Walter Roberson le 25 Avr 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

Plus de réponses (1)

KALYAN ACHARJYA
KALYAN ACHARJYA le 25 Avr 2020
Modifié(e) : KALYAN ACHARJYA le 25 Avr 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
  2 commentaires
Abhishek Bakhla
Abhishek Bakhla le 28 Avr 2020
thank you.
MUHAMMAD ISLAM
MUHAMMAD ISLAM le 29 Sep 2021
Modifié(e) : MUHAMMAD ISLAM le 29 Sep 2021
Brother you saved me <3

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Type Conversion dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by