Skip to content

Commit 223fc07

Browse files
author
Anders
committed
Load ROIs if they have already been extracted
1 parent 7e73507 commit 223fc07

File tree

1 file changed

+42
-13
lines changed

1 file changed

+42
-13
lines changed

common/ROI/extractROIsFromGeotiff.m

Lines changed: 42 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,50 @@
2121

2222
verbose = p.Results.verbose;
2323
numDisplaySpaces = (verbose-1)*3;
24-
25-
% Load map(s)
26-
if (verbose)
27-
dispts(['Loading map: ' mapFilePath], numDisplaySpaces);
24+
25+
% Check if all ROIs exist. If they do, don't load map and extract them again
26+
[mapPath,mapFilename,~] = fileparts(mapFilePath);
27+
[~, ROIfilename, ~] = fileparts(ROIheader.Source.File);
28+
ROImapsFolder = fullfile(mapPath, 'ROIs', ROIfilename);
29+
ROIs_exist = zeros(size(ROIs));
30+
for r = 1:length(ROIs)
31+
ROIfilename = fullfile(ROImapsFolder,'raw', [mapFilename '_ROI_' ROIs(r).name{1} '.tif']);
32+
ROIs_exist(r) = exist(ROIfilename, 'file');
2833
end
29-
[map, ~] = geotiffread(mapFilePath);
30-
[mapInfo] = geotiffinfo(mapFilePath);
3134

32-
% Extracting ROIs from map
33-
if (verbose)
34-
dispts('Extracting ROIs from map...', numDisplaySpaces);
35+
if all(ROIs_exist) && isempty(refMapFile)
36+
dispts(['ROIs already extracted. Skip loading map, and loading extracted ROIs instead.'], numDisplaySpaces)
37+
saveROIs = false;
38+
tic;
39+
ROIsMap = ROIs;
40+
for r = 1:length(ROIs)
41+
ROIfilename = fullfile(ROImapsFolder,'raw', [mapFilename '_ROI_' ROIs(r).name{1} '.tif']);
42+
roi_info = geotiffinfo(ROIfilename);
43+
IroiGeotiff = geotiffread(ROIfilename);
44+
Iroi = IroiGeotiff(:,:,1:end-1);
45+
Imask = logical(IroiGeotiff(:,:,end));
46+
ROIsMap(r).Iroi = Iroi;
47+
ROIsMap(r).Imask = Imask;
48+
ROIsMap(r).mapTransformationROI = roi_info.SpatialRef;
49+
end
50+
clear roi_info IroiGeotiff Iroi Imask;
51+
toc
52+
else
53+
% Load map(s)
54+
if (verbose)
55+
dispts(['Loading map: ' mapFilePath], numDisplaySpaces);
56+
end
57+
[map, ~] = geotiffread(mapFilePath);
58+
[mapInfo] = geotiffinfo(mapFilePath);
59+
60+
% Extracting ROIs from map
61+
if (verbose)
62+
dispts('Extracting ROIs from map...', numDisplaySpaces);
63+
end
64+
% [ROIsMap] = extractROIsFromMap( map, mapInfo, ROIs, featureHandles, 'edges', edges, 'featureNames', featureNames, 'verbose', sign(verbose)*(verbose+1));
65+
[ROIsMap] = extractROIsFromMap( map, mapInfo, ROIs, 'verbose', sign(verbose)*(verbose+1));
66+
clear map;
3567
end
36-
% [ROIsMap] = extractROIsFromMap( map, mapInfo, ROIs, featureHandles, 'edges', edges, 'featureNames', featureNames, 'verbose', sign(verbose)*(verbose+1));
37-
[ROIsMap] = extractROIsFromMap( map, mapInfo, ROIs, 'verbose', sign(verbose)*(verbose+1));
38-
clear map;
3968

4069
% Load reference map
4170
if (isempty(refMapFile))
@@ -144,7 +173,7 @@
144173
% Get GeoKeyDirectoryTag from original map
145174
info = geotiffinfo(mapFilePath);
146175
GeoKeyDirectoryTag = info.GeoTIFFTags.GeoKeyDirectoryTag;
147-
176+
148177
% Loop throug ROIs
149178
for r = 1:length(ROIs)
150179
% Save ROIs as small geotiff

0 commit comments

Comments
 (0)