forked from hpe079/Feature_Tracking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatureTrackingReadImages.m
More file actions
209 lines (183 loc) · 6.66 KB
/
featureTrackingReadImages.m
File metadata and controls
209 lines (183 loc) · 6.66 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
function [A, iA, B, iB] = featureTrackingReadImages(fileA, fileB)
% featureTrackingReadImages - read two geotiff images for feature tracking
%
% SYNTAX:
%
% [A, Ia, B, Ib] = featureTrackingReadImages(fileA, fileB)
%
% INPUTS:
%
% fileA: Name of first file to input
%
% fileB: Name of second file to input
%
% OUTPUTS:
%
% A: The image data for the first image (as a TWO dimensional single
% precision numeric array)
%
% Ia: geotiff information corresponding to the first image
%
% B: The image data for the second image (as a TWO dimensional single
% precision numeric array)
%
% Ib: geotiff information corresponding to the second image
%
% NOTES:
%
% In addition to loading the images, the following checks are also
% carried out:
%
% i) That the geographical coordinates are in METRES rather than
% latitude and longitude
%
% ii) The two images have almost exactly the same resolution and take
% up the same space
%
% EXAMPLE:
%
% % Read in files
% fA = 'G:\Isung_2015to2016\Isung290715_DEM_1.35m_clipRep.tif';
% fB = 'G:\Isung_2015to2016\Isung120716_DEM_1.35m_clipRep.tif';
% [A, Ia, B, Ib] = featureTrackingReadImages(fA, fB);
%
% % And display
% figure;
% ax1 = subplot (1,2,1);
% image (A);
% ax2 = subplot (1,2,2);
% image (B);
% linkaxes ([ax1 ax2])
%
%----------------
% STEP 0: Check inputs
%----------------
if nargin < 2
error('featureTrackingReadImages:notEnoughInputs', 'Must supply two inputs');
end
i_checkInputs(fileA, fileB);
%----------------
% STEP 1: Load Files
%----------------
% Pass on to load and do a few quick checks
[A, iA] = i_loadAndCheckFile(fileA);
[B, iB] = i_loadAndCheckFile(fileB);
% And some cross checks?
i_doCrossChecks(iA, iB);
%--------------------------------------------------------------------------
function i_doCrossChecks(iA, iB)
% Cross checks between the two files
% Check A: EXACTLY the same size??
if ~isequal(iA.RasterSize, iB.RasterSize)
error('featureTrackingReadImages:differentSizes', 'Images are different sizes (%s and %s)', mat2str(iA.RasterSize), mat2str(iB.RasterSize));
end
% Check B: Same resolution or just check the overall extent of the image?
dX = iA.XWorldLimits - iB.XWorldLimits;
dY = iA.YWorldLimits - iB.YWorldLimits;
% And some strings
if dX(1) <= 0
leftStr = sprintf('Left edge of second image is %sm to the right of left edge of first image', num2str(-dX(1)));
else
leftStr = sprintf('Left edge of second image is %sm to the left of left edge of first image', num2str(dX(1)));
end
if dX(2) <= 0
rightStr = sprintf('Right edge of second image is %sm to the right of right edge of first image', num2str(-dX(2)));
else
rightStr = sprintf('Right edge of second image is %sm to the left of right edge of first image', num2str(dX(2)));
end
if dY(1) <= 0
bottomStr = sprintf('Bottom edge of second image is %sm above bottom edge of first image', num2str(-dY(1)));
else
bottomStr = sprintf('Bottom edge of second image is %sm below bottom edge of first image', num2str(dY(1)));
end
if dY(2) <= 0
topStr = sprintf('Top edge of second image is %sm above top edge of first image', num2str(-dY(2)));
else
topStr = sprintf('Top edge of second image is %sm below top edge of first image', num2str(dY(2)));
end
% Are they errors or displays?!
maxTol = 1;
if dX(1) > maxTol
error(leftStr);
else
disp(leftStr);
end
if dX(2) > maxTol
error(rightStr);
else
disp(rightStr);
end
if dY(1) > maxTol
error(bottomStr);
else
disp(bottomStr);
end
if dY(2) > maxTol
error(topStr);
else
disp(topStr);
end
%--------------------------------------------------------------------------
function [A, iA] = i_loadAndCheckFile(fileA)
% Load and check an individual file
%-----------------
% STEP 1: Load file
%-----------------
% Just pass on, but rewrap the error
try
[A, iA] = geotiffread(fileA);
catch lErr
error('featureTrackingReadImages:errorLoadingFile', 'Unexpected error loading "%s": %s', fileA, lErr.message);
end
%-----------------
% STEP 2: Make consistent
%-----------------
% FOR THE MOMENT JUST SUPPORTING TWO DIMENSIONAL ARRAYS THAT ARE ALREADY
% SINGLE
if isa(A, 'uint16') && size(A, 3) == 1
A = single(A)/65535;
elseif ~isa(A, 'single') || size(A, 3) ~= 1
error('featureTrackingReadImages:unsupportedImageType', 'Image not yet supported - please see Dave to make necessary changes!! (File: "%s")', fileA);
end
% IN FUTURE WILL PROBABLY NEED TO ADD UINT8 OR UINT16 FOR NON-DEM IMAGES AS
% WELL AS [R, G, B] IMAGES
% AND check for any missing values?
imInfo = imfinfo(fileA);
if isfield(imInfo, 'GDAL_NODATA') && ischar(imInfo.GDAL_NODATA)
missingVal = str2double(imInfo.GDAL_NODATA);
A(A == missingVal) = NaN;
end
%-----------------
% STEP 3: Check geographical data
%-----------------
% Check A: Make sure it is in metres rather than in lat/lon
if ~isprop(iA, 'CellExtentInWorldX') || ~isprop(iA, 'CellExtentInWorldY')
error('featureTrackingReadImages:badDimensionType', 'Files must use UTM coordanates not Lat/Lon (File: "%s")', fileA);
end
% Check B: Check they're reasonable??
cX = iA.CellExtentInWorldX;
cY = iA.CellExtentInWorldY;
minVal = 0.1;
maxVal = 100;
if cX < minVal || cX > maxVal
error('featureTrackingReadImages:xOutsideRange', 'Pixel size in X direction (%sm) is outside expected range (%s - %s m) (File: "%s")', num2str(cX), num2str(minVal), num2str(maxVal), fileA);
elseif cY < minVal || cY > maxVal
error('featureTrackingReadImages:yOutsideRange', 'Pixel size in Y direction (%sm) is smaller than minimum expected (%s - %s m) (File: "%s")', num2str(cY), num2str(minVal), num2str(maxVal), fileA);
end
% Check C: Check they're both really close to each other? Make sure
% within a cm
maxDiff = 0.01;
if abs(cX - cY) > maxDiff
error('featureTrackingReadImages:xyDifferent', 'Pixel size in X direction (%sm) and Y direction (%sm) are more than %sm different (File: "%s")', num2str(cX), num2str(cY), num2str(maxDiff), fileA);
end
%--------------------------------------------------------------------------
function i_checkInputs(fileA, fileB)
% Check the two inputs are ok...
% Must both be strings (to be filenames...)
if ~ischar(fileA) || ~ischar(fileB)
error('featureTrackingReadImages:badInputs', 'Inputs must both be strings');
elseif exist(fileA, 'file') ~= 2
error('featureTrackingReadImages:badInput1', 'Could not find first file ("%s")', fileA);
elseif exist(fileB, 'file') ~= 2
error('featureTrackingReadImages:badInput2', 'Could not find second file ("%s")', fileB);
end