Borrar filtros
Borrar filtros

How to convert an image to binary matrix

2 visualizaciones (últimos 30 días)
Abhishek Bakhla
Abhishek Bakhla el 25 de Abr. de 2020
Editada: MUHAMMAD ISLAM el 29 de Sept. de 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 ?

Respuesta aceptada

Walter Roberson
Walter Roberson el 25 de Abr. de 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

Más respuestas (1)

KALYAN ACHARJYA
KALYAN ACHARJYA el 25 de Abr. de 2020
Editada: KALYAN ACHARJYA el 25 de Abr. de 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 comentarios
Abhishek Bakhla
Abhishek Bakhla el 28 de Abr. de 2020
thank you.
MUHAMMAD ISLAM
MUHAMMAD ISLAM el 29 de Sept. de 2021
Editada: MUHAMMAD ISLAM el 29 de Sept. de 2021
Brother you saved me <3

Iniciar sesión para comentar.

Categorías

Más información sobre Data Type Conversion en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by