MBL
MBL

Reputation: 1227

MATLAB: Reading both Bytes of a Unsigned 16-bit binary file

I have a binary Band Sequential (1-band, BSQ file), which is an unsigned 16-bit (2-byte) integer.

Currently I'm reading the whole (image) through multibandread:

img=multibandread('IMAGE.bsq',[400 400 1],'uint16',0,'bsq','n');

What process in MATLAB would allow me to read both bytes individually? i.e. I would like to read the file into 2 new arrays in MATLAB e.g. byte1 (400x400x1) and byte2 (400x400x1).

Can this be achieved through fread? I note in the 'precision' section it is possible to skip source values (e.g. 'N*source=>output'), but I'm unsure of the exact process.

Upvotes: 5

Views: 1062

Answers (1)

Eitan T
Eitan T

Reputation: 32930

One way would be splitting your current img with bitwise operations. The LSB image would be:

img1 = bitand(img, 255);   %// 0x00FF

and the MSB image would be:

img2 = bitsra(img, 8);

Not mandatory, but maybe you'll also want to convert these into uint8s:

img1 = uint8(img1);
img2 = uint8(img2);

Upvotes: 4

Related Questions