-
Notifications
You must be signed in to change notification settings - Fork 2
/
peaches_data.m
208 lines (157 loc) · 7.79 KB
/
peaches_data.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
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
% --------------- Example How to Load and Work with RGB+thermal data ------
clear all; clc; close all; % clean up!
%%
% TOP row:
rgbpath = './data/peaches/top/RGB';
thermalpath = './data/peaches/top/thermal';
% BOTTOM row:
rgbpath = './data/peaches/bottom/RGB';
thermalpath = './data/peaches/bottom/thermal';
rgbds = datastore( rgbpath );
thermalds = datastore( thermalpath );
assert( length(rgbds.Files) == length(thermalds.Files) );
% Note: RGB-thermal pairs are paired by the sorted filename!
% (e.g., the first image in the RGB folder corresponds to the first image
% in the thermal folder)
%% display pairs (without any modifications)
for i = 1:length(rgbds.Files)
rgb = readimage( rgbds, i );
thermal = readimage( thermalds, i );
figure(i); clf;
subplot(2,1,1); imshow( rgb ); title( 'RGB' );
subplot(2,1,2); imshow( thermal, [] ); title ( 'thermal' );
% show with colormap: colormap( 'parula' );
drawnow; % display immediately
end
%% display undistored pairs
rgbParams = load( './data/camParams_RGB.mat' );
thermalParams = load( './data/camParams_thermal.mat' );
for i = 1; % for all use 1:length(rgbds.Files)
rgb = undistortImage( readimage( rgbds, i ), rgbParams.cameraParams );
thermal = undistortImage( readimage( thermalds, i ), thermalParams.cameraParams );
figure(i); clf;
subplot(2,1,1); imshow( rgb ); title( 'RGB' );
subplot(2,1,2); imshow( thermal, [] ); title ( 'thermal' );
colormap( 'parula' );
drawnow; % display immediately
end
%% naively overlay rgb on thermal images
i = 1; % index
h_fig = figure(i); clf;
thermal = undistortImage( readimage( thermalds, i ), thermalParams.cameraParams );
rgb = undistortImage( readimage( rgbds, i ), rgbParams.cameraParams );
rgb = imresize( rgb, size(thermal) );
imshowpair( rgb, thermal, 'falsecolor' );
%% overlay thermal and rgb images with intrinsics
% we need a transformation to map RGB images to thermal
% multiplying the inverse RGB intrinsic and the thermal intrinsics
% results in the proper transformation.
M = inv(rgbParams.cameraParams.IntrinsicMatrix) * thermalParams.cameraParams.IntrinsicMatrix;
tform = projective2d( M );
for i = 1; % for all use 1:length(rgbds.Files)
thermal = undistortImage( readimage( thermalds, i ), thermalParams.cameraParams );
rgb = undistortImage( readimage( rgbds, i ), rgbParams.cameraParams );
rgb = imwarp(rgb,tform,'OutputView',imref2d(size(thermal))); %warp RGB
[~,rgbfileinfo] = readimage( rgbds, i );
[~,thermalfileinfo] = readimage( thermalds, i );
[~,rgbname,~] = fileparts(rgbfileinfo.Filename);
[~,thermalname,~] = fileparts(thermalfileinfo.Filename);
h_fig = figure(i); clf;
set( h_fig, 'Name', [ 'RGB:' rgbname ', thermal:' thermalname ] );
imshowpair( rgb, thermal, 'falsecolor' );
drawnow; % display immediately
end
%% calibrate mapping of RGB<->thermal with checkerboard
% CALIBRATION:
calibrgbpath = './data/peaches/calibration/RGB';
calibthermalpath = './data/peaches/calibration/thermal';
calibrgbds = datastore( calibrgbpath );
calibthermalds = datastore( calibthermalpath );
usePatternId = 1;
rgb = undistortImage( readimage( calibrgbds, usePatternId ), rgbParams.cameraParams );
[rgb_imagePoints,rgb_boardSize,rgb_imagesUsed] = detectCheckerboardPoints(rgb);
thermal = undistortImage(readimage( calibthermalds, usePatternId ), thermalParams.cameraParams );
thermal = 255 - thermal; % invert so that it matches the RGB colors
[thermal_imagePoints,thermal_boardSize,thermal_imagesUsed] = detectCheckerboardPoints(thermal);
assert( isequal( thermal_boardSize, rgb_boardSize ) );
figure(100); clf;
subplot(2,2,1); imshow( rgb ); title( 'RGB' ); hold on;
plot( rgb_imagePoints(:,1), rgb_imagePoints(:,2), 'rx' );
subplot(2,2,2); imshow( thermal, [] ); title ( 'thermal' ); hold on;
plot( thermal_imagePoints(:,1), thermal_imagePoints(:,2), 'rx' );
%% estimate 2D transformation (for imwarp)
tform = fitgeotrans(rgb_imagePoints,thermal_imagePoints,'projective');
% --- warp images ---
% rgb on thermal
warpedrgb = imwarp(rgb,tform,'OutputView',imref2d(size(thermal)));
% thermal on RGB
warpedthermal = imwarp(thermal,tform.invert(),'OutputView',imref2d(size(rgb)));
figure(100); % continue figure ...
subplot(2,2,3); imshow( warpedthermal ); title( 'warped thermal' ); hold on;
plot( rgb_imagePoints(:,1), rgb_imagePoints(:,2), 'rx' );
subplot(2,2,4); imshow( warpedrgb ); title ( 'warped RGB' ); hold on;
plot( thermal_imagePoints(:,1), thermal_imagePoints(:,2), 'rx' );
figure(101);
subplot(1,2,1); title ( 'warped thermal to RGB' );
imshowpair( rgb, warpedthermal, 'falsecolor' ); title( 'original RGB, warped thermal' );
subplot(1,2,2); title ( 'RGB to warped thermal' );
imshowpair( warpedrgb, thermal, 'falsecolor' ); title( 'original thermal, warped RGB' );
%% estimate extrinsics too
squareSize = 50; % 'millimeters'
worldPoints = generateCheckerboardPoints(thermal_boardSize, squareSize);
[thermal_rotation,thermal_transl] = extrinsics(thermal_imagePoints,worldPoints,thermalParams.cameraParams);
[rgb_rotation, rgb_transl] = extrinsics(rgb_imagePoints,worldPoints,rgbParams.cameraParams);
%% plot location of thermal and RGB camera
figure(102); clf; hold on;
grid on; axis equal;
% thermal
[orientation,location] = extrinsicsToCameraPose( thermal_rotation,thermal_transl );
plotCamera('Orientation', orientation, 'Location', location , 'Size', 5);
text( location(1), location(2), location(3), 'thermal' );
% RGB
[orientation,location] = extrinsicsToCameraPose( rgb_rotation,rgb_transl );
plotCamera('Orientation', orientation, 'Location', location , 'Size', 5);
text( location(1), location(2), location(3), 'RGB' );
title( 'RGB and thermal poses' );
%% relative transformation (rotation + translation) from RGB to thermal
R = rgb_rotation' * thermal_rotation;
t = thermal_transl - rgb_transl * R;
% save:
if ~exist( 'results', 'dir' ), mkdir( 'results' ); end;
save( './results/rgb2thermal_transf.mat', 'R', 't' );
worldPoints3D = worldPoints;
worldPoints3D(:,3) = 0;
% reproject thermal
thermal_reproj = worldToImage( thermalParams.cameraParams, thermal_rotation, thermal_transl, worldPoints3D );
% reproject on thermal with RGB rotation and translation
rgb2thermal_reproj = worldToImage( thermalParams.cameraParams, rgb_rotation*R, rgb_transl*R+t, worldPoints3D );
% reproject RGB
rgb_reproj = worldToImage( rgbParams.cameraParams, rgb_rotation, rgb_transl, worldPoints3D );
% reproject on RGB with thermal transformations
thermal2rgb_reproj = worldToImage( rgbParams.cameraParams, thermal_rotation*R', (thermal_transl-t)*R', worldPoints3D );
figure(103);
subplot(1,2,1); imshow( thermal ); title ( 'thermal' ); hold on;
plot( thermal_reproj(:,1), thermal_reproj(:,2), 'r+' );
plot( rgb2thermal_reproj(:,1), rgb2thermal_reproj(:,2), 'gx' );
subplot(1,2,2); imshow( rgb ); title ( 'RGB' ); hold on;
plot( rgb_reproj(:,1), rgb_reproj(:,2), 'r+' );
plot( thermal2rgb_reproj(:,1), thermal2rgb_reproj(:,2), 'gx' );
%% estimate transformation from extrinsics (for imwarp)
z = 900; % millimeters
% the checkerboard is ~900 millimeters away
% the tree in the background is ~100000 millimeters (100 m)
P = (inv(rgbParams.cameraParams.IntrinsicMatrix) * R * thermalParams.cameraParams.IntrinsicMatrix );
P_transl = (t * thermalParams.cameraParams.IntrinsicMatrix);
P_ = P; % copy
P_(3,:) = P_(3,:) + P_transl./z; % add translation
tform = projective2d( P_ );
% --- warp images ---
% rgb on thermal
warpedrgb = imwarp(rgb,tform,'OutputView',imref2d(size(thermal)));
% thermal on RGB
warpedthermal = imwarp(thermal,tform.invert(),'OutputView',imref2d(size(rgb)));
figure(104); clf;
subplot(1,2,1); title ( 'warped thermal to RGB' );
imshowpair( rgb, warpedthermal, 'falsecolor' ); title( 'original RGB, warped thermal' );
subplot(1,2,2); title ( 'RGB to warped thermal' );
imshowpair( warpedrgb, thermal, 'falsecolor' ); title( 'original thermal, warped RGB' );