-
Notifications
You must be signed in to change notification settings - Fork 0
/
addOutputImages.m
57 lines (51 loc) · 1.71 KB
/
addOutputImages.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
% script to add n 1-band mask files to make 1 n-band categorical raster
% TODO: generalize so I can use it in other dirs
% Written by Ethan Kyzivat
% DEPRECATED
function addOutputImages(filename)
global env
%% test params
% filename='*Class*.png'
%% input file type
if ~isunix
base_names=ls([env.output.test_dir, filename, '*.png']);
else
f.base_names_0=ls([env.output.test_dir, filename, '*.png']); %hot fix
f.base_names_1=textscan(f.base_names_0, '%s', 'Delimiter', '\n');
base_names=char(f.base_names_1{:});
% base_names=strjoin(cellstr(f.base_names_2), ' ');
end
%%
georef_in=(ls([env.output.test_dir, filename, '.tif']));
if ~isunix
output_pth=[env.output.test_dir, georef_in(1,1:end-4), '_cls.tif'];
gt=geotiffinfo([env.output.test_dir, georef_in]);
else
output_pth=[georef_in(1,1:end-5), '_cls.tif'];
gt=geotiffinfo(georef_in(1,1:end-1)); % hot fix
end
for n=1:size(base_names,1)
if ~isunix %hot fix
I=imread([env.output.test_dir, base_names(n,:)]);
else
I=imread(base_names(n,:));
end
if n==1
out=zeros(size(I), 'uint8');
end
out=n*uint8(I)+out;
end
%% apply mask
if ~isunix
test_im=imread([env.output.test_dir, georef_in]);
else
test_im=imread(strtrim(georef_in));
end
mask=isnan(test_im(:,:,end)); % negative data mask
out(mask)=env.constants.noDataValue_ouput; %%% HERE 2/18/20: mask is different size than out
%% write
geotiffwrite(output_pth, out, gt.SpatialRef, 'GeoKeyDirectoryTag',gt.GeoTIFFTags.GeoKeyDirectoryTag);
% gdal_calc.py -A input1.tif -B input2.tif --outfile=result.tif --calc="A+B"
%% Add NoData values to rasters
cmd=sprintf('gdal_edit.py -a_nodata %d %d', env.constants.noDataValue_ouput, output_pth);
system(cmd)