Find Image Rotation and Scale - MATLAB & Simulink Example
Find Image Rotation and Scale - MATLAB & Simulink Example
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
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
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