# How I Can read 3 MSBs in RGB image

1 view (last 30 days)
Abduellah Elbakoush on 2 Jan 2022
Edited: DGM on 2 Jan 2022
e.g the Bit in image is 250 =11111010
I want to red the 3 bits of Most Significit bit =111
How I can do that

DGM on 2 Jan 2022
Edited: DGM on 2 Jan 2022
Depends what you want the output to be. It might be simple for scalar inputs:
% assume uint8
pixel = uint8(250);
% using dec2bin() to get a character vector
binarypixel = dec2bin(pixel)
binarypixel = '11111010'
msb3 = binarypixel(1:3)
msb3 = '111'
% using bitget() to get a logical vector
msb3_2 = bitget(pixel,8:-1:6)
msb3_2 = 1×3
1 1 1
... but array inputs will need to be handled differently.
% if you want to get bit planes of an array, you can use bitget()
bit8 = bitget(pixel,8)
bit8 = uint8 1
bit7 = bitget(pixel,7)
bit7 = uint8 1
bit6 = bitget(pixel,6)
bit6 = uint8 1
% or just use basic math
bit8 = mod(pixel,256)>=128
bit8 = logical
1
bit7 = mod(pixel,128)>=64
bit7 = logical
1
bit6 = mod(pixel,64)>=32
bit6 = logical
1
Pay attention to the output class and scaling here. Bitget() is returning a uint8() output, while the latter is returning actual logical outputs.