フィルターのクリア

How to convert an image to binary matrix

2 ビュー (過去 30 日間)
Abhishek Bakhla
Abhishek Bakhla 2020 年 4 月 25 日
編集済み: MUHAMMAD ISLAM 2021 年 9 月 29 日
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 ?

採用された回答

Walter Roberson
Walter Roberson 2020 年 4 月 25 日
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
  1 件のコメント
Abhishek Bakhla
Abhishek Bakhla 2020 年 4 月 28 日
thank you.

サインインしてコメントする。

その他の回答 (1 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2020 年 4 月 25 日
編集済み: KALYAN ACHARJYA 2020 年 4 月 25 日
>> 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 件のコメント
Abhishek Bakhla
Abhishek Bakhla 2020 年 4 月 28 日
thank you.
MUHAMMAD ISLAM
MUHAMMAD ISLAM 2021 年 9 月 29 日
編集済み: MUHAMMAD ISLAM 2021 年 9 月 29 日
Brother you saved me <3

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by