Alex
Alex

Reputation: 1254

How to represent translation and scaling as a matrix?

I'm trying to implement the normalized 8-point algorithm to estimate the fundamental matrix. One step is to normalize the points such that they have center (0,0) and mean distance of sqrt(2). I know how to translate and scale the points to do so, but how do I represent the steps as a matrix to use at a later step?

My current function transforms the points as below, but I also need to figure out what the transformation matrix is:

%% Normalize points to have center (0,0) and mean distance sqrt(2)
function [pts1, T] = normalizePoints(pts)
    Xs = pts(:,1);
    Ys = pts(:,2);

    %% Compute old center and translate
    Xc = mean(Xs);
    Yc = mean(Ys);
    Xs = Xs - Xc;
    Ys = Ys - Yc;

    %% Compute mean distance and scale
    Ds = sqrt(Xs .^ 2 + Ys .^ 2);
    meanD = mean(Ds);
    scale = sqrt(2) / meanD;

    pts1 = [Xs .* scale, Ys .* scale];
    T = ... % How do I represent the previous operations as T?
end

Upvotes: 0

Views: 299

Answers (1)

user2999345
user2999345

Reputation: 4195

use Homogeneous coordinates:

T = [3;5];
% Normalize points to have center (0,0) and mean distance sqrt(2)
pts = rand(10,2);
Xs = pts(:,1);
Ys = pts(:,2);

% Compute old center and translate
Xc = mean(Xs);
Yc = mean(Ys);
Xs = Xs - Xc;
Ys = Ys - Yc;

% Compute mean distance and scale
Ds = sqrt(Xs .^ 2 + Ys .^ 2);
meanD = mean(Ds);
scale = sqrt(2) / meanD;

% composing transformation matrix
H = eye(3);
H([1,5]) = H([1,5])*scale;
H(1:2,3) = T(:);
% making homogenous coordinates (add ones as z values) 
ptsHomo = [pts';ones(1,size(pts,1))];
% apply transform
ptsRes = H*ptsHomo;
ptsRes = bsxfun(@rdivide,ptsRes(1:2,:),ptsRes(3,:))';

subplot(121);
plot(pts(:,1),pts(:,2),'o');
title('original points')

subplot(122);
plot(ptsRes(:,1),ptsRes(:,2),'o');
title('transformed points')

enter image description here

Upvotes: 1

Related Questions