Commit d4ba0a6e161ab56c7ce1412a5c8d389ecc3bdfc5
Merge branch 'spe' into 'master'
added support for Wei's SPE files See merge request !7
Showing
2 changed files
with
112 additions
and
0 deletions
Show diff stats
1 | +% Read images of TIFF, SPE2.2(WinSpec) and SPE3.0(Lightfield) | ||
2 | +% Version: JTL Jun-9-2016 | ||
3 | +% ----------------- READ THIS FIRST !!!!! -------------------------------- | ||
4 | +% Change the file name to "readspe" before use | ||
5 | +% Example: | ||
6 | +% Z = readspe(filename) | ||
7 | +% Z = readspe(filename,'info') | ||
8 | +% Z = readspe(filename,frame_index) | ||
9 | +% Z = readspe(filename,frame_index,'info') | ||
10 | +% Input: | ||
11 | +% filename - filename string, e.g. 'image.spe' | ||
12 | +% frame_index - frame index, start from 1 | ||
13 | +% If you have multiple frames, use a "for" loop | ||
14 | +% 'info' - flag to show file info, i.e. dimension, number of frames, version | ||
15 | +% Output: | ||
16 | +% Z - UINT16 image (convert to double if you need) | ||
17 | +% ------------------------------------------------------------------------ | ||
18 | +% Z = readspe (filename,frame_index,'info') | ||
19 | +function Z = readspe (filename,varargin) | ||
20 | + | ||
21 | +if exist(filename) == 2 | ||
22 | + | ||
23 | + Nfr = 1; % default read first frame | ||
24 | + if nargin >1 | ||
25 | + if isa(varargin{1},'numeric') | ||
26 | + Nfr = varargin{1}; | ||
27 | + end | ||
28 | + end | ||
29 | + | ||
30 | + [~,name,ext] = fileparts(filename); | ||
31 | + switch upper(ext) | ||
32 | + case '.TIFF' | ||
33 | + file_ver = 'TIFF'; | ||
34 | + Z = imread(filename); | ||
35 | + [Y,X] = size(Z); | ||
36 | + % datatype = class(Z) | ||
37 | + | ||
38 | + case '.SPE' | ||
39 | + fid = fopen(filename); | ||
40 | + I = fread(fid,Inf,'uint8'); | ||
41 | + X = double(typecast(uint8(I(43:44)),'uint16')); | ||
42 | + Y = double(typecast(uint8(I(657:658)),'uint16')); | ||
43 | + fr = typecast(uint8(I(1447:1450)),'int32'); | ||
44 | + spe_ver = typecast(uint8(I(1993:1996)),'single'); | ||
45 | + file_ver = ['SPE ' num2str(spe_ver)]; | ||
46 | + datatypeN = typecast(uint8(I(109:110)),'int16'); | ||
47 | + switch datatypeN | ||
48 | + case 0 % 32-bit float | ||
49 | + datatype = 'single'; datalength = 4; | ||
50 | + case 1 % 32-bit signed integer | ||
51 | + datatype = 'int32'; datalength = 4; | ||
52 | + case 2 % 16-bit signed integer | ||
53 | + datatype = 'int16'; datalength = 2; | ||
54 | + case 3 % 16-bit unsigned integer | ||
55 | + datatype = 'uint16'; datalength = 2; | ||
56 | + case 8 % 32-bit unsigned integer | ||
57 | + datatype = 'uint32'; datalength = 4; | ||
58 | + end | ||
59 | + % A = I(4101:4100+X*Y*2); % Default read first frame | ||
60 | + A = I(4101+X*Y*datalength*(Nfr-1):4100+X*Y*datalength*Nfr); | ||
61 | + B = typecast(uint8(A),datatype); % important | ||
62 | + Z = reshape(B,X,Y); | ||
63 | + Z = Z'; | ||
64 | + fclose(fid); | ||
65 | + end | ||
66 | + | ||
67 | + if nargin >1 | ||
68 | + if varargin{end} == 'info' | ||
69 | + display(['X = ' num2str(X)]); | ||
70 | + display(['Y = ' num2str(Y)]); | ||
71 | + if(exist('fr','var'));display(['Number of Frames: ' num2str(fr)]);end; | ||
72 | + display(['File version: ' file_ver]); | ||
73 | + end | ||
74 | + end | ||
75 | + | ||
76 | +elseif exist(filename) == 0 | ||
77 | + display('File does not exist!'); | ||
78 | +end | ||
0 | \ No newline at end of file | 79 | \ No newline at end of file |
1 | +function spe2envi(filemask, outfile) | ||
2 | + | ||
3 | + filelist = dir(filemask); | ||
4 | + | ||
5 | + %get a list of date numbers | ||
6 | + datenums = cell2mat({filelist.datenum}); | ||
7 | + | ||
8 | + %sort the file order based on acquisition time | ||
9 | + [~, id] = sort(datenums); | ||
10 | + | ||
11 | + %get the number of files | ||
12 | + Y = length(id); %size of the image along Y | ||
13 | + | ||
14 | + %load the first file to determine the spectral and X-axis size | ||
15 | + temp = readspe(filelist(1).name); | ||
16 | + X = size(temp, 1); %size of the image along X | ||
17 | + B = size(temp, 2); %number of bands in the image | ||
18 | + | ||
19 | + %create the cube | ||
20 | + I = zeros(X, Y, B); | ||
21 | + | ||
22 | + %for each line | ||
23 | + for y = 1:Y | ||
24 | + | ||
25 | + %read a SPE file | ||
26 | + img = readspe(filelist(id(y)).name); | ||
27 | + | ||
28 | + I(:, y, :) = permute(img, [1 3 2]); | ||
29 | + end | ||
30 | + | ||
31 | + enviSaveRaw(single(I), outfile, [outfile '.hdr']); | ||
32 | + | ||
33 | + | ||
34 | + |