diff --git a/matlab/readspe.m b/matlab/readspe.m new file mode 100644 index 0000000..22f01fa --- /dev/null +++ b/matlab/readspe.m @@ -0,0 +1,78 @@ +% Read images of TIFF, SPE2.2(WinSpec) and SPE3.0(Lightfield) +% Version: JTL Jun-9-2016 +% ----------------- READ THIS FIRST !!!!! -------------------------------- +% Change the file name to "readspe" before use +% Example: +% Z = readspe(filename) +% Z = readspe(filename,'info') +% Z = readspe(filename,frame_index) +% Z = readspe(filename,frame_index,'info') +% Input: +% filename - filename string, e.g. 'image.spe' +% frame_index - frame index, start from 1 +% If you have multiple frames, use a "for" loop +% 'info' - flag to show file info, i.e. dimension, number of frames, version +% Output: +% Z - UINT16 image (convert to double if you need) +% ------------------------------------------------------------------------ +% Z = readspe (filename,frame_index,'info') +function Z = readspe (filename,varargin) + +if exist(filename) == 2 + + Nfr = 1; % default read first frame + if nargin >1 + if isa(varargin{1},'numeric') + Nfr = varargin{1}; + end + end + + [~,name,ext] = fileparts(filename); + switch upper(ext) + case '.TIFF' + file_ver = 'TIFF'; + Z = imread(filename); + [Y,X] = size(Z); + % datatype = class(Z) + + case '.SPE' + fid = fopen(filename); + I = fread(fid,Inf,'uint8'); + X = double(typecast(uint8(I(43:44)),'uint16')); + Y = double(typecast(uint8(I(657:658)),'uint16')); + fr = typecast(uint8(I(1447:1450)),'int32'); + spe_ver = typecast(uint8(I(1993:1996)),'single'); + file_ver = ['SPE ' num2str(spe_ver)]; + datatypeN = typecast(uint8(I(109:110)),'int16'); + switch datatypeN + case 0 % 32-bit float + datatype = 'single'; datalength = 4; + case 1 % 32-bit signed integer + datatype = 'int32'; datalength = 4; + case 2 % 16-bit signed integer + datatype = 'int16'; datalength = 2; + case 3 % 16-bit unsigned integer + datatype = 'uint16'; datalength = 2; + case 8 % 32-bit unsigned integer + datatype = 'uint32'; datalength = 4; + end + % A = I(4101:4100+X*Y*2); % Default read first frame + A = I(4101+X*Y*datalength*(Nfr-1):4100+X*Y*datalength*Nfr); + B = typecast(uint8(A),datatype); % important + Z = reshape(B,X,Y); + Z = Z'; + fclose(fid); + end + + if nargin >1 + if varargin{end} == 'info' + display(['X = ' num2str(X)]); + display(['Y = ' num2str(Y)]); + if(exist('fr','var'));display(['Number of Frames: ' num2str(fr)]);end; + display(['File version: ' file_ver]); + end + end + +elseif exist(filename) == 0 + display('File does not exist!'); +end \ No newline at end of file diff --git a/matlab/spe2envi.m b/matlab/spe2envi.m new file mode 100644 index 0000000..df15818 --- /dev/null +++ b/matlab/spe2envi.m @@ -0,0 +1,34 @@ +function spe2envi(filemask, outfile) + + filelist = dir(filemask); + + %get a list of date numbers + datenums = cell2mat({filelist.datenum}); + + %sort the file order based on acquisition time + [~, id] = sort(datenums); + + %get the number of files + Y = length(id); %size of the image along Y + + %load the first file to determine the spectral and X-axis size + temp = readspe(filelist(1).name); + X = size(temp, 1); %size of the image along X + B = size(temp, 2); %number of bands in the image + + %create the cube + I = zeros(X, Y, B); + + %for each line + for y = 1:Y + + %read a SPE file + img = readspe(filelist(id(y)).name); + + I(:, y, :) = permute(img, [1 3 2]); + end + + enviSaveRaw(single(I), outfile, [outfile '.hdr']); + + + -- libgit2 0.21.4