-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBP_OBIA.m
173 lines (153 loc) · 6.17 KB
/
BP_OBIA.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
% Script to apply block processing to water classification. Had some bugs
% with Matlab r2018a and AMD Radeon Pro Graphics card driver- causing a
% memory error and the blue screen of death. Bug is now possibly fixed by
% updating AMD driver. Workaround at the time was to use opengl for
% graphics operations rather than AMD driver. BP stands for block
% processing and OBIA stands for object-based image classification.
% File queue
clear; clc; close all
env_vars % load environment variables
disp('Batch started.'); disp(datetime)
tbatch=tic;
global f % this structure holds all user-dependent parameters, such as file paths and tuning parameters
dbstop if error
opengl software % don't use AMD graphics card/driver
%%%%%%%%%%%%%%%%%%%% user params
exclude=[]; % exclude files from queue
fileQueue=[216]; % list of file numbers in input directory to process.
f.plot=false; %set as true if you want to asee plots (slows it down a little)
f.logDir='D:\ArcGIS\FromMatlab\CIRLocalThreshClas\Final\logs\';
% set(0,'DefaultFigureVisible','off')
f.dir_in='F:\AboveDCSRasterManagement\CanadaAlbersTranslate\';
f.dir_out='D:\ArcGIS\FromMatlab\CIRLocalThreshClas\Final\';
f.parallel=0;
f.RegionGrowing=1; % set to test on global NDWI only
%% Check requirements
p=split(path, ';');
req_in_path=any(contains(p, 'Image Graphs'));
tbxs=matlab.addons.toolbox.installedToolboxes;
tbx_names={tbxs.Name};
req_in_toolbox=any(contains(tbx_names, 'Image Graphs'));
if ~req_in_path && ~req_in_toolbox
warning('The required toolbox ''Image Graphs'' is required. Install it at: https://www.mathworks.com/matlabcentral/fileexchange/53614-image-graphs')
end
clear p req_in_path req_in_toolbox tbx_names tbxs
%% Run
%logfile=[f.logDir, 'log.txt'];
% fid=fopen(logfile, 'a');
% fprintf(fid, '----------------------\n');
% fclose(fid);
% files=cellstr(ls([f.dir_in, '*.tif']));
files=cellstr(ls([f.dir_in, '*.tif']));
disp('Files:')
disp([num2cell([1:length(files)]'), files])
fileQueue=setdiff(fileQueue, exclude, 'stable');
% f.tileSize has to be a multiple of 16, and apparentely
% needs to be same as processing window size
if f.parallel==1
% f.tileSize = [6400, 6400];
f.tileSize = [5760, 5760];
try parpool(2);
catch
end
end
datecode=char(datetime('now','Format','yyyy-MM-dd-HHmm'));
disp('File queue:')
disp(files(fileQueue))
% read param table
tbl_in='D:\ArcGIS\FromMatlab\CIRLocalThreshClas\Final\logs\WC_LOG_Summ.xlsx';
[tbl, tbl_raw]=xlsread(tbl_in, 1);
%% Loop
for i=fileQueue
disp(datetime)
fprintf('File number: %d\n', i)
name_in=files{i}; %27
img_in=[f.dir_in, name_in];
disp(dir(img_in))
% Format tiff object
inFileInfo = imfinfo(img_in);
% Parallel
% if inFileInfo.FileSize < 3e+9
% f.parallel=0;
% else
% f.parallel=0;
% end
% Format name out
name_out=['WC', name_in(4:end-4), '.tif'];
img_out=[f.dir_out, name_out]; %NB means not border
% load params from check file
try
f.aConn=tbl(i,26); %
f.bConn=tbl(i,27); %
f.tileSize=[tbl(i,28), tbl(i,29)];
f.bounds=[tbl(i,30), tbl(i,31)];
f.NDWIWaterAmount=tbl(i,33); % -
f.NDWILandAmount=tbl(i,32);
f.wp=tbl(i,34);
f.windex=tbl_raw{i+1, 35}; % note raw table input includes column headers...
f.Tlim=tbl(i,39);
catch % just in case parsing problem
warning('Trouble reading user params (structure f).')
f.aConn=15; %
f.bConn=170; %
f.tileSize=[9600 9600];
f.bounds=[1.5 2.5];
f.NDWIWaterAmount=0.04; % -
f.NDWILandAmount=-0.06;
f.wp=10;
f.windex='NDWI';
f.Tlim=5.3;
end
% Process images
if f.RegionGrowing==1
g = @(block_struct) OBIA_BP_Fun(block_struct, f.logDir, 'local', img_out, datecode);
else
g = @(block_struct) OBIA_BP_Fun(block_struct, f.logDir, 'global', img_out, datecode);
name_out=['WC', name_in(4:end-4), '_Global.tif'];
img_out=[f.dir_out, name_out];
end
outFileWriter = BP_bigTiffWriterEK(img_out, inFileInfo(1).Height,...
inFileInfo(1).Width, f.tileSize(1), f.tileSize(2));
% g= @(M_strxr) M_strxr.data; % identity function as test
window=f.tileSize; % block proc window size
tic
disp('Classifying...')
try
blockproc(img_in, window, g, 'Destination', outFileWriter, 'UseParallel', f.parallel);
catch
warning('Blockproc failed. Trying again with smaller tile size.')
f.tileSize = [8192, 8192];
outFileWriter = BP_bigTiffWriterEK(img_out, inFileInfo(1).Height,...
inFileInfo(1).Width, f.tileSize(1), f.tileSize(2));
window=f.tileSize; % block proc window size
blockproc(img_in, window, g, 'Destination', outFileWriter, 'UseParallel', f.parallel);
end
fprintf('Done. \n\tParallel option = %u. Window = %u by %u pixels\n', f.parallel, window)
toc
% Save georef info
% .mat file
try
info=geotiffinfo(img_in);
% gti_out=[f.dir_out, name_out(1:end-4), '.mat'];
% save(gti_out, 'info')
% .tfw world file
gti_out=[f.dir_out, name_out(1:end-4), '.tfw'];
worldfilewrite(info.SpatialRef, gti_out)
% add geotiffwrite for ease (extra processing)!
% .proj file
AlbersProj='prj\Canada Albers Equal Area Conic.prj';
proj_out=[f.dir_out, name_out(1:end-4), '.prj'];
% copyfile(AlbersProj, proj_out);
% Display
disp('Georef files written.')
catch
disp('NO georef files written.')
end
fprintf('Output written: %s\n', img_out);
end
clear outFileWriter
outFileInfo = imfinfo(img_out);
disp('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
disp('Batch finished.'); disp(datetime)
elapsedTime=toc(tbatch); fprintf('Elapsed time:\t%3.2f minutes\n', ...
elapsedTime/60);