5

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.

2
  • Did you try specifying 'uint8' instead of 'uint16', obtaining the result and then dividing it into two images? Or alternatively splitting your current result into two images using bitwise operations? Commented Jan 17, 2013 at 16:32
  • I need to extract both bytes from the uint16 array - would reading as uint8 achieve this? (I'm more interested in the data contained in the second byte though) Commented Jan 17, 2013 at 16:34

1 Answer 1

4

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);
Sign up to request clarification or add additional context in comments.

6 Comments

img2 = bitsra(img, 8); would be better.
That seems to have done it. I'm only really interested in the MSB which looks like it comes with reasonable values using img1 = bitand(A, 255); %// 0x00FF. Cheers
@MBL I think the img1 is LSB, not MSB. The img2 is MSB.
@Masi That is what I stated in the answer, no?
@EitanT Yes, your answer is correct. MBL comments different in the second comment: MSB --- img1.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.