Commit d24662d2531bf1bf482134e1d92591988af21d16
Merge branch 'jahandar' into 'master'
added a Matlab function that converts images to one envi file Just some notes Dr. Mayerich: 1. The images should be stored in a struct to be loaded in the function. 2. I cast any type to float. So, if we have some files of 'double' type, we might loose some accuracy. 3. I just included the 'samples', 'lines', 'bands', 'header_offset', 'file_type', 'data_type' and 'interleave' parameters in the header file. Please let me know if you need any other things to be added to the function. See merge request !6
Showing
1 changed file
with
74 additions
and
0 deletions
Show diff stats
1 | +function images2ENVI( image_struct, filename ) | |
2 | +%HSIWRITE : This function generates a hyperspectral binary image and header | |
3 | +% based on multi-dimention image | |
4 | +% | |
5 | +% | |
6 | +% This function accepts a srutcture which contains several channels. | |
7 | +% It creates a 'bsq' interleaved image and a ENVI header filer correspoing | |
8 | +% to the information of the image | |
9 | + | |
10 | +fields = fieldnames(image_struct); | |
11 | + | |
12 | +% check all images to have the same size | |
13 | +for i = 1 : size (fields, 1) | |
14 | + for j = 1 : size (fields, 1) | |
15 | + if size(image_struct.(fields{i}))~= size(image_struct.(fields{i})) | |
16 | + fprintf ('Dimension of input images do not Match !'); | |
17 | + exit; | |
18 | + end | |
19 | + end | |
20 | +end | |
21 | + | |
22 | + | |
23 | + | |
24 | + | |
25 | + | |
26 | +% Extracting the needed information | |
27 | +base_image = image_struct.(fields{1}); | |
28 | + | |
29 | +rows = size(base_image, 1); | |
30 | +columns = size(base_image, 2); | |
31 | +bands = size(fields,1); | |
32 | +multiDimImage = base_image'; % Matlab stores column major, transposing make it row major to be saved on memory | |
33 | + | |
34 | +% Create multi-dimentional image | |
35 | +for j = 1 : size(fields,1) | |
36 | + multiDimImage(:,:,j) = (image_struct.(fields{j})'); % Matlab stores column major, transposing make it row major to be saved on memory | |
37 | +end | |
38 | + | |
39 | +% create the binary image | |
40 | +HSImage = reshape (multiDimImage, [rows*columns*bands, 1]); | |
41 | +if (~isfloat(HSImage)) | |
42 | + HSImage_float = single(HSImage); % casting to float | |
43 | +end | |
44 | +multibandwrite(HSImage_float,filename,'bsq'); | |
45 | + | |
46 | + | |
47 | +% create the header file | |
48 | +info.samples = columns; | |
49 | +info.lines = rows; | |
50 | +info.bands = bands; | |
51 | +info.header_offset = 0; | |
52 | +info.file_type = 'ENVI Standard'; | |
53 | +info.data_type = 4; | |
54 | +info.interleave = 'bsq'; | |
55 | + | |
56 | + | |
57 | + | |
58 | + | |
59 | +fid = fopen(strcat(filename, '.hdr'),'w'); % open a file to write the header file | |
60 | +fprintf(fid,'%s\n','ENVI'); % first line of the header file showing it is an ENVI file | |
61 | + | |
62 | +info_fields=fieldnames(info); | |
63 | +for i = 1:length(info_fields) | |
64 | + parameter = info_fields{i}; % get the parameter of each line | |
65 | + value = info.(info_fields{i}); % get the corresponding value of the parameter | |
66 | + parameter(strfind(parameter,'_')) = ' '; % change '_' to ' ' | |
67 | + line=[parameter,' = ',num2str(value)]; % create the line of the hdr file | |
68 | + fprintf(fid,'%s\n',line); % write it to the hdr file | |
69 | +end | |
70 | + | |
71 | +fclose(fid); % clode the header file | |
72 | + | |
73 | +end | |
74 | + | ... | ... |