57
Lab Course Kinect Programming for Computer Vision Transformations and Camera Calibration Loren Schwarz ([email protected]) May 25, 2011

kinect duas camaras

Embed Size (px)

Citation preview

Page 1: kinect duas camaras

Lab Course Kinect Programming for Computer Vision

Transformations and Camera Calibration!

Loren Schwarz ([email protected])!May 25, 2011!

Page 2: kinect duas camaras

Lecture Outline"

!!

Part I: Basics of Transformations!!

Part II: Basics of Cameras!

Kinect Lab Course - Transformations and Cameras! 2!

Page 3: kinect duas camaras

Outline Part I"

•  2D Transformations!

•  Homogeneous Coordinates in 2D!

•  Hierarchy of Transformations in 2D!

•  Hierarchy of Transformations in 3D!

Kinect Lab Course - Transformations and Cameras! 3!

Page 4: kinect duas camaras

2D Transformations"

The basic building blocks of transformations are:!

•  Scaling (isotropic, non-isotropic)!•  Rotation (around one rotation axis)!•  Translation (motion along the two axes)!

Transformations are defined by their action on point coordinates.!!Notation:!

4!Kinect Lab Course - Transformations and Cameras!

p = (x, y)� =�

xy

�p� = (x�, y�)� =

�x�

y�

original point! transformed point!

Page 5: kinect duas camaras

2D Transformations – Scaling"

5!Kinect Lab Course - Transformations and Cameras!

Isotropic scaling: same scaling factor in both spatial dimensions!

�x�

y�

�=

�s 00 s

� �xy

Page 6: kinect duas camaras

2D Transformations – Scaling"

6!Kinect Lab Course - Transformations and Cameras!

Non-isotropic scaling: different scaling factors in the two spatial dimensions!

�x�

y�

�=

�sx 00 sy

� �xy

Page 7: kinect duas camaras

2D Transformations – Rotation"

7!Kinect Lab Course - Transformations and Cameras!

�x�

y�

�=

�cos θ −sin θsin θ cos θ

� �xy

θ"

Page 8: kinect duas camaras

2D Transformations – Rotation"

Kinect Lab Course - Transformations and Cameras! 8!

p�

p

x!

y!

p�

p

r!

p =�

xy

�=

�r cos(α)r sin(α)

Page 9: kinect duas camaras

2D Transformations – Translation"

9!Kinect Lab Course - Transformations and Cameras!

�x�

y�

�=

�xy

�+

�txty

Page 10: kinect duas camaras

2D Transformations"

Have you noticed we have two types of representations for the basic transformations seen so far?!!Scaling, Rotation: !Matrix-vector product"!!!Translation: !Sum of vectors"""""

Kinect Lab Course - Transformations and Cameras! 10!

�x�

y�

�=

�xy

�+

�txty

�x�

y�

�=

�cos θ −sin θsin θ cos θ

� �xy

��x�

y�

�=

�sx 00 sy

� �xy

How to achieve a matrix-vector product form for all types of transformations?!

Page 11: kinect duas camaras

Outline Part I"

•  2D Transformations!

•  Homogeneous Coordinates in 2D!

•  Hierarchy of Transformations in 2D!

•  Hierarchy of Transformations in 3D!

Kinect Lab Course - Transformations and Cameras! 11!

Page 12: kinect duas camaras

Homogeneous Coordinates in 2D"

•  Write a 2D point in homogeneous coordinates:!

!

•  Then, translation can easily be expressed as a matrix-vector product:!

Kinect Lab Course - Transformations and Cameras! 12!

p =

xy1

∈ R3

We will see more properties of homogeneous representation later.!

x�

y�

1

=

1 0 tx0 1 ty0 0 1

xy1

=

x + txy + ty

1

How to achieve a matrix-vector product form for all types of transformations?!

Page 13: kinect duas camaras

Homogeneous Coordinates in 2D"

•  Rotation in homogeneous representation:!

!•  Combined transformations (here: rotation and translation):!

Kinect Lab Course - Transformations and Cameras! 13!

x�

y�

1

=

cos θ −sin θ 0sin θ cos θ 0

0 0 1

xy1

x�

y�

1

=

cos θ −sin θ txsin θ cos θ ty

0 0 1

xy1

=

xcos θ − ysin θ + txxsin θ + ycos θ + ty

1

Page 14: kinect duas camaras

2D Projective Space "

!

•  Points in 2D Euclidean space are represented by Cartesian coordinates!

•  Points in 2D Projective space are represented by Homogeneous coordinates:!

Kinect Lab Course - Transformations and Cameras! 14!

p =�

xy

�∈ R2

p =

xy1

∈ P2

When dealing with points represented in homogeneous coordinates, we are working in a Projective space.!

Page 15: kinect duas camaras

2D Projective Space "

Kinect Lab Course - Transformations and Cameras! 15!

Point in Euclidean 2D space! Point in Projective 2D space.All points on the orange line represent the same point. A homogeneous vector in 2D projective space represents an equivalence class of points.!

z!

y!

p = (x,y,1)!x!

k(x,y,1)!

x!

y!

p = (x,y)!

z = 1!

Projective Plane!

Page 16: kinect duas camaras

Points and Lines in 2D Projective Space"

Properties of points"

•  A 2D point has the homogeneous representation . !

•  The homogeneous vector with w≠0 represents the point .!

•  For any non-zero k, and represent the same point.!

Kinect Lab Course - Transformations and Cameras! 16!

(x, y)� (x, y, 1)�

(x, y, w)� (x

w,

y

w, 1)�

k(x, y, w)�(x, y, w)�

Page 17: kinect duas camaras

Points and Lines in 2D Projective Space"

Properties of lines"

•  A line has the homogeneous representation .!

•  A point lies on a line if and only if .!

•  Two lines and intersect in the point ! .!

•  The line joining the two points and is .!

Kinect Lab Course - Transformations and Cameras! 17!

ax + by + c = 0 l = (a, b, c)�

x l x�l = 0

l l� x = l× l�

l = x× x�x x�

Page 18: kinect duas camaras

Points and Lines in 2D Projective Space"

Special points and lines"

•  Points with 3rd coordinate zero are called points at infinity.!

•  All points at infinity lie on the line at infinity .!

•  Parallel lines intersect at infinity.!

Kinect Lab Course - Transformations and Cameras! 18!

(x, y, 0)�

l∞ = (0, 0, 1)�

In projective space, all lines intersect in a point, even parallel lines.!

l�∞(x, y, 0) = (0, 0, 1)�(x, y, 0) = 0

Page 19: kinect duas camaras

Outline Part I"

•  2D Transformations!

•  Homogeneous Coordinates in 2D!

•  Hierarchy of Transformations in 2D!

•  Hierarchy of Transformations in 3D!

Kinect Lab Course - Transformations and Cameras! 19!

Page 20: kinect duas camaras

Hierarchy of Transformations in 2D"

•  Classification of transformations in terms of quantities or properties that are invariant (left unchanged) by the transformation!

•  Hierarchy: Starting from most specialized type of transformation, successively remove invariants to get more general types of transformations !!

!!

•  Containment relation from general to specialized, e.g.: affine transformations have all properties of projective transformations, plus additional ones.!

Kinect Lab Course - Transformations and Cameras! 20!

Most general:!! !! !

Most specialized:!

Projective transformations!!Affine transformations!!Similarity transformations!!Euclidean transformations!

Page 21: kinect duas camaras

Hierarchy of Transformations in 2D: Euclidean Transformations"

•  Transformation:!

•  Concise notation for transformation matrix:!

•  Degrees of freedom: !–  3 (1 rotation, 2 translation)

!•  Invariants: length, area, angles!

Kinect Lab Course - Transformations and Cameras! 21!

Teuclidean =�

R t0� 1

x�

y�

1

=

cos θ −sin θ txsin θ cos θ ty

0 0 1

xy1

R�R = Idet(R) = 1

Page 22: kinect duas camaras

Hierarchy of Transformations in 2D: Similarity Transformations"

•  Transformation:!

•  Concise notation for transformation matrix:!

•  Degrees of freedom: !–  4 (1 rotation, 2 translation, 1 isotropic scaling)

!•  Invariants: ratio of lengths, angles, parallel lines!

Kinect Lab Course - Transformations and Cameras! 22!

x�

y�

1

=

scos θ −ssin θ txssin θ scos θ ty

0 0 1

xy1

Tsimilarity =�

sR t0� 1

�R�R = I

det(R) = 1

Page 23: kinect duas camaras

Hierarchy of Transformations in 2D: Affine Transformations"

•  Transformation:!

•  Concise notation for transformation matrix:!

•  Degrees of freedom: !–  6 (2 translation, 4 entries of A)

!•  Invariants: parallel lines, ratios of areas, line at infinity !

Kinect Lab Course - Transformations and Cameras! 23!

x�

y�

1

=

a11 a12 txa21 a22 ty0 0 1

xy1

Taffinity =�

A t0� 1

�A non-singular!

Page 24: kinect duas camaras

Hierarchy of Transformations in 2D: Affine Transformations"

Affine transformations geometrically consist of two components:!

Kinect Lab Course - Transformations and Cameras! 24!

A = R(θ)R(−φ)�

λ1 00 λ2

�R(φ)

Rotation! Non-isotropic Scaling!

Page 25: kinect duas camaras

Hierarchy of Transformations in 2D:Projective Transformations"

•  Transformation:!

•  Concise notation for transformation matrix:!

•  Degrees of freedom: !–  8 (9 arbitrary matrix entries, up to scale)

!•  Invariants: Intersection points, tangency, cross-ratios!

Kinect Lab Course - Transformations and Cameras! 25!

x�

y�

1

=

a11 a12 txa21 a22 tyv1 v2 v

xy1

Tprojectivity =�

A tv� v

�A non-singular!

Page 26: kinect duas camaras

Hierarchy of Transformations in 2D – Summary"

Kinect Lab Course - Transformations and Cameras! 26!

Projective "8 DOF!!!!Affine " "6 DOF!!!!Similarity "4 DOF!!!!Euclidean "3 DOF!

Tprojectivity =�

A tv� v

Taffinity =�

A t0� 1

Tsimilarity =�

sR t0� 1

Teuclidean =�

R t0� 1

Page 27: kinect duas camaras

•  Affine Transformations:!

Action of Transformations in 2D on Points at Infinity"

Kinect Lab Course - Transformations and Cameras! 27!

a11 a12 txa21 a22 ty0 0 1

xy0

=

a11x + a12ya21x + a22y

0

Point at infinity,e.g. intersection !

of blue lines!

Mapped by affine transformation to another

point at infinity!

Page 28: kinect duas camaras

•  Projective Transformations:!

Mapped by projective transformation to a finite point (vanishing point)!

Action of Transformations in 2D on Points at Infinity"

Kinect Lab Course - Transformations and Cameras! 28!

a11 a12 txa21 a22 tyv1 v2 v

xy0

=

a11x + a12ya21x + a22yv1x + v2y

Point at infinity,e.g. intersection !

of blue lines!

Only projective transformations can make points at infinity finite points.!

Page 29: kinect duas camaras

Outline Part I"

•  2D Transformations!

•  Homogeneous Coordinates in 2D!

•  Hierarchy of Transformations in 2D!

•  Hierarchy of Transformations in 3D!

Kinect Lab Course - Transformations and Cameras! 29!

Page 30: kinect duas camaras

Homogeneous Coordinates in 3D"

Kinect Lab Course - Transformations and Cameras! 30!

•  Write a 3D point in homogeneous coordinates:!

!

•  3D projective space: properties analogous to 2D homogeneous representation!•  All types of transformations (scaling, translation, rotation, etc.) can be written

as a matrix-vector product!•  Several transformations can be composed into one transformation matrix!

How to achieve a matrix-vector product form for all types of transformations?!

p =

xyz1

∈ R4

Page 31: kinect duas camaras

Hierarchy of Transformations in 3D (1): Euclidean Transformations"

•  Transformation:!

•  Concise notation for transformation matrix:!

•  Degrees of freedom: !–  6 (3 rotation, 3 translation)

!•  Invariants: volume, angles!

Kinect Lab Course - Transformations and Cameras! 31!

Teuclidean =�

R t0� 1

�R�R = I

det(R) = 1

x�

y�

z�

1

=

r11 r12 r13 txr21 r22 r23 tyr31 r32 r33 tz0 0 0 1

xyz1

Page 32: kinect duas camaras

Hierarchy of Transformations in 3D (2): Similarity Transformations"

•  Transformation:!

•  Concise notation for transformation matrix:!

•  Degrees of freedom: !–  7 (3 rotation, 3 translation, 1 scaling)

!•  Invariants: angles!

Kinect Lab Course - Transformations and Cameras! 32!

Tsimilarity =�

sR t0� 1

�R�R = I

det(R) = 1

x�

y�

z�

1

=

sr11 sr12 sr13 txsr21 sr22 sr23 tysr31 sr32 sr33 tz0 0 0 1

xyz1

Page 33: kinect duas camaras

Hierarchy of Transformations in 3D (3): Affine Transformations"

•  Transformation:!

•  Concise notation for transformation matrix:!

•  Degrees of freedom: !–  12 (3 translation, 9 entries of A)

!•  Invariants: parallel planes, ratios of volumes, plane at infinity !

Kinect Lab Course - Transformations and Cameras! 33!

Taffinity =�

A t0� 1

�A non-singular!

x�

y�

z�

1

=

a11 a12 a13 txa21 a22 a23 tya31 a32 a33 tz0 0 0 1

xyz1

Page 34: kinect duas camaras

Hierarchy of Transformations in 3D (4)Projective Transformations"

•  Transformation:!

•  Concise notation for transformation matrix:!

•  Degrees of freedom: !–  15 (16 arbitrary matrix entries, up to scale)

!•  Invariants: Intersection points, tangency!

Kinect Lab Course - Transformations and Cameras! 34!

Tprojectivity =�

A tv� v

�A non-singular!

x�

y�

z�

1

=

a11 a12 a13 txa21 a22 a23 tya31 a32 a33 tzv1 v2 v3 v

xyz1

Page 35: kinect duas camaras

Hierarchy of Transformations in 3D – Summary"

Kinect Lab Course - Transformations and Cameras! 35!

Projective "15 DOF!!!!Affine " "12 DOF!!!!Similarity "7 DOF!!!!Euclidean "6 DOF!

Tprojectivity =�

A tv� v

Taffinity =�

A t0� 1

Tsimilarity =�

sR t0� 1

Teuclidean =�

R t0� 1

Page 36: kinect duas camaras

Lecture Outline"

!!

Part I: Basics of Transformations!!

Part II: Basics of Cameras!

Kinect Lab Course - Transformations and Cameras! 36!

Page 37: kinect duas camaras

Pinhole Camera Model"Mapping between 3D world and 2D image by central projection!

37!Kinect Lab Course - Transformations and Cameras!

Page 38: kinect duas camaras

Pinhole Camera Model"In homogeneous coordinates…!

38!Kinect Lab Course - Transformations and Cameras!

fXfYZ

!

"

####

$

%

&&&&=

f 0f 01 0

'

(

)))

*

+

,,,

XYZ1

!

"

####

$

%

&&&&

Page 39: kinect duas camaras

Pinhole Camera Model"Mapping between 3D world and 2D image by central projection!

39!Kinect Lab Course - Transformations and Cameras!

fXfYZ

!

"

####

$

%

&&&&=

ff1

'

(

)))

*

+

,,,

1 01 01 0

'

(

)))

*

+

,,,

XYZ1

!

"

####

$

%

&&&&

Page 40: kinect duas camaras

Pinhole Camera Model"Origin of image plane is not necessarily at the camera center!

40!Kinect Lab Course - Transformations and Cameras!

principal point (perpendicular intersection point of

principal axis and image plane)

Page 41: kinect duas camaras

Pinhole Camera Model"Origin of image plane is not necessarily at the camera center!

41!Kinect Lab Course - Transformations and Cameras!

fX + ZpxfY + Zpx

Z

!

"

####

$

%

&&&&=

f px 0f py 0

1 0

'

(

))))

*

+

,,,,

XYZ1

!

"

####

$

%

&&&&

K =

f pxf py

1

!

"

####

$

%

&&&&

intrinsic camera parameters matrix

Page 42: kinect duas camaras

Pinhole Camera Model"Camera center is not necessarily at the world coordinate system origin!

Xcam =R !R !C0 1

"

#$$

%

&''X

3D point in camera coordinates

x =K I | 0[ ]Xcam

x =KR I |! !C"# $%X

camera center in world coordinates

projection to image plane from camera coordinates

projection to image plane from world coordinates

!Xcam = R !X- !C( )Points with a tilde are in non-homogeneous notation!

Page 43: kinect duas camaras

Pinhole Camera Model"Camera center is not necessarily at the world coordinate system origin!

x =KR I |! !C"# $%X

K =

f pxf py

1

!

"

####

$

%

&&&&

x = PX

P =KR I |! !C"# $%

projection matrix, 9 DOF

intrinsic parameters matrix, 3 DOF

R, !C

extrinsic parameters, each 3 DOF

Page 44: kinect duas camaras

K =

mx

my

1

!

"

####

$

%

&&&&

f pxf py

1

!

"

####

$

%

&&&&

K =

!x x0!y y0

1

!

"

####

$

%

&&&&

CCD Camera Model"

mx,my number of pixels per unit distance in camera coordinates

4 DOF

P =KR I |! !C"# $% 10 DOF

44!Kinect Lab Course - Transformations and Cameras!

Camera coordinates are mapped to pixels of the CCD chip!

Page 45: kinect duas camaras

K =

!x s x0!y y0

1

!

"

####

$

%

&&&&

P =KR I |! !C"# $%

Finite Projective Camera Model"

s skew parameter

5 DOF

11 DOF

45!Kinect Lab Course - Transformations and Cameras!

Both axes in camera coordinates need not have identical scaling!

Page 46: kinect duas camaras

K =

!x s x0!y y0

1

!

"

####

$

%

&&&&

P =KR I |! !C"# $%

Camera Calibration"

46!Kinect Lab Course - Transformations and Cameras!

•  Relating the ideal camera model to the properties of an actual physical device and determining the position and orientation of the camera w.r.t. the world coordinate frame1.!

•  Estimating the entries of!

•  Camera models with increasing generality:!–  pinhole camera ! ! 9 DOF!–  CCD camera ! !10 DOF!–  finite projective camera !11 DOF!

•  In addition: lens distortion parameters!

1 http://www.dis.uniroma1.it/~iocchi/stereo/calib.html!

K,R, !C

Page 47: kinect duas camaras

Camera Calibration"

Kinect Lab Course - Transformations and Cameras! 47!

Checkerboard pattern captured from multiple views (the checker sizes

in the real world are known)

Semi-automatic measurements to find sizes in each of the images

•  Linear and non-linear optimization for estimating camera parameters •  MATLAB calibration toolbox: http://www.vision.caltech.edu/bouguetj/calib_doc!

Images from http://www.vision.caltech.edu/bouguetj/calib_doc!

Page 48: kinect duas camaras

Camera Calibration"

•  sdfsdf!

Kinect Lab Course - Transformations and Cameras! 48!Images from http://www.vision.caltech.edu/bouguetj/calib_doc!

Page 49: kinect duas camaras

Stereo Calibration"

•  Find the spatial relation between two cameras observing the same scene (stereo setup)!

•  For each orientation of the calibration pattern, capture corresponding left and right camera images!

•  Calibrate each camera separately, as before!

•  Perform stereo calibration using the result of separate calibration as an input!

Kinect Lab Course - Transformations and Cameras! 49!

Page 50: kinect duas camaras

Calibrating the Kinect"

•  Depth and RGB images do not match exactly, there is an offset!•  Stereo calibration for getting both images from the same camera perspective!

Kinect Lab Course - Transformations and Cameras! 50!

RGB Image! Depth Image!

Page 51: kinect duas camaras

Calibrating the Kinect"

•  Kinect has two cameras with standard optics!–  RGB camera!–  Infrared (IR) camera!

•  Infrared image is used internally for computing depth image, but is accessible through the API!

•  Checkerboard pattern is not visible in depth images, but visible in IR images!

Kinect Lab Course - Transformations and Cameras! 51!

Infrared!Projector!

RGB!Camera!

Infrared!Camera!

RGB Image!

Infrared Image!

Page 52: kinect duas camaras

Relating RGB and Depth Images"

Stereo calibration gives and .!

Step 1: Back-project every 2D point from depth image into 3D space!Step 2: Apply rigid transformation between two cameras to 3D points!Step 3: Project every transformed 3D point into the RGB image!

Kinect Lab Course - Transformations and Cameras! 52!

RGB"

IR"

R, t 3D Points!

Back-projection using!

Projection using!

R, t

Relative transformation!between rigidly

connected cameras!

Page 53: kinect duas camaras

Relating RGB and Depth Images"

Step 1: Back-project every 2D point from depth image into 3D space!

•  ToF data: depth d in meters for every pixel location !•  Desired data: 3D coordinates for every pixel !!

Kinect Lab Course - Transformations and Cameras! 53!

Depth Image! 3D Point Cloud!

X = (X,Y, Z)�x = (x, y)�

Page 54: kinect duas camaras

Relating RGB and Depth Images"

Step 1: Back-project every 2D point from depth image into 3D space!!Apply inverse of IR camera projection to obtain 3D points:!

Kinect Lab Course - Transformations and Cameras! 54!

xyw

=

fx 0 cx 00 fy cy 00 0 1 0

XYZW

=

fxX + cxZfyY + cyZ

Z

X =(x− cx)Z

fxY =

(y − cy)Zfy

Inverse relation for X and Y!

Page 55: kinect duas camaras

Relating RGB and Depth Images"

Kinect Lab Course - Transformations and Cameras! 55!

!For every depth pixel at location with depth d, !compute a 3D point with coordinates: !

x = (x, y)�

X = (X,Y, Z)�

Intrinsics of the IR (depth) camera!

Step 1: Back-project every 2D point from depth image into 3D space!

Page 56: kinect duas camaras

Relating RGB and Depth Images"

Step 2: Apply rigid transformation between two cameras to 3D points!Step 3: Project every transformed 3D point into the RGB image!!!!•  are the coordinates of the

projection of X into the RGB image!•  look up color corrsponding to

a given 3D point!

Kinect Lab Course - Transformations and Cameras! 56!

Page 57: kinect duas camaras

Assignment 3"

•  Extend your application such that it can read out Kinect IR images!•  Extend your application for saving IR and RGB images to bitmap files!•  Familiarize yourself with the Matlab Camera Calibration Toolbox

http://www.vision.caltech.edu/bouguetj/calib_doc/!–  Read the examples (especially examples 1 and 5)!–  Download the toolbox!–  Try out the examples!

•  Perform stereo-calibration for your Kinect device!–  Note that you cannot extract IR and RGB images simultaneously…!

"Outlook Assignment 4:"•  Implement a 3D point cloud visualization for kinect depth data!•  Add a feature to color the 3D points with the colors from the RGB camera!

Kinect Lab Course - Transformations and Cameras! 57!