Filter löschen
Filter löschen

How to convert an image to binary matrix

5 Ansichten (letzte 30 Tage)
Abhishek Bakhla
Abhishek Bakhla am 25 Apr. 2020
Bearbeitet: MUHAMMAD ISLAM am 29 Sep. 2021
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 ?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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

Weitere Antworten (1)

KALYAN ACHARJYA
KALYAN ACHARJYA am 25 Apr. 2020
Bearbeitet: KALYAN ACHARJYA am 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
  2 Kommentare
Abhishek Bakhla
Abhishek Bakhla am 28 Apr. 2020
thank you.
MUHAMMAD ISLAM
MUHAMMAD ISLAM am 29 Sep. 2021
Bearbeitet: MUHAMMAD ISLAM am 29 Sep. 2021
Brother you saved me <3

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by