-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathenviread.m
86 lines (71 loc) · 2.41 KB
/
enviread.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
function [im,jiDim,jiUL,resolu,ZC]=enviread(filename)
% any ENVI file uint8, int16, and uint16
% [im,jiDim,jiUL,resolu,ZC]=enviread(filename)
% Input file directory
% Output 1) im = data
% Output 2) jiDim dimension cols and rows
% Output 3)jiUL UpperLeft coner of the pixel (x,y)
% Output 4) resolution [x,y]
% Output 5) ZC zone code
% version 1.1 Able to read in USGS ENVI format data
% record orignial name
origname = filename;
% remove .img or other extra names for image file
filename = strsplit(filename,'.');
filename = char(filename(1));
filename_HDR=[filename,'.HDR'];
filename_hdr=[filename,'.hdr'];
fid_in1=fopen(filename_hdr,'r');
fid_in2=fopen(filename_HDR,'r');
if fid_in1~=-1
fid_in=fid_in1;
elseif fid_in2~=-1
fid_in=fid_in2;
else
fprintf('Wrong ENVI header file!\n');
return;
end
geo_char=fscanf(fid_in,'%c',inf);
fclose(fid_in);
geo_char=geo_char';
geo_str=strread(geo_char,'%s');
indx_samples = strmatch('samples',geo_str)+2;
indx_lines = strmatch('lines',geo_str)+2;
indx_bands = strmatch('bands',geo_str)+2;
indx_datatype = strmatch('data',geo_str)+3;
indx_interleave = strmatch('interleave',geo_str)+2;
indx_xUL = strmatch('map',geo_str)+6;
indx_yUL = strmatch('map',geo_str)+7;
indx_xreso = strmatch('map',geo_str)+8;
indx_yreso = strmatch('map',geo_str)+9;
indx_zc = strmatch('map',geo_str)+10;
% read input image hdr
cols = str2double(geo_str(indx_samples));
rows = str2double(geo_str(indx_lines));
jiDim = [cols,rows];
bands = str2double(geo_str(indx_bands));
datatype = str2double(geo_str(indx_datatype));
datatype = datatype(1); % only use the first one for datatype
interleave = char(geo_str(indx_interleave));
jiUL(1)=str2double(geo_str(indx_xUL));
jiUL(2)=str2double(geo_str(indx_yUL));
resolu(1)=str2double(geo_str(indx_xreso));
resolu(2)=str2double(geo_str(indx_yreso));
ZC=str2double(geo_str(indx_zc));
if datatype == 1
in_type = 'uint8';
elseif datatype == 2
in_type = 'int16';
elseif datatype == 3
in_type = 'int32';
elseif datatype == 12
in_type = 'uint16';
elseif datatype ==4
in_type = 'single';
else
error('Invalid read data type!');
end
% Define the data set.
% Read every other band of the data using the Band-Sequential format.
im = multibandread(origname, [rows cols bands], ...
[in_type,'=>',in_type], 0, interleave, 'ieee-le');