You are on page 1of 3

12/18/13

Find Image Rotation and Scale - MATLAB & Simulink Example

Find Image Rotation and Scale


This example show s how to align or register tw o images that differ by a rotation and a scale change. You can use f i t g e o t r a n s to find the rotation angle and scale factor after manually picking corresponding points. You can then transform the distorted image to recover the original image. Step 1: Read Im age Read an image into the w orkspace. o r i g i n a l=i m r e a d ( ' c a m e r a m a n . t i f ' ) ; i m s h o w ( o r i g i n a l ) ; t e x t ( s i z e ( o r i g i n a l , 2 ) , s i z e ( o r i g i n a l , 1 ) + 1 5 ,. . . ' I m a g ec o u r t e s yo fM a s s a c h u s e t t sI n s t i t u t eo fT e c h n o l o g y ' ,. . . ' F o n t S i z e ' , 7 , ' H o r i z o n t a l A l i g n m e n t ' , ' r i g h t ' ) ;

Step 2: Resize and Rotate the Im age s c a l e=0 . 7 ; d i s t o r t e d=i m r e s i z e ( o r i g i n a l , s c a l e ) ;%T r yv a r y i n gt h es c a l ef a c t o r . t h e t a=3 0 ; d i s t o r t e d=i m r o t a t e ( d i s t o r t e d , t h e t a ) ;%T r yv a r y i n gt h ea n g l e ,t h e t a . f i g u r e ,i m s h o w ( d i s t o r t e d )

www.mathworks.com/help/images/examples/find-image-rotation-and-scale.html

1/3

12/18/13

Find Image Rotation and Scale - MATLAB & Simulink Example

Step 3: Select Control Points Use the Control Point Selection Tool to pick at least tw o pairs of control points. m o v i n g P o i n t s=[ 1 5 1 . 5 2 1 6 4 . 7 9 ;1 3 1 . 4 07 9 . 0 4 ] ; f i x e d P o i n t s=[ 1 3 5 . 2 6 2 0 0 . 1 5 ;1 7 0 . 3 07 9 . 3 0 ] ; You can run the rest of the example w ith these pre-picked points, but try picking your ow n points to see how the results vary. c p s e l e c t ( d i s t o r t e d , o r i g i n a l , m o v i n g P o i n t s , f i x e d P o i n t s ) ; Save control points by choosing the File menu, then the Save Points to Workspace option. Save the points, overw riting variables m o v i n g P o i n t sand f i x e d P o i n t s . Step 4: Estim ate Transform ation Fit a nonreflective similarity transformation to your control points. t f o r m=f i t g e o t r a n s ( m o v i n g P o i n t s , f i x e d P o i n t s , ' n o n r e f l e c t i v e s i m i l a r i t y ' ) ; After you have done Steps 5 and 6, repeat Steps 4 through 6 but try using 'affine' instead of 'NonreflectiveSimilarity'. What happens? Are the results as good as they w ere w ith 'NonreflectiveSimilarity'? Step 5: Solve for Scale and Angle The geometric transformation, t f o r m , contains a transformation matrix in t f o r m . T . Since you know that the transformation includes only rotation and scaling, the math is relatively simple to recover the scale and angle. L e ts c=s * c o s ( t h e t a ) L e ts s=s * s i n ( t h e t a ) T h e n ,T i n v=i n v e r t ( t f o r m ) ,a n dT i n v . T=[ s cs s 0 ; s s s c 0 ; t x t y 1 ] w h e r et xa n dt ya r exa n dyt r a n s l a t i o n s ,r e s p e c t i v e l y . t f o r m I n v=i n v e r t ( t f o r m ) ; T i n v=t f o r m I n v . T ; s s=T i n v ( 2 , 1 ) ;
www.mathworks.com/help/images/examples/find-image-rotation-and-scale.html 2/3

12/18/13

Find Image Rotation and Scale - MATLAB & Simulink Example

s c=T i n v ( 1 , 1 ) ; s c a l e _ r e c o v e r e d=s q r t ( s s * s s+s c * s c ) t h e t a _ r e c o v e r e d=a t a n 2 ( s s , s c ) * 1 8 0 / p i s c a l e _ r e c o v e r e d= 0 . 7 0 0 0

t h e t a _ r e c o v e r e d= 2 9 . 3 7 4 1

The recovered values of s c a l e _ r e c o v e r e dand t h e t a _ r e c o v e r e dshould match the values you set in Step 2: Resize and Rotate the Im age . Step 6: Recover Original Im age Recover the original image by transforming d i s t o r t e d , the rotated-and-scaled image, using the geometric transformation t f o r m and w hat you know about the spatial referencing of o r i g i n a l . The 'OutputView ' Name/Value pair is used to specify the resolution and grid size of the resampled output image. R o r i g i n a l=i m r e f 2 d ( s i z e ( o r i g i n a l ) ) ; r e c o v e r e d=i m w a r p ( d i s t o r t e d , t f o r m , ' O u t p u t V i e w ' , R o r i g i n a l ) ; Compare r e c o v e r e dto o r i g i n a lby looking at them side-by-side in a montage. f i g u r e ,i m s h o w p a i r ( o r i g i n a l , r e c o v e r e d , ' m o n t a g e ' )

The r e c o v e r e d(right) image quality does not match the o r i g i n a l(left) image because of the distortion and recovery process. In particular, the image shrinking causes loss of information. The artifacts around the edges are due to the limited accuracy of the transformation. If you w ere to pick more points in Step 3: Select Control Points , the transformation w ould be more accurate.

www.mathworks.com/help/images/examples/find-image-rotation-and-scale.html

3/3

You might also like