Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 856
MBL Avatar asked Oct 05 '22 08:10

MBL


1 Answers

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);
like image 166
Eitan T Avatar answered Oct 10 '22 03:10

Eitan T