Subversion Repositories seema-scanner

Rev

Rev 219 | Rev 237 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 219 Rev 236
1
function MSE = alignSubScansMarkers(calibrationFileName, alnFileName)
1
function MSE = alignSubScansMarkers(calibrationFileName, alnFileName)
2
%ALIGNSUBSCANSMARKERS Determines an exact alignment of sub scans (scans
2
%ALIGNSUBSCANSMARKERS Determines an exact alignment of sub scans (scans
3
% from e.g. one revolution of the rotation stage). 
3
% from e.g. one revolution of the rotation stage). 
4
% The method searches for circular white markers of a specific diameter.
4
% The method searches for circular white markers of a specific diameter.
5
% White frames corresponding to each sub scan must be available.
5
% White frames corresponding to each sub scan must be available.
6
% A coarse alignment in the form of an aln-file must be provided. 
6
% A coarse alignment in the form of an aln-file must be provided. 
7
%
7
%
8
% 2017 Jakob Wilm, DTU
8
% 2017 Jakob Wilm, DTU
9
 
9
 
10
initialAlign = readMeshLabALN(alnFileName);
10
initialAlign = readMeshLabALN(alnFileName);
11
 
11
 
12
calibration = readOpenCVXML(calibrationFileName);
12
calibration = readOpenCVXML(calibrationFileName);
13
 
13
 
14
% correct for Matlab 1-indexing in principle point coordinates
14
% correct for Matlab 1-indexing in principle point coordinates
15
calibration.K0(1:2, 3) = calibration.K0(1:2, 3)+1;
15
calibration.K0(1:2, 3) = calibration.K0(1:2, 3)+1;
16
calibration.K1(1:2, 3) = calibration.K1(1:2, 3)+1;
16
calibration.K1(1:2, 3) = calibration.K1(1:2, 3)+1;
17
 
17
 
18
% full projection matrices in Matlab convention
18
% full projection matrices in Matlab convention
19
P0 = transpose(calibration.K0*[eye(3) zeros(3,1)]);
19
P0 = transpose(calibration.K0*[eye(3) zeros(3,1)]);
20
P1 = transpose(calibration.K1*[calibration.R1 calibration.T1']);
20
P1 = transpose(calibration.K1*[calibration.R1 calibration.T1']);
21
 
21
 
22
% matlab cam params for undistortion
22
% matlab cam params for undistortion
23
camParams0 = cameraParameters('IntrinsicMatrix', calibration.K0', 'RadialDistortion', calibration.k0([1 2 5]), 'TangentialDistortion', calibration.k0([3 4]));
23
camParams0 = cameraParameters('IntrinsicMatrix', calibration.K0', 'RadialDistortion', calibration.k0([1 2 5]), 'TangentialDistortion', calibration.k0([3 4]));
24
camParams1 = cameraParameters('IntrinsicMatrix', calibration.K1', 'RadialDistortion', calibration.k1([1 2 5]), 'TangentialDistortion', calibration.k1([3 4]));
24
camParams1 = cameraParameters('IntrinsicMatrix', calibration.K1', 'RadialDistortion', calibration.k1([1 2 5]), 'TangentialDistortion', calibration.k1([3 4]));
25
 
25
 
26
% matlab struct for triangulation
26
% matlab struct for triangulation
27
camStereoParams = stereoParameters(camParams0, camParams1, calibration.R1', calibration.T1');
27
camStereoParams = stereoParameters(camParams0, camParams1, calibration.R1', calibration.T1');
28
 
28
 
29
nSubScans = length(initialAlign);
29
nSubScans = length(initialAlign);
30
 
30
 
31
% 3D coordinates of markers in local camera frame
31
% 3D coordinates of markers in local camera frame
32
E = cell(nSubScans, 1);
32
E = cell(nSubScans, 1);
33
 
33
 
34
% 3D coordinates of markers in global initial alignment
34
% 3D coordinates of markers in global initial alignment
35
Eg = cell(size(E));
35
Eg = cell(size(E));
36
 
36
 
37
% find 3D markers coordinates 
37
% find 3D markers coordinates 
38
for i=1:nSubScans
38
for i=1:nSubScans
39
 
39
%for i=5:5
40
    % load point cloud
40
    % load point cloud
41
    pc = pcread(initialAlign(i).FileName);
41
    pc = pcread(initialAlign(i).FileName);
42
    Q = pc.Location;
42
    Q = pc.Location;
43
    idString = strsplit(initialAlign(i).FileName, {'.ply', '_'});
43
    idString = strsplit(initialAlign(i).FileName, {'.ply', '_'});
44
    idString = idString{end-1};
44
    idString = idString{end-1};
45
    
45
    
46
    % load white frames
46
    % load white frames
47
    scanDir = strsplit(initialAlign(i).FileName, '/');
47
    scanDir = strsplit(initialAlign(i).FileName, '/');
48
    scanDir = fullfile(scanDir{1:end-1});
48
    scanDir = fullfile(scanDir{1:end-1});
49
    frame0 = imread(fullfile(scanDir, ['sequence_' idString], 'frames0_0.png'));
49
    frame0 = imread(fullfile(scanDir, ['sequence_' idString], 'frames0_0.png'));
50
    frame1 = imread(fullfile(scanDir, ['sequence_' idString], 'frames1_0.png'));
50
    frame1 = imread(fullfile(scanDir, ['sequence_' idString], 'frames1_0.png'));
51
 
51
 
52
    e0Coords = autoDetectMarkers(frame0, P0, Q);
52
    e0Coords = autoDetectMarkers(frame0, P0, Q);
53
    e1Coords = autoDetectMarkers(frame1, P1, Q);
53
    e1Coords = autoDetectMarkers(frame1, P1, Q);
54
    
54
    
55
    %e0Coords = manuallyDetectMarkers(frame0, P0, Q);
55
    %e0Coords = manuallyDetectMarkers(frame0, P0, Q);
56
    %e1Coords = manuallyDetectMarkers(frame1, P1, Q);
56
    %e1Coords = manuallyDetectMarkers(frame1, P1, Q);
57
    
57
    
58
    %e0Coords = undistortPoints(e0Coords, camParams0);
58
    e0Coords = undistortPoints(e0Coords, camParams0);
59
    %e1Coords = undistortPoints(e1Coords, camParams1);
59
    e1Coords = undistortPoints(e1Coords, camParams1);
60
 
60
 
61
    % match ellipse candidates between cameras based on projection
61
    % match ellipse candidates between cameras based on projection
62
    E0 = projectOntoPointCloud(e0Coords, P0, Q);
62
    E0 = projectOntoPointCloud(e0Coords, P0, Q);
63
    E1 = projectOntoPointCloud(e1Coords, P1, Q);
63
    E1 = projectOntoPointCloud(e1Coords, P1, Q);
64
 
64
 
65
    matchedPairs = nan(size(E0, 1), 2);
65
    matchedPairs = nan(size(E0, 1), 2);
66
    nMatchedPairs = 0;
66
    nMatchedPairs = 0;
67
    for j=1:size(E0, 1)
67
    for j=1:size(E0, 1)
68
        
68
        
69
        % should use pdist2 instead..
69
        % should use pdist2 instead..
70
        sqDists = sum((E1 - repmat(E0(j,:), size(E1, 1), 1)).^2, 2);
70
        sqDists = sum((E1 - repmat(E0(j,:), size(E1, 1), 1)).^2, 2);
71
        
71
        
72
        [minSqDist, minSqDistIdx] = min(sqDists);
72
        [minSqDist, minSqDistIdx] = min(sqDists);
73
 
73
 
74
        if(minSqDist < 1^2)
74
        if(minSqDist < 1^2)
75
            nMatchedPairs = nMatchedPairs + 1;
75
            nMatchedPairs = nMatchedPairs + 1;
76
            matchedPairs(nMatchedPairs, :) = [j, minSqDistIdx];
76
            matchedPairs(nMatchedPairs, :) = [j, minSqDistIdx];
77
        end
77
        end
78
    end
78
    end
79
    matchedPairs = matchedPairs(1:nMatchedPairs, :);
79
    matchedPairs = matchedPairs(1:nMatchedPairs, :);
80
    
80
    
81
    % triangulate marker centers (lens correction has been performed)
81
    % triangulate marker centers (lens correction has been performed)
82
    [E{i}, e] = triangulate(e0Coords(matchedPairs(:, 1),:), e1Coords(matchedPairs(:, 2),:), camStereoParams);
82
    [E{i}, e] = triangulate(e0Coords(matchedPairs(:, 1),:), e1Coords(matchedPairs(:, 2),:), camStereoParams);
83
    E{i} = E{i}(e<3.0, :);
83
    E{i} = E{i}(e<3.0, :);
84
    display(e);
84
    display(e);
85
    
85
    
86
    % write point cloud with marker (debugging)
86
    % write point cloud with marker (debugging)
87
    pcDebug = pointCloud([pc.Location; E{i}], 'Color', [pc.Color; repmat([255, 0, 0], size(E{i}, 1), 1)]);
87
    pcDebug = pointCloud([pc.Location; E{i}], 'Color', [pc.Color; repmat([255, 0, 0], size(E{i}, 1), 1)]);
88
    pcwrite(pcDebug, 'pcDebug.ply');
88
    pcwrite(pcDebug, 'pcDebug.ply');
89
    
89
    
90
    % bring markers into initial alignment
90
    % bring markers into initial alignment
91
    [U,~,V] = svd(initialAlign(i).Rotation);
91
    [U,~,V] = svd(initialAlign(i).Rotation);
92
    Ri = U*V';
92
    Ri = U*V';
93
    Ti = initialAlign(i).Translation;
93
    Ti = initialAlign(i).Translation;
94
    
94
    
95
    Eg{i} = E{i}*Ri' + repmat(Ti', size(E{i}, 1), 1);
95
    Eg{i} = E{i}*Ri' + repmat(Ti', size(E{i}, 1), 1);
96
end
96
end
97
 
97
 
98
% show found markers in initial alignment
98
% show found markers in initial alignment
99
figure;
99
figure;
100
hold('on');
100
hold('on');
101
for i=1:nSubScans
101
for i=1:nSubScans
102
    % fix Ri to be orthogonal
102
    % fix Ri to be orthogonal
103
    [U,~,V] = svd(initialAlign(i).Rotation);
103
    [U,~,V] = svd(initialAlign(i).Rotation);
104
    Ri = U*V';
104
    Ri = U*V';
105
    
105
    
106
    % bring point cloud into initial alignment
106
    % bring point cloud into initial alignment
107
    pc = pcread(initialAlign(i).FileName);
107
    pc = pcread(initialAlign(i).FileName);
108
    tform = affine3d([Ri' [0;0;0]; initialAlign(i).Translation' 1]);
108
    tform = affine3d([Ri' [0;0;0]; initialAlign(i).Translation' 1]);
109
    pcg = pctransform(pc, tform);
109
    pcg = pctransform(pc, tform);
110
   
110
   
111
    pcshow(pcg);
111
    pcshow(pcg);
112
    xlabel('x');
112
    xlabel('x');
113
    ylabel('y');
113
    ylabel('y');
114
    zlabel('z');
114
    zlabel('z');
115
    
115
    
116
    plot3(Eg{i}(:,1), Eg{i}(:,2), Eg{i}(:,3), '.', 'MarkerSize', 15);
116
    plot3(Eg{i}(:,1), Eg{i}(:,2), Eg{i}(:,3), '.', 'MarkerSize', 15);
117
    title('Initial Alignment');
117
    title('Initial Alignment');
118
end
118
end
119
 
119
 
120
% match markers between poses using initial alignment
120
% match markers between poses using initial alignment
121
Pg = {};
121
Pg = {};
122
P = {};
122
P = {};
123
for i=1:nSubScans
123
for i=1:nSubScans
124
    for j=1:size(Eg{i}, 1)
124
    for j=1:size(Eg{i}, 1)
125
        pg = Eg{i}(j,:);
125
        pg = Eg{i}(j,:);
126
        p = E{i}(j,:);
126
        p = E{i}(j,:);
127
        matched = false;
127
        matched = false;
128
        for k=1:size(Pg, 2)
128
        for k=1:size(Pg, 2)
129
            clusterCenter = mean(cat(1, Pg{:,k}), 1);
129
            clusterCenter = mean(cat(1, Pg{:,k}), 1);
130
            if(sum((pg - clusterCenter).^2) < 3^2)
130
            if(sum((pg - clusterCenter).^2) < 3^2)
131
                % store in global frame
131
                % store in global frame
132
                Pg{i,k} = pg;
132
                Pg{i,k} = pg;
133
                % store in local frame
133
                % store in local frame
134
                P{i,k} = p;
134
                P{i,k} = p;
135
                matched = true;
135
                matched = true;
136
                break;
136
                break;
137
            end
137
            end
138
        end
138
        end
139
        % create new cluster
139
        % create new cluster
140
        if(not(matched))
140
        if(not(matched))
141
            Pg{i,end+1} = pg;
141
            Pg{i,end+1} = pg;
142
            P{i,end+1} = p;
142
            P{i,end+1} = p;
143
        end 
143
        end 
144
    end
144
    end
145
end
145
end
146
 
146
 
147
% run optimization
147
% run optimization
148
alignment = groupwiseOrthogonalProcrustes(P, initialAlign);
148
alignment = groupwiseOrthogonalProcrustes(P, initialAlign);
149
 
149
 
150
% show found markers in optimized alignment
150
% show found markers in optimized alignment
151
figure;
151
figure;
152
hold('on');
152
hold('on');
153
for i=1:nSubScans
153
for i=1:nSubScans
154
    % fix Ri to be orthogonal
154
    % fix Ri to be orthogonal
155
    [U,~,V] = svd(alignment(i).Rotation);
155
    [U,~,V] = svd(alignment(i).Rotation);
156
    Ri = U*V';
156
    Ri = U*V';
157
    Ti = alignment(i).Translation;
157
    Ti = alignment(i).Translation;
158
    
158
    
159
    Ea = E{i}*Ri' + repmat(Ti', size(E{i}, 1), 1);
159
    Ea = E{i}*Ri' + repmat(Ti', size(E{i}, 1), 1);
160
    
160
    
161
    % bring point cloud into optimized alignment
161
    % bring point cloud into optimized alignment
162
    pc = pcread(initialAlign(i).FileName);
162
    pc = pcread(initialAlign(i).FileName);
163
    tform = affine3d([Ri' [0;0;0]; initialAlign(i).Translation' 1]);
163
    tform = affine3d([Ri' [0;0;0]; initialAlign(i).Translation' 1]);
164
    pcg = pctransform(pc, tform);
164
    pcg = pctransform(pc, tform);
165
   
165
   
166
    pcshow(pcg);
166
    pcshow(pcg);
167
    xlabel('x');
167
    xlabel('x');
168
    ylabel('y');
168
    ylabel('y');
169
    zlabel('z');
169
    zlabel('z');
170
    
170
    
171
    plot3(Ea(:,1), Ea(:,2), Ea(:,3), '.', 'MarkerSize', 15);
171
    plot3(Ea(:,1), Ea(:,2), Ea(:,3), '.', 'MarkerSize', 15);
172
    title('Optimized Alignment');
172
    title('Optimized Alignment');
173
end
173
end
174
 
174
 
175
% write to ALN file
175
% write to ALN file
176
for i=1:length(alignment)
176
for i=1:length(alignment)
177
    alignment(i).FileName = initialAlign(i).FileName;
177
    alignment(i).FileName = initialAlign(i).FileName;
178
end
178
end
179
 
179
 
180
writeMeshLabALN(alignment, strrep(alnFileName, '.aln', 'Optimized.aln'));
180
writeMeshLabALN(alignment, strrep(alnFileName, '.aln', 'Optimized.aln'));
181
 
181
 
182
end
182
end
183
 
183
 
184
function e = autoDetectMarkers(frame, P, pointCloud)
184
function e = autoDetectMarkers(frame, P, pointCloud)
185
 
185
 
186
    % create mask based on morphology
186
    % create mask based on morphology
187
    bw = imbinarize(rgb2gray(frame));
187
    g = rgb2gray(frame);
-
 
188
    g(g>254) = 0;
-
 
189
    bw = imbinarize(g, 'adaptive', 'Sensitivity', 10^(-50));
188
    cc = bwconncomp(bw);
190
    cc = bwconncomp(bw);
189
    rp = regionprops(cc, 'Area', 'Solidity', 'Eccentricity', 'Centroid');
191
    rp = regionprops(cc, 'Area', 'Solidity', 'Eccentricity', 'Centroid');
190
    idx = ([rp.Area] > 100 & [rp.Area] < 1000 & [rp.Solidity] > 0.9);
192
    idx = ([rp.Area] > 100 & [rp.Area] < 1000 & [rp.Solidity] > 0.9);
191
    
193
    
192
    initialGuesses = cat(1, rp(idx).Centroid);
194
    initialGuesses = cat(1, rp(idx).Centroid);
193
 
195
 
194
    [e, ~] = detectMarkersSubpix(frame, initialGuesses, P, pointCloud);
196
    [e, ~] = detectMarkersSubpix(frame, initialGuesses, P, pointCloud);
195
 
197
 
196
    figure; 
198
    figure; 
197
    imshow(frame);
199
    imshow(frame);
198
        hold('on');
200
        hold('on');
199
    plot(e(:,1), e(:,2), 'rx', 'MarkerSize', 15);
201
    plot(e(:,1), e(:,2), 'rx', 'MarkerSize', 15);
200
    drawnow;
202
    drawnow;
201
end
203
end
202
 
204
 
203
function e = manuallyDetectMarkers(frame, P, pointCloud)
205
function e = manuallyDetectMarkers(frame, P, pointCloud)
204
    
206
    
205
    e = [];
207
    e = [];
206
	%edges = edge(rgb2gray(frame), 'Canny', [0.08 0.1], 2);
208
	%edges = edge(rgb2gray(frame), 'Canny', [0.08 0.1], 2);
207
 
209
 
208
    figure; 
210
    figure; 
209
    hold('on');
211
    hold('on');
210
    imshow(frame);
212
    imshow(frame);
211
    title('Close figure to end.');
213
    title('Close figure to end.');
212
    set(gcf, 'pointer', 'crosshair'); 
214
    set(gcf, 'pointer', 'crosshair'); 
213
    set(gcf, 'WindowButtonDownFcn', @clickCallback);
215
    set(gcf, 'WindowButtonDownFcn', @clickCallback);
214
    
216
    
215
    uiwait;
217
    uiwait;
216
 
218
 
217
    function clickCallback(caller, ~)
219
    function clickCallback(caller, ~)
218
        
220
        
219
        p = get(gca, 'CurrentPoint'); 
221
        p = get(gca, 'CurrentPoint'); 
220
        p = p(1, 1:2);
222
        p = p(1, 1:2);
221
 
223
 
222
        [el, ~] = detectMarkersSubpix(frame, p, P, pointCloud);
224
        [el, ~] = detectMarkersSubpix(frame, p, P, pointCloud);
223
        e = [e; el(:, 1:2)];
225
        e = [e; el(:, 1:2)];
224
        
226
        
225
        if(not(isempty(el)))
227
        if(not(isempty(el)))
226
            figure(caller);
228
            figure(caller);
227
            hold('on');
229
            hold('on');
228
            plot(el(1), el(2), 'rx', 'MarkerSize', 15);
230
            plot(el(1), el(2), 'rx', 'MarkerSize', 15);
229
        end
231
        end
230
    end
232
    end
231
    
233
    
232
end
234
end
233
 
235
 
234
function [e, conf] = detectMarkersSubpix(frame, initGuesses, P, Q)
236
function [e, conf] = detectMarkersSubpix(frame, initGuesses, P, Q)
235
 
237
 
236
    % create mask based on morphology
238
    % create mask based on morphology
237
    bw = imbinarize(rgb2gray(frame));
239
    g = rgb2gray(frame);
-
 
240
    g(g>254) = 0;
-
 
241
    bw = imbinarize(g);
238
    cc = bwconncomp(bw);
242
    cc = bwconncomp(bw);
239
    labels = labelmatrix(cc);
243
    labels = labelmatrix(cc);
240
 
244
 
241
    % project point cloud into image
245
    % project point cloud into image
242
    q = [Q ones(size(Q,1),1)]*P;
246
    q = [Q ones(size(Q,1),1)]*P;
243
    q = q./[q(:,3) q(:,3) q(:,3)];
247
    q = q./[q(:,3) q(:,3) q(:,3)];
244
    
248
    
245
    e = zeros(size(initGuesses));
249
    e = zeros(size(initGuesses));
246
    conf = zeros(size(initGuesses, 1), 1);
250
    conf = zeros(size(initGuesses, 1), 1);
247
    
251
    
248
    nMarkersFound = 0;
252
    nMarkersFound = 0;
249
    
253
    
250
    for i=1:size(initGuesses, 1)
254
    for i=1:size(initGuesses, 1)
251
        
255
        
252
        labelId = labels(round(initGuesses(i,2)), round(initGuesses(i,1)));
256
        labelId = labels(round(initGuesses(i,2)), round(initGuesses(i,1)));
253
        labelMask = (labels == labelId);
257
        labelMask = (labels == labelId);
254
        labelMask = imdilate(labelMask, strel('disk', 3, 0));
258
        labelMask = imdilate(labelMask, strel('disk', 3, 0));
255
        
259
        
256
        if(sum(sum(labelMask)) < 10 || sum(sum(labelMask)) > 1000)
260
        if(sum(sum(labelMask)) < 10 || sum(sum(labelMask)) > 1000)
257
            continue;
261
            continue;
258
        end
262
        end
259
        
263
        
260
        % determine 3D points that are part of the marker
264
        % determine 3D points that are part of the marker
261
        % note: we should probably undistort labelMask
265
        % note: we should probably undistort labelMask
262
        pointMask = false(size(q, 1), 1);
266
        pointMask = false(size(q, 1), 1);
263
        for j=1:size(q,1)
267
        for j=1:size(q,1)
264
            if(round(q(j,2)) > size(labelMask, 1) || round(q(j,1)) > size(labelMask, 2) || round(q(j,2)) < 1 || round(q(j,1)) < 1)
268
            if(round(q(j,2)) > size(labelMask, 1) || round(q(j,1)) > size(labelMask, 2) || round(q(j,2)) < 1 || round(q(j,1)) < 1)
265
                continue;
269
                continue;
266
            end
270
            end
267
            
271
            
268
            if(labelMask(round(q(j,2)), round(q(j,1))))
272
            if(labelMask(round(q(j,2)), round(q(j,1))))
269
                pointMask(j) = true;
273
                pointMask(j) = true;
270
            end
274
            end
271
        end
275
        end
272
        
276
        
273
        if(sum(pointMask)) < 10
277
        if(sum(pointMask)) < 10
274
            continue;
278
            continue;
275
        end
279
        end
276
        
280
        
277
        % project 3D points onto local plane
281
        % project 3D points onto local plane
278
        [~, sc, ~] = pca(Q(pointMask, :));
282
        [~, sc, ~] = pca(Q(pointMask, :));
279
        Qlocal = sc(:, 1:2);
283
        Qlocal = sc(:, 1:2);
280
        
284
        
281
        % synthetic marker in high res. space
285
        % synthetic marker in high res. space
282
        m = zeros(151, 151);
286
        m = zeros(151, 151);
283
        [x, y] = meshgrid(1:151, 1:151);
287
        [x, y] = meshgrid(1:151, 1:151);
284
        m((x(:)-76).^2 + (y(:)-76).^2 <= 50^2) = 1.0;
288
        m((x(:)-76).^2 + (y(:)-76).^2 <= 50^2) = 1.0;
285
        
289
        
286
        % relation between marker space (px) and true marker/local plane(mm)
290
        % relation between marker space (px) and true marker/local plane(mm)
287
        % true marker diameter is 1.75mm
291
        % true marker diameter is 1.75mm
288
        mScale = 101/1.8; %px/mm
292
        mScale = 101/1.4; %px/mm
289
        mShift = 76; %px
293
        mShift = 76; %px
290
        
294
        
291
        % build homography from image to marker space
295
        % build homography from image to marker space
292
        %H = fitgeotrans(q(pointMask, 1:2), mScale*Qlocal+mShift,  'projective');
296
        H = fitgeotrans(q(pointMask, 1:2), mScale*Qlocal+mShift,  'projective');
293
        Hdlt = Hest_DLT([mScale*Qlocal+mShift, ones(size(Qlocal, 1), 1)]', q(pointMask,:)');
297
        %Hdlt = Hest_DLT([mScale*Qlocal+mShift, ones(size(Qlocal, 1), 1)]', q(pointMask,:)');
294
        H = projective2d(Hdlt');
298
        %H = projective2d(Hdlt');
295
        
299
        
296
        % bring image of marker into marker space
300
        % bring image of marker into marker space
297
        imMarkerSpace = imwarp(frame, H, 'OutputView', imref2d(size(m)));
301
        imMarkerSpace = imwarp(frame, H, 'OutputView', imref2d(size(m)));
298
        imMarkerSpace = rgb2gray(im2double(imMarkerSpace));
302
        imMarkerSpace = rgb2gray(im2double(imMarkerSpace));
299
        
303
        
300
        %figure; imshowpair(imMarkerSpace, m);
304
        %figure; imshowpair(imMarkerSpace, m);
301
        
305
        
302
        % perform image registration
306
        % perform image registration
303
        % might be better off using subpixel image correlation
307
        % might be better off using subpixel image correlation
304
        [opt, met] = imregconfig('multimodal');
308
        [opt, met] = imregconfig('multimodal');
305
        T = imregtform(m, imMarkerSpace, 'translation', opt, met, 'DisplayOptimization', false);
309
        T = imregtform(m, imMarkerSpace, 'translation', opt, met, 'DisplayOptimization', false);
306
        
310
        
307
        rege = imwarp(m, T, 'OutputView', imref2d(size(m)));
311
        rege = imwarp(m, T, 'OutputView', imref2d(size(m)));
308
        %figure; imshowpair(imMarkerSpace, rege);
312
        %figure; imshowpair(imMarkerSpace, rege);
309
        
313
        
310
        % measure of correlation
314
        % measure of correlation
311
        confI = sum(sum(imMarkerSpace .* rege))/sqrt(sum(sum(imMarkerSpace) * sum(sum(rege))));
315
        confI = sum(sum(imMarkerSpace .* rege))/sqrt(sum(sum(imMarkerSpace) * sum(sum(rege))));
312
        confI = 1.0;
316
        %confI = 1.0;
313
        
317
        
314
        if confI<0.4
318
        if confI<0.4
315
            continue;
319
            continue;
316
        end
320
        end
317
        
321
        
318
        fprintf('Found marker with confidence: %f\n', confI);
322
        fprintf('Found marker with confidence: %f\n', confI);
319
            
323
            
320
        % transform marker space coordinates (76,76) to frame space
324
        % transform marker space coordinates (76,76) to frame space
321
        el = T.transformPointsForward([76, 76]);
325
        el = T.transformPointsForward([76, 76]);
322
        el = H.transformPointsInverse(el);
326
        el = H.transformPointsInverse(el);
323
        
327
        
324
        nMarkersFound = nMarkersFound+1;
328
        nMarkersFound = nMarkersFound+1;
325
        e(nMarkersFound,:) = el;
329
        e(nMarkersFound,:) = el;
326
        conf(nMarkersFound) = confI;
330
        conf(nMarkersFound) = confI;
327
    end
331
    end
328
    
332
    
329
    e = e(1:nMarkersFound, :);
333
    e = e(1:nMarkersFound, :);
330
    conf = conf(1:nMarkersFound);
334
    conf = conf(1:nMarkersFound);
331
end
335
end
332
 
336
 
333
function E = projectOntoPointCloud(e, P, pointCloud)
337
function E = projectOntoPointCloud(e, P, pointCloud)
334
 
338
 
335
    q = [pointCloud ones(size(pointCloud,1),1)]*P;
339
    q = [pointCloud ones(size(pointCloud,1),1)]*P;
336
    q = q(:,1:2)./[q(:,3) q(:,3)];
340
    q = q(:,1:2)./[q(:,3) q(:,3)];
337
 
341
 
338
    E = nan(size(e,1), 3);
342
    E = nan(size(e,1), 3);
339
    
343
    
340
    for i=1:size(e, 1)
344
    for i=1:size(e, 1)
341
        sqDists = sum((q - repmat(e(i,:), size(q, 1), 1)).^2, 2);
345
        sqDists = sum((q - repmat(e(i,:), size(q, 1), 1)).^2, 2);
342
        
346
        
343
        [sqDistsSorted, sortIdx] = sort(sqDists);
347
        [sqDistsSorted, sortIdx] = sort(sqDists);
344
        
348
        
345
        neighbors = (sqDistsSorted < 4.0^2);
349
        neighbors = (sqDistsSorted < 4.0^2);
346
        
350
        
347
        distsSorted = sqrt(sqDistsSorted(neighbors));
351
        distsSorted = sqrt(sqDistsSorted(neighbors));
348
        invDistsSorted = 1.0/distsSorted;
352
        invDistsSorted = 1.0/distsSorted;
349
        sortIdx = sortIdx(neighbors);
353
        sortIdx = sortIdx(neighbors);
350
        
354
        
351
        nNeighbors = sum(neighbors);
355
        nNeighbors = sum(neighbors);
352
        
356
        
353
        if(nNeighbors >= 2)
357
        if(nNeighbors >= 2)
354
            E(i, :) = 0;
358
            E(i, :) = 0;
355
            for j=1:nNeighbors
359
            for j=1:nNeighbors
356
                E(i, :) = E(i, :) + invDistsSorted(j)/sum(invDistsSorted) * pointCloud(sortIdx(j), :);
360
                E(i, :) = E(i, :) + invDistsSorted(j)/sum(invDistsSorted) * pointCloud(sortIdx(j), :);
357
            end
361
            end
358
        end
362
        end
359
            
363
            
360
    end    
364
    end    
361
end
365
end
362
 
366
 
363
function H = Hest_DLT(q1, q2)
367
function H = Hest_DLT(q1, q2)
364
    % Estimate the homography between a set of point correspondences using the 
368
    % Estimate the homography between a set of point correspondences using the 
365
    % direct linear transform algorithm.
369
    % direct linear transform algorithm.
366
    %
370
    %
367
    % Input:
371
    % Input:
368
    %           q1: 3xN matrix of homogenous point coordinates from camera 1. 
372
    %           q1: 3xN matrix of homogenous point coordinates from camera 1. 
369
    %           q2: 3xN matrix of corresponding points from camera 2.
373
    %           q2: 3xN matrix of corresponding points from camera 2.
370
    % Output:
374
    % Output:
371
    %           H: 3x3 matrix. The Fundamental Matrix estimate. 
375
    %           H: 3x3 matrix. The Fundamental Matrix estimate. 
372
    %
376
    %
373
    % Note that N must be at least 4.
377
    % Note that N must be at least 4.
374
    % See derivation in Aanaes, Lecture Notes on Computer Vision, 2011
378
    % See derivation in Aanaes, Lecture Notes on Computer Vision, 2011
375
 
379
 
376
    % Normalize points
380
    % Normalize points
377
    [T1,invT1] = normalizationMat(q1);
381
    [T1,invT1] = normalizationMat(q1);
378
    q1_tilde = T1*q1;
382
    q1_tilde = T1*q1;
379
 
383
 
380
    T2 = normalizationMat(q2);
384
    T2 = normalizationMat(q2);
381
    q2_tilde = T2*q2;
385
    q2_tilde = T2*q2;
382
 
386
 
383
    % DLT estimation
387
    % DLT estimation
384
    N = size(q1_tilde,2);
388
    N = size(q1_tilde,2);
385
    assert(size(q2_tilde,2)==N);
389
    assert(size(q2_tilde,2)==N);
386
 
390
 
387
    B = zeros(3*N,9);
391
    B = zeros(3*N,9);
388
 
392
 
389
    for i=1:N
393
    for i=1:N
390
        q1i = q1_tilde(:,i);
394
        q1i = q1_tilde(:,i);
391
        q2i = q2_tilde(:,i);
395
        q2i = q2_tilde(:,i);
392
        q1_x = [0 -q1i(3) q1i(2); q1i(3) 0 -q1i(1); -q1i(2) q1i(1) 0];
396
        q1_x = [0 -q1i(3) q1i(2); q1i(3) 0 -q1i(1); -q1i(2) q1i(1) 0];
393
        biT = kron(q2i', q1_x); 
397
        biT = kron(q2i', q1_x); 
394
        B(3*(i-1)+1:3*i, :) = biT;
398
        B(3*(i-1)+1:3*i, :) = biT;
395
    end
399
    end
396
 
400
 
397
    [U,S,~] = svd(B');
401
    [U,S,~] = svd(B');
398
 
402
 
399
    [~,idx] = min(diag(S));
403
    [~,idx] = min(diag(S));
400
    h = U(:,idx);
404
    h = U(:,idx);
401
 
405
 
402
    H_tilde = reshape(h, 3, 3);
406
    H_tilde = reshape(h, 3, 3);
403
 
407
 
404
    % Unnormalize H
408
    % Unnormalize H
405
    H = invT1*H_tilde*T2;
409
    H = invT1*H_tilde*T2;
406
 
410
 
407
    % Arbitrarily chose scale
411
    % Arbitrarily chose scale
408
    H = H * 1/H(3,3);
412
    H = H * 1/H(3,3);
409
end
413
end
410
 
414
 
411
function [T,invT] = normalizationMat(q)
415
function [T,invT] = normalizationMat(q)
412
    % Gives a normalization matrix for homogeneous coordinates
416
    % Gives a normalization matrix for homogeneous coordinates
413
    % such that T*q will have zero mean and unit variance.
417
    % such that T*q will have zero mean and unit variance.
414
    % See Aanaes, Computer Vision Lecture Notes 2.8.2
418
    % See Aanaes, Computer Vision Lecture Notes 2.8.2
415
    %
419
    %
416
    % q: (M+1)xN matrix of N MD points in homogenous coordinates
420
    % q: (M+1)xN matrix of N MD points in homogenous coordinates
417
    %
421
    %
418
    % Extended to also efficiently compute the inverse matrix
422
    % Extended to also efficiently compute the inverse matrix
419
    % DTU, 2013, Jakob Wilm
423
    % DTU, 2013, Jakob Wilm
420
 
424
 
421
    [M,N] = size(q);
425
    [M,N] = size(q);
422
    M = M-1;
426
    M = M-1;
423
 
427
 
424
    mu = mean(q(1:M,:),2);
428
    mu = mean(q(1:M,:),2);
425
 
429
 
426
    q_bar = q(1:M,:)-repmat(mu,1,N);
430
    q_bar = q(1:M,:)-repmat(mu,1,N);
427
 
431
 
428
    s = mean(sqrt(diag(q_bar'*q_bar)))/sqrt(2);
432
    s = mean(sqrt(diag(q_bar'*q_bar)))/sqrt(2);
429
 
433
 
430
    T = [eye(M)/s, -mu/s; zeros(1,M) 1];
434
    T = [eye(M)/s, -mu/s; zeros(1,M) 1];
431
 
435
 
432
    invT = [eye(M)*s, mu; zeros(1,M) 1];
436
    invT = [eye(M)*s, mu; zeros(1,M) 1];
433
end
437
end
434
 
438
 
435

Generated by GNU Enscript 1.6.6.
439

Generated by GNU Enscript 1.6.6.
436
 
440
 
437
 
441
 
438
 
442