How to convert .hdr image data set to .mat?

22 views (last 30 days)
Please help to save my hdr image data into .mat file.
  5 Comments
Voss
Voss on 4 Feb 2022
Edited: Voss on 4 Feb 2022
OK. The hdr file looks like a text file that looks like this:
ENVI
description = {
File Resize Result, x resize factor: 1.000000, y resize factor: 1.000000.
[Wed Feb 02 15:19:23 2022]}
samples = 361
lines = 392
bands = 6
header offset = 0
file type = ENVI Standard
data type = 12
interleave = bsq
sensor type = Unknown
byte order = 0
x start = 3349
y start = 3650
map info = {UTM, 1.000, 1.000, 642525.000, 2403045.000, 3.0000000000e+001, 3.0000000000e+001, 45, North, WGS-84, units=Meters}
coordinate system string = {PROJCS["WGS_1984_UTM_Zone_45N",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",87.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]}
wavelength units = Unknown
band names = {
Resize (Layer (Band 1:LC08_L2SP_138045_20161121_20200905_02_T1_SR_B1.TIF):image),
Resize (Layer (Band 1:LC08_L2SP_138045_20161121_20200905_02_T1_SR_B2.TIF):image),
Resize (Layer (Band 1:LC08_L2SP_138045_20161121_20200905_02_T1_SR_B3.TIF):image),
Resize (Layer (Band 1:LC08_L2SP_138045_20161121_20200905_02_T1_SR_B4.TIF):image),
Resize (Layer (Band 1:LC08_L2SP_138045_20161121_20200905_02_T1_SR_B5.TIF):image),
Resize (Layer (Band 1:LC08_L2SP_138045_20161121_20200905_02_T1_SR_B6.TIF):image)}
What information do you want out of that and how should it be stored in the mat file? If you want to store it just as it is, you can do this:
fid = fopen('resizeimage.hdr');
data = char(fread(fid).');
fclose(fid);
save('resizeimage.mat','data');

Sign in to comment.

Accepted Answer

DGM
DGM on 4 Feb 2022
Edited: DGM on 4 Feb 2022
You can use multibandread() to read these types of data files.
In order to get all those parameters, you need to read the .hdr file first. You could use enviinfo(). Similarly, you could use hypercube() to do both without needing multibandread().
I don't have enviinfo() or hypercube(), and I'm not climbing a mountain in order to install either. I just manually fetched the parameters from the header file. It's a plain text file, and the parameter translation into MATLAB terms is explained here:
headerfile = 'resizeimage.hdr'; % normally you'd use something to read this to get the parameters
datafile = 'resizeimage.bsq'; % i added the extension
datasize = [392 361 6]; % [lines samples bands]
dataprec = 'uint16'; % data type = 12
dataoffset = 0;
interleaveopt = 'bsq';
endianopt = 'ieee-le'; % byte order = 0
A = multibandread(datafile,datasize,dataprec,dataoffset,interleaveopt,endianopt);
imshow(A(:,:,1),[]) % show layer 1
In lieu of manually parsing each header file, you could probably write a simple replacement for enviinfo() relatively easily.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!