Result of .img file readed by Matlab is different from SNAP

I use SNAP read terrasar .cos file, and create a subet which is named i_VV.img, the result shows like:

then I use Matlab read the same i_VV.img, but there is no object showed, the Matlab code is:

close all;

filename = 'i_VV.img';

% data = multibandread(filename, [243, 382, 1], 'int16', 0, 'bsq', 'ieee-le');

fid = fopen(filename);
data = fread(fid, [243, 382], 'int16');
% fclose(fid)

figure; imshow(mat2gray(data));

the result of Matlab like:

It seems like the nosie, I try different params, but there are not correct result.

Not only the show img, the values are also different in the pixels. For example,
In SNAP, I select the pixel located in (381,0) > right click > Copy Pixel-Info to Clipboard, the result:

Product:	i_VV.img

Image-X:	381	pixel
Image-Y:	0	pixel

BandName	Value	Unit
i_VV:	46		

the value is 46, but in matlab, the data[1, 382] is -15870.
So I want to know how matlab could read the .img file, then letting the value is same to the vaule in SNAP.

The is here: (128.4 KB)

I think you need to add more of the metadata from the .hdr file. Matlab enviinfo should be used to pass the metadata to the file reader.

1 Like

Thankssss! the enviinfo function solve my problem, and I read .img file correctly. Nice! The correct code is:

data = multibandread('i_VV.img', [243, 382, 1], 'int16', 0, 'bsq', 'ieee-be');

the byte-order only is the different filed. So I have another problem, how can I get the byte-order from .hdr, the content of .hdr like below, and the byte-order is ieee-be:

description = {Level 1B Product - Unit: real}
samples = 382
lines = 243
bands = 1
header offset = 0
file type = ENVI Standard
data type = 2
interleave = bsq
byte order = 1
band names = { i_VV }
data gain values = {1.0}
data offset values = {0.0}

Thank you again.

1 Like

byte order ‘0’ is ieee-le, ‘1’ is ieee-be

1 Like

ENVI HDR file documentation.

1 Like