Alignment and Object Instance Recognition: Computer Vision Jia-Bin Huang, Virginia Tech

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 71

Alignment and Object Instance

Recognition

Computer Vision
Jia-Bin Huang, Virginia Tech
Many slides from S. Lazebnik and D. Hoiem
Administrative Stuffs
• HW 2 due 11:55 PM Oct 3
Today’s class
• Review fitting

• Alignment

• Object instance recognition

• Going over HW 2
Previous class
• Global optimization / Search for parameters
• Least squares fit
• Robust least squares
• Iterative closest point (ICP)

• Hypothesize and test


• Generalized Hough transform
• RANSAC
Least squares line fitting
•Data: (x1, y1), …, (xn, yn)
y=mx+b
•Line equation: yi = m xi + b
•Find (m, b) to minimize
(xi, yi)
E  i 1 ( yi  m xi  b ) 2
n

2
2  x1 1  y1 
 m    m  
E  i 1   xi 1    yi             Ap  y
n 2

 b   b
 xn 1    yn 
 y T y  2( Ap)T y  ( Ap)T ( Ap)
dE
 2 A T Ap  2 A T y  0
dp Matlab: p = A \ y;

A T Ap  A T y  p   A T A  A T y
1

Modified from S. Lazebnik


Least squares line fitting
function [m, b] = lsqfit(x, y)
% y = mx + b
% find line that best predicts y given x
% minimize sum_i (m*x_i + b - y_i).^2
A = [x(:) ones(numel(x), 1)];
b = y(:);
p = A\b;
m = p(1);
b = p(2);

A p y
Slide modified from S. Lazebnik

Total least squares


Find (a, b, c) to minimize the sum of
squared perpendicular distances ax+by+c=0
Unit normal:
E  i 1 ( a xi  b yi  c )
n 2
E   (a x (x
n
i 1
 b y, yd ))
i
i i
i
2 N=(a, b)

E a n b n
 i 1 2( a xi  b yi  c )  0 c   i 1 xi  i 1 yi  a x  b y
n

c n n
2
 x1  xy1  y 
a 
E  i 1 ( a ( xi  x )  b( yi  y )) 2    
n
    p T A T Ap
  b 
 x n  x
y n  y 
p T A T Ap
minimize p T A T Ap s.t. p T p  1  minimize
pT p
Solution is eigenvector corresponding to smallest eigenvalue of ATA

See details on Raleigh Quotient: http://en.wikipedia.org/wiki/Rayleigh_quotient


Total least squares
function [m, b, err] = total_lsqfit(x, y)
% ax + by + c = 0
% distance to line for (a^2+b^2=1): dist_sq = (ax + by + c).^2
A = [x(:)-mean(x) y(:)-mean(y)];
[v, d] = eig(A'*A);
p = v(:, 1); % eigenvector corr. to smaller eigenvalue

% get a, b, c parameters
a = p(1);
b = p(2);
c = -(a*mean(x)+b*mean(y));
err = (a*x+b*y+c).^2;
A p
% convert to slope-intercept (m, b)
m = -a/b;
b = -c/b; % note: this b is for slope-intercept now
Robust Estimator
•1.  Initialize: e.g., choose by least squares fit and

  1.5  median  error 


error ( , data i ) 2
2. Choose params to minimize: i  2  error ( , data ) 2
• E.g., numerical optimization i

  1.5  median  error 


3. Compute new

4. Repeat (2) and (3) until convergence


function [m, b] = robust_lsqfit(x, y)
% iterative robust fit y = mx + b
% find line that best predicts y given x
% minimize sum_i (m*x_i + b - y_i).^2
[m, b] = lsqfit(x, y);
p = [m ; b];
err = sqrt((y-p(1)*x-p(2)).^2);
sigma = median(err)*1.5;
for k = 1:7
p = fminunc(@(p)geterr(p,x,y,sigma), p);
err = sqrt((y-p(1)*x-p(2)).^2);
sigma = median(err)*1.5;
end
m = p(1);
b = p(2);
Hough transform
P.V.C. Hough, Machine Analysis of Bubble Chamber Pictures, Proc. Int. Conf. High Energy
Accelerators and Instrumentation, 1959

Use a polar representation for the parameter space


x cos   y sin   

y



x
Hough space

Slide from S. Savarese


function [m, b] = houghfit(x, y)
% y = mx + b % smooth the bin counts
counts = imfilter(counts,
% x*cos(theta) + y*sin(theta) = r fspecial('gaussian', 5, 0.75));
% find line that best predicts y given x
% get best theta, rho and show counts
% minimize sum_i (m*x_i + b - y_i).^2 [maxval, maxind] = max(counts(:));
thetas = (-pi+pi/50):(pi/100):pi; [thetaind, rind] = ind2sub(size(counts),
maxind);
costhetas = cos(thetas); theta = thetas(thetaind);
sinthetas = sin(thetas); r = minr + stepr*(rind-1);

minr = 0; stepr = 0.005; maxr = 1; % convert to slope-intercept


b = r/sin(theta);
m = -cos(theta)/sin(theta);
% count hough votes
counts = zeros(numel(thetas),(maxr-minr)/stepr+1);
for k = 1:numel(x)
r = x(k)*costhetas + y(k)*sinthetas;
% only count parameters within the range of r
inrange = find(r >= minr & r <= maxr);
rnum = round((r(inrange)-minr)/stepr)+1;
ind = sub2ind(size(counts), inrange, rnum);
counts(ind) = counts(ind) + 1;
end
RANSAC


N I  14
Algorithm:
1. Sample (randomly) the number of points required to fit the model (#=2)
2. Solve for model parameters using samples
3. Score by the fraction of inliers within a preset threshold of the model

Repeat 1-3 until the best model is found with high confidence
function [m, b] = ransacfit(x, y)
% y = mx + b
N = 200;
thresh = 0.03;
bestcount = 0;

for k = 1:N
rp = randperm(numel(x));
tx = x(rp(1:2));
ty = y(rp(1:2));
m = (ty(2)-ty(1)) ./ (tx(2)-tx(1));
b = ty(2)-m*tx(2);

nin = sum(abs(y-m*x-b)<thresh);
if nin > bestcount
bestcount = nin;
inliers = (abs(y - m*x - b) < thresh);
end
end
% total least square fitting on inliers
[m, b] = total_lsqfit(x(inliers), y(inliers));
Line fitting demo
demo_linefit(npts, outliers, noise, method)

• npts: number of points


• outliers: number of outliers
• noise: noise level
• Method
• lsq: least squares
• tlsq: total least squares
• rlsq: robust least squares
• hough: hough transform
• ransac: RANSAC
Which algorithm should I use?
 If we know which points belong to the line, how
do we find the “optimal” line parameters?
Least squares

 What if there are outliers?


Robust fitting, RANSAC

• What if there are many lines?


• Voting methods: RANSAC, Hough transform

Slide credit: S. Lazebnik


Alignment as fitting
• Previous lectures: fitting a model to features in one
image M

xi Find model M that minimizes

 residual ( x , M )
i
i

• Alignment: fitting a model to a transformation


between pairs of features (matches) in two images
xi
xi' Find transformation T
T that minimizes

 residual (T ( x ), x)
i
i i
What if you want to align but have no prior
matched pairs?
• Hough transform and RANSAC not applicable

• Important applications

Medical imaging: match brain Robotics: match point clouds


scans or contours
Iterative Closest Points (ICP)
Algorithm
Goal: estimate transform between two dense sets of
points

1. Initialize transformation (e.g., compute difference in


means and scale)
2. Assign each point in {Set 1} to its nearest neighbor in {Set
2}
3. Estimate transformation parameters
• e.g., least squares or robust least squares
4. Transform the points in {Set 1} using estimated parameters
5. Repeat steps 2-4 until change is very small
Example: solving for translation

A1

A2 A3 B1

B2 B3

Given matched points in {A} and {B}, estimate the translation of the object
 xiB   xiA  t x 
 B    A   t 
 yi   yi   y 
Example: solving for translation
A1

A2 A3 (tx, ty) B1

B2 B3

Least squares solution


 xiB   xiA  t x 
 B    A   t 
1. Write down objective function
2. Derived solution
a) Compute derivative
 yi   yi   y 
b) Compute solution 1 0  x1B  x1A 
0  B 
1  y1A 
3. Computational solution   t x   y1
a) Write in form Ax=b        
  ty  
b) Solve using pseudo-inverse or eigenvalue 1 0    x nB  x nA 
decomposition 0 1  y nB  y nA 
Example: solving for translation
A1
A5
B4
A2 A3 (tx, ty) B1

A4
B2 B3
B5

Problem: outliers

RANSAC solution  xiB   xiA  t x 


1. Sample a set of matching points (1 pair)  B    A   t 
2. Solve for transformation parameters  yi   yi   y 
3. Score parameters with number of inliers
4. Repeat steps 1-3 N times
Example: solving for translation
B4
A1 B5 B6

A2 A3 (tx, ty) B1

A4 B2 B3
A5 A6

Problem: outliers, multiple objects, and/or many-to-one matches

Hough transform solution  xiB   xiA  t x 


1. Initialize a grid of parameter values  B    A   t 
2. Each matched pair casts a vote for consistent  yi   yi   y 
values
3. Find the parameters with the most votes
4. Solve using least squares with inliers
Example: solving for translation

(tx, ty)

Problem: no initial guesses for correspondence

ICP solution  xiB   xiA  t x 


1. Find nearest neighbors for each point  B    A   t 
2. Compute transform using matches  yi   yi   y 
3. Move points using transform
4. Repeat steps 1-3 until convergence
Algorithm Summary
• Least Squares Fit
• closed form solution
• robust to noise
• not robust to outliers
• Robust Least Squares
• improves robustness to noise
• requires iterative optimization
• Hough transform
• robust to noise and outliers
• can fit multiple models
• only works for a few parameters (1-4 typically)
• RANSAC
• robust to noise and outliers
• works with a moderate number of parameters (e.g, 1-8)
• Iterative Closest Point (ICP)
• For local alignment only: does not require initial correspondences
Alignment
• Alignment: find parameters of model that maps one
set of points to another

• Typically want to solve for a global transformation


that accounts for most true correspondences

• Difficulties
• Noise (typically 1-3 pixels)
• Outliers (often 30-50%)
• Many-to-one matches or multiple objects
Parametric (global) warping
T

p = (x,y) p’ = (x’,y’)
Transformation T is a coordinate-changing machine:
p’ = T(p)

What does it mean that T is global?


• Is the same for any point p
• can be described by just a few numbers (parameters)

For linear transformations, we can represent T as a matrix


p’ = Tp
 x'  x
 y '  T  y 
   
Common transformations

original

Transformed

translation rotation aspect

affine perspective Slide credit (next few slides):


A. Efros and/or S. Seitz
Scaling
• Scaling a coordinate means multiplying each of its components by a
scalar
• Uniform scaling means this scalar is the same for all components:

2
Scaling

• Non-uniform scaling: different scalars per component:

X  2,
Y  0.5
Scaling

• Scaling operation: x '  ax


y '  by
• Or, in matrix form:
 x '   a 0  x 
 y '   0 b   y 
    
scaling matrix S
2-D Rotation

(x’, y’)

(x, y)

x’ = x cos() - y sin()
 y’ = x sin() + y cos()
2-D Rotation

Polar coordinates…
x = r cos (f )
y = r sin (f)
x’ = r cos (f + )
(x’, y’) y’ = r sin (f + )

Trig Identity…
(x, y) x’ = r cos(f) cos() – r sin(f) sin()
y’ = r sin(f) cos() + r cos(f) sin()

Substitute…

 f
x’ = x cos() - y sin()
y’ = x sin() + y cos()
2-D Rotation
This is easy to capture in matrix form:

 x '  cos   sin     x 


 y '   sin   cos    y 
  
R

Even though sin(q) and cos(q) are nonlinear functions of q,


• x’ is a linear combination of x and y
• y’ is a linear combination of x and y

What is the inverse transformation?


• Rotation by –q R 1  R T
• For rotation matrices
Basic 2D transformations
 x'   s x 0  x  x'   1  x  x
 y '   0 s y   y   y '  
      y 1   y 
Scale Shear

 x
 x'  cos   sin   x   x  1 0 t x   
 y '   sin  cos    y   y   0 1 t   y 
        y
 1 
Rotate Translate

x
 x   a b c   Affine is any combination of
 y   d e 
f 
y
   translation, scale, rotation,
 1 
Affine shear
Affine Transformations
 x
Affine transformations are combinations of  x   a b c  
• Linear transformations, and  y   d e 
f 
y
  
• Translations  1 
or
Properties of affine transformations:
• Lines map to lines
 x'   a b c  x 
• Parallel lines remain parallel
 y '   d e f  y
• Ratios are preserved     
• Closed under composition  1   0 0 1   1 
Projective Transformations

Projective transformations are combos of  x'   a b c  x 


• Affine transformations, and  y '  d e f  y 
• Projective warps
 w'  g h i   w

Properties of projective transformations:


• Lines map to lines
• Parallel lines do not necessarily remain parallel
• Ratios are not preserved
• Closed under composition
• Models change of basis
• Projective matrix is defined up to a scale (8 DOF)
Projective Transformations (homography)
• The transformation between two views of a planar
surface

• The transformation between images from two


cameras that share the same center
Application: Panorama stitching

Source: Hartley & Zisserman


Application: document scanning
2D image transformations
(reference table)
Object Instance Recognition
A1 B3
1. Match keypoints to object A3
model A2

Matched B2
B1
keypoints
2. Solve for affine
transformation parameters
Affine
Parameters
3. Score by inliers and choose
solutions with score above This Class
# Inliers
threshold
Choose hypothesis with max
score above threshold
Overview of Keypoint Matching
1. Find a set of
distinctive key-
points
A1 B3
2. Define a region
around each
A2 A3 keypoint
B2
3. Extract and
B1
normalize the
region content
fA fB
4. Compute a local
N pixels

descriptor from the


e.g. color e.g. color
normalized region
N pixels d ( f A, fB )  T
5. Match local
descriptors
K. Grauman, B. Leibe
Finding the objects (overview)

Input
Image Stored
Image

1. Match interest points from input image to database image


2. Matched points vote for rough position/orientation/scale of
object
3. Find position/orientation/scales that have at least three votes
4. Compute affine registration and matches using iterative least
squares with outlier check
5. Report object if there are at least T matched points
Matching Keypoints
• Want to match keypoints between:
1. Query image
2. Stored image containing the object

• Given descriptor x0, find two nearest neighbors x1, x2


with distances d1, d2

• x1 matches x0 if d1/d2 < 0.8


• This gets rid of 90% false matches, 5% of true matches in
Lowe’s study
Affine Object Model
• Accounts for 3D rotation of a surface under
orthographic projection
Fitting an affine transformation
• Assume we know the correspondences, how do we
get the (transformation?
x,y )
i i
( xi, yi)

xi  Mx i  t
 xi   m1 m2   xi   t1 
 y   m   Want to find M, t to minimize
  
m4   yi  t 2 
 i  3 n

 || xi  Mxi  t ||
i 1
 2
Fitting an affine transformation
• Assume we know the correspondences, how do we
get the (transformation?
x,y )
i i
( xi, yi)

 m1 
 
   m2  
x 1 0  m3   xi 
 xi   m1 m2   xi   t1   i yi 0 0
 y   m       
 i  3 m4   yi  t 2  0 0 xi yi 0 1 m4   yi 
    
   t1 
 
 t 2 
Fitting an affine transformation
 m1 
 
   m2  
x yi 0 0 1 0  m3   xi 
 i  
0 0 xi yi 0 1 m4   yi 
    
   t1 
 
 t 2 
• Linear system with six unknowns
• Each match gives us two linearly independent
equations: need at least three to solve for the
transformation parameters
Finding the objects (in detail)
1. Match interest points from input image to database image
2. Get location/scale/orientation using Hough voting
• In training, each point has known position/scale/orientation
wrt whole object
• Matched points vote for the position, scale, and orientation
of the entire object
• Bins for x, y, scale, orientation
• Wide bins (0.25 object length in position, 2x scale, 30 degrees orientation)
• Vote for two closest bin centers in each direction (16 votes total)
3. Geometric verification
• For each bin with at least 3 keypoints
• Iterate between least squares fit and checking for inliers and
outliers
4. Report object if > T inliers (T is typically 3, can be computed to
match some probabilistic threshold)
Examples of recognized objects
View interpolation
• Training
– Given images of different
viewpoints
– Cluster similar viewpoints
using feature matches
– Link features in adjacent
views

• Recognition
– Feature matches may be
spread over several
training viewpoints
 Use the known links to
“transfer votes” to other [Lowe01]
viewpoints Slide credit: David Lowe
Applications
• Sony Aibo
(Evolution Robotics)

• SIFT usage
– Recognize
docking station
– Communicate
with visual cards

• Other uses
– Place recognition
– Loop closure in SLAM

K. Grauman, B. Leibe 53
Slide credit: David Lowe
Location Recognition

Training

[Lowe04]
Slide credit: David Lowe
Another application: category
recognition
• Goal: identify what type of object is in the image
• Approach: align to known objects and choose
category with best match

“Shape matching and object recognition using low distortion correspondence”, Berg et
al., CVPR 2005: http://www.cnbc.cmu.edu/cns/papers/berg-cvpr05.pdf
Examples of Matches
Examples of Matches
Other ideas worth being aware of
• Thin-plate splines: combines global affine warp with
smooth local deformation

• Robust non-rigid point matching: A


new point matching algorithm for non-rigid
registration, CVIU 2003 (includes code, demo,
paper)
HW 2: Feature Tracking
function featureTracking
% Main function for feature tracking
folder = '.\images';
im = readImages(folder, 0:50);

tau = 0.06; % Threshold for harris corner detection


[pt_x, pt_y] = getKeypoints(im{1}, tau); % Prob 1.1: keypoint detection

ws = 7; % Tracking ws x ws patches
[track_x, track_y] = ... % Prob 1.2 Keypoint tracking
trackPoints(pt_x, pt_y, im, ws);

% Visualizing the feature tracks on the first and the last frame
figure(2), imagesc(im{1}), hold off, axis image, colormap gray
hold on, plot(track_x', track_y', 'r');
HW 2 – Feature/Keypoint detection
• Compute second moment matrix
 I x2 ( D ) I x I y ( D )
 ( I ,  D )  g ( I )   
 I x I y ( D ) I y ( D ) 
2

• Harris corner criterion


har  det[ ( I , D)]   [trace( ( I , D)) 2 ] 
g ( I x2 ) g ( I y2 )  [ g ( I x I y )]2   [ g ( I x2 )  g ( I y2 )]2
  0.04
• Threshold
• Non-maximum suppression
function [keyXs, keyYs] = getKeypoints(im, tau)
% im: input image; tau: threshold
% keyXs, keyYs: detected keypoints, with dimension [N] x [1]

% 0. Smooth image (optional)

% 1. Compute image gradients. Hint: can use “gradient”

% 2. Compute Ix*Ix, Iy*Iy, Ix*Iy

% 3. Smooth Ix*Ix, Iy*Iy, Ix*Iy (locally weighted average)

% 4. Compute Harris corner score. Normalize the max to 1

% 5. Non-maximum suppression. Hint: use “ordfilt2”

% 6. Find positions whose values larger than tau. Hint: use


“find”
end
function [track_x, track_y] = trackPoints(pt_x, pt_y, im, ws)
% Tracking initial points (pt_x, pt_y) across the image sequence
% track_x: [Number of keypoints] x [nim]
% track_y: [Number of keypoints] x [nim]

% Initialization
N = numel(pt_x); % Number of keypoints
nim = numel(im); % Number of images
track_x = zeros(N, nim);
track_y = zeros(N, nim);
track_x(:, 1) = pt_x(:);
track_y(:, 1) = pt_y(:);

for t = 1:nim-1 % Tracking points from t to t+1


[track_x(:, t+1), track_y(:, t+1)] = ...
getNextPoints(track_x(:, t), track_y(:, t), im{t}, im{t+1}, ws);
end
Iterative L-K algorithm Original (x,y) position

1. Initialize (x’,y’) = (x,y) It = I(x’, y’, t+1) - I(x, y, t)


2. Compute (u,v) by

2nd moment matrix for feature


patch in first image displacement

3. Shift window by (u, v): x’=x’+u; y’=y’+v;


4. Recalculate It
5. Repeat steps 2-4 until small change
• Use interpolation for subpixel values
function [x2, y2] = getNextPoints(x, y, im1, im2, ws) % Iterative Lucas-Kanade feature tracking
% (x, y) : initialized keypoint position in im1; (x2, y2): tracked keypoint positions in im2
% ws: patch window size

% 1. Compute gradients from Im1 (get Ix and Iy)


% 2. Grab patches of Ix, Iy, and im1.
Hint 1: use “[X, Y] = meshgrid(-hw:hw,-hw:hw);” to get patch index, where hw = floor(ws/2);
Hint 2: use “interp2” to sample non-integer positions.

for iter = 1:numIter % 5 iterations should be sufficient


% Check if tracked patch are outside the image. Only track valid patches.
% For each keypoint
% - grab patch1 (centered at x1, y1), grab patch2 (centered at x2,y2)
% - compute It = patch2 – patch1
% - grab Ix, Iy (centered at x1, y1)
% - Set up matrix A and vector b
% - Solve linear system d = A\b.
% - x2(p)=x2(p)+d(1); y2(p)=y2(p)+d(1); -> Update the increment
end
HW 2 – Shape Alignment

• Global transformation (similarity, affine, perspective)


• Iterative closest point algorithm
• 
function Tfm = align_shape(im1, im2)
% im1: input edge image 1
% im2: input edge image 2
% Output: transformation Tfm [3] x [3]

% 1. Find edge points in im1 and im2. Hint: use “find”


% 2. Compute initial transformation (e.g., compute translation and scaling by
center of mass, variance within each image)

for i = 1: 50

% 3. Get nearest neighbors: for each point find corresponding

% 4. Compute transformation T based on matches

% 5. Warp points p according to T


HW 2 – Local Feature Matching
• Implement distance ratio test feature matching
algorithm
function featureMatching
im1 = imread('stop1.jpg');
im2 = imread('stop2.jpg');
% Load pre-computed SIFT features
load('SIFT_features.mat');
% Descriptor1, Descriptor2: SIFT features
% Frame1, Frame2: position, scale, rotation

% For every descriptor in im1, find the 1st nearest neighbor


and the 2nd nearest neighbor in im2.

% Compute distance ratio score.

% Threshold and get the matches: a 2 x N array of indices


that indicates which keypoints from image1 match which
points in image 2
figure(1), hold off, clf
plotmatches(im2double(im1),im2double(im2),Frame1,Frame2,mat
ches); % Display the matched keypoints
Things to remember
• Alignment
• Hough transform
• RANSAC
• ICP

• Object instance recognition


• Find keypoints, compute
descriptors
• Match descriptors
• Vote for / fit affine parameters
• Return object if # inliers > T
What have we learned?
• Interest points
• Find distinct and repeatable points in images
• Harris-> corners, DoG -> blobs
• SIFT -> feature descriptor
• Feature tracking and optical flow
• Find motion of a keypoint/pixel over time
• Lucas-Kanade:
• brightness consistency, small motion, spatial coherence
• Handle large motion:
• iterative update + pyramid search
• Fitting and alignment
• find the transformation parameters that
best align matched points
• Object instance recognition
• Keypoint-based object instance recognition and search
Next week –
Perspective and 3D Geometry

You might also like