Subversion Repositories seema-scanner

Rev

Rev 218 | Rev 236 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 218 Rev 219
Line 287... Line 287...
287
        % true marker diameter is 1.75mm
287
        % true marker diameter is 1.75mm
288
        mScale = 101/1.8; %px/mm
288
        mScale = 101/1.8; %px/mm
289
        mShift = 76; %px
289
        mShift = 76; %px
290
        
290
        
291
        % build homography from image to marker space
291
        % build homography from image to marker space
292
        H = fitgeotrans(q(pointMask, 1:2), mScale*Qlocal+mShift,  'projective');
292
        %H = fitgeotrans(q(pointMask, 1:2), mScale*Qlocal+mShift,  'projective');
-
 
293
        Hdlt = Hest_DLT([mScale*Qlocal+mShift, ones(size(Qlocal, 1), 1)]', q(pointMask,:)');
-
 
294
        H = projective2d(Hdlt');
293
        
295
        
294
        % bring image of marker into marker space
296
        % bring image of marker into marker space
295
        imMarkerSpace = imwarp(frame, H, 'OutputView', imref2d(size(m)));
297
        imMarkerSpace = imwarp(frame, H, 'OutputView', imref2d(size(m)));
296
        imMarkerSpace = rgb2gray(im2double(imMarkerSpace));
298
        imMarkerSpace = rgb2gray(im2double(imMarkerSpace));
297
        
299
        
Line 305... Line 307...
305
        rege = imwarp(m, T, 'OutputView', imref2d(size(m)));
307
        rege = imwarp(m, T, 'OutputView', imref2d(size(m)));
306
        %figure; imshowpair(imMarkerSpace, rege);
308
        %figure; imshowpair(imMarkerSpace, rege);
307
        
309
        
308
        % measure of correlation
310
        % measure of correlation
309
        confI = sum(sum(imMarkerSpace .* rege))/sqrt(sum(sum(imMarkerSpace) * sum(sum(rege))));
311
        confI = sum(sum(imMarkerSpace .* rege))/sqrt(sum(sum(imMarkerSpace) * sum(sum(rege))));
-
 
312
        confI = 1.0;
310
        
313
        
311
        if confI<0.4
314
        if confI<0.4
312
            continue;
315
            continue;
313
        end
316
        end
314
        
317
        
Line 355... Line 358...
355
        end
358
        end
356
            
359
            
357
    end    
360
    end    
358
end
361
end
359
 
362
 
-
 
363
function H = Hest_DLT(q1, q2)
-
 
364
    % Estimate the homography between a set of point correspondences using the 
-
 
365
    % direct linear transform algorithm.
-
 
366
    %
-
 
367
    % Input:
-
 
368
    %           q1: 3xN matrix of homogenous point coordinates from camera 1. 
-
 
369
    %           q2: 3xN matrix of corresponding points from camera 2.
-
 
370
    % Output:
-
 
371
    %           H: 3x3 matrix. The Fundamental Matrix estimate. 
-
 
372
    %
-
 
373
    % Note that N must be at least 4.
-
 
374
    % See derivation in Aanaes, Lecture Notes on Computer Vision, 2011
-
 
375
 
-
 
376
    % Normalize points
-
 
377
    [T1,invT1] = normalizationMat(q1);
-
 
378
    q1_tilde = T1*q1;
-
 
379
 
-
 
380
    T2 = normalizationMat(q2);
-
 
381
    q2_tilde = T2*q2;
-
 
382
 
-
 
383
    % DLT estimation
-
 
384
    N = size(q1_tilde,2);
-
 
385
    assert(size(q2_tilde,2)==N);
-
 
386
 
-
 
387
    B = zeros(3*N,9);
-
 
388
 
-
 
389
    for i=1:N
-
 
390
        q1i = q1_tilde(:,i);
-
 
391
        q2i = q2_tilde(:,i);
-
 
392
        q1_x = [0 -q1i(3) q1i(2); q1i(3) 0 -q1i(1); -q1i(2) q1i(1) 0];
-
 
393
        biT = kron(q2i', q1_x); 
-
 
394
        B(3*(i-1)+1:3*i, :) = biT;
-
 
395
    end
-
 
396
 
-
 
397
    [U,S,~] = svd(B');
-
 
398
 
-
 
399
    [~,idx] = min(diag(S));
-
 
400
    h = U(:,idx);
-
 
401
 
-
 
402
    H_tilde = reshape(h, 3, 3);
-
 
403
 
-
 
404
    % Unnormalize H
-
 
405
    H = invT1*H_tilde*T2;
-
 
406
 
-
 
407
    % Arbitrarily chose scale
-
 
408
    H = H * 1/H(3,3);
-
 
409
end
-
 
410
 
-
 
411
function [T,invT] = normalizationMat(q)
-
 
412
    % Gives a normalization matrix for homogeneous coordinates
-
 
413
    % such that T*q will have zero mean and unit variance.
-
 
414
    % See Aanaes, Computer Vision Lecture Notes 2.8.2
-
 
415
    %
-
 
416
    % q: (M+1)xN matrix of N MD points in homogenous coordinates
-
 
417
    %
-
 
418
    % Extended to also efficiently compute the inverse matrix
-
 
419
    % DTU, 2013, Jakob Wilm
-
 
420
 
-
 
421
    [M,N] = size(q);
-
 
422
    M = M-1;
-
 
423
 
-
 
424
    mu = mean(q(1:M,:),2);
-
 
425
 
-
 
426
    q_bar = q(1:M,:)-repmat(mu,1,N);
-
 
427
 
-
 
428
    s = mean(sqrt(diag(q_bar'*q_bar)))/sqrt(2);
-
 
429
 
-
 
430
    T = [eye(M)/s, -mu/s; zeros(1,M) 1];
-
 
431
 
-
 
432
    invT = [eye(M)*s, mu; zeros(1,M) 1];
-
 
433
end
360
 
434