Professional Documents
Culture Documents
Help
Image Processing
Affine transforms
We have seen in the lecture that an affine transform simply defines a change of coordinate system. In the most general terms, given a 2 2 matrix A and a 2 1 vector d, it is expressed by
y1 y
2
) = A(
x1 x2
) d.
The difficulty comes from the fact that we work in a discrete space. Indeed, a pixel of an image is indexed by two integers corresponding to index of the row and the column where this pixel is located. Then, in the formula above, x1 and x2 are integers. In general, since we work with a general matrix A and vector d, this is not the case for y 1 and y 2 . In this example, we look at a possible implementation of the affine transform to see how this difficulty is circumvented. The solution is already outlined in the lecture slides. For each pixel of the new image, indexed by y 1 and transform
x1 x2 x1 x2 y2 ,
we inverse the
) = A
((
) + d).
Then, we find the nearest integers strictly smaller than x1 and x2 , referred to as 1 and 2 respectively and we denote by i the difference between xi and i , i = 1, 2. The intensity of the pixel is then obtained using an interpolation formula. For example, with a bilinear interpolation, the intensity of the pixel of the new image
I ( y 1 , y 2 ) = (1 1 )(1 2 )I ( 1 , 2 ) + 1 (1 2 )I ( 1 + 1, 2 ) + 2 (1 1 )I ( 1 , 2 + 1) + 1 2 I ( 1 + 1, 2 + 1)
Finally, it may happen that i or i + 1 fall outside the range of the original image. We then use zero-padding, i.e., that the intensity of the pixel outside the original image is set to 0. The following code implements a particular affine transform called shearing.
c l e a ra l l c l o s ea l l c l c %P a r a m e t e r s S=0 . 3 ; %C o n s t r u c ta n dd i s p l a yt h eo r i g i n a li m a g e I _ o r i g=z e r o s ( 6 4 ,6 4 ) ; I _ o r i g ( 2 5 ,1 7 : 4 8 )=1 ; I _ o r i g ( 4 0 ,1 7 : 4 8 )=1 ; I _ o r i g ( 2 5 : 5 6 ,1 7 )=1 ; I _ o r i g ( 9 : 4 0 ,4 8 )=1 ; f i g u r e i m a g e ( I _ o r i g ) ; c o l o r m a p ( g r a y ) a x i so f f a x i si m a g e %C o m p u t em a t r i xAa n dd A=[ 1S ;01 ] ; d=[ 0 ;0 ] ; %I n i t i a l i z a t i o n
n b _ r o w=s i z e ( I _ o r i g ,1 ) ; n b _ c o l=s i z e ( I _ o r i g ,2 ) ; I=z e r o s ( n b _ r o w ,n b _ c o l ) ; f o ry 1=1 : n b _ r o w f o ry 2=1 : n b _ c o l x=i n v ( A ) * ( [ y 1 ;y 2 ]+d ) ; e t a=f l o o r ( x ) ; t h e t a=x-e t a ; i fe t a ( 1 )> =1& &e t a ( 1 )< =n b _ r o w i fe t a ( 2 )> =1& &e t a ( 2 )< =n b _ c o l I ( y 1 ,y 2 )=I ( y 1 ,y 2 )+( 1-t h e t a ( 1 ) ) * ( 1-t h e t a ( 2 ) ) * I _ o r i g ( e t a ( 1 ) ,e t a ( 2 ) ) ; e n d i fe t a ( 2 )> =0& &e t a ( 2 )< =( n b _ c o l-1 ) I ( y 1 ,y 2 )=I ( y 1 ,y 2 )+( 1-t h e t a ( 1 ) ) * t h e t a ( 2 ) * I _ o r i g ( e t a ( 1 ) ,e t a ( 2 )+1 ) ; e n d e n d i fe t a ( 1 )> =0& &e t a ( 1 )< =( n b _ r o w-1 ) i fe t a ( 2 )> =1& &e t a ( 2 )< =n b _ c o l I ( y 1 ,y 2 )=I ( y 1 ,y 2 )+t h e t a ( 1 ) * ( 1-t h e t a ( 2 ) ) * I _ o r i g ( e t a ( 1 )+1 ,e t a ( 2 ) ) ; e n d i fe t a ( 2 )> =0& &e t a ( 2 )< =( n b _ c o l-1 ) I ( y 1 ,y 2 )=I ( y 1 ,y 2 )+t h e t a ( 1 ) * t h e t a ( 2 ) * I _ o r i g ( e t a ( 1 )+1 ,e t a ( 2 )+1 ) ; e n d e n d e n d e n d %D i s p l a yt h er e s u l t f i g u r e i m a g e ( I ) ; c o l o r m a p ( g r a y ) a x i so f f a x i si m a g e
Unsharp masking
Unsharp masking is an edge enhancement technique commonly available on commercial image manipulation softwares like Photoshop. The principle underlying it is quite simple. In its most basic form, it consists in subtracting from the original image a blurred version of the image. The latter is obtained by applying a 2D Gaussian blur filter, whose impulse response is given by
n +n
2 1 2 2
h(n1 , n2 ) =
1 2 2
exp(
2 2
for
|n1 , n2 | N .
Download the image from here and save it in your Matlab workspace directory. Then load the image in the workspace using the
i m r e a d ( . . . ) function.
S I G M A=1 2 ;
f i g u r e i m a g e ( I _ s h a r p h e n e d ) ; c o l o r m a p ( g r a y ) ; a x i so f f a x i ss q u a r e
The resulting image looks like
By subtracting a blurred, i.e., low pass version of the image, this procedure boosts the high frequency component of the image. The difference is particularly important in the texture of the grass or the edge of the tower in the background. This procedure also creates artifacts, for example there is a sort of halo that surrounds the cameraman, particularly behind his coat. As this procedure is uniformly applied on all parts of the image, we create the impression of an edge enhancement, but the information content in the image is not improved.
First, download a downsampled image of the cameraman from here and save it in your Matlab workspace directory. Then load it in the workspace and rearrange it as a column vector.
b a s i s _ m a t r i x=g e t _ d c t 2 _ v e c t o r s ( n b _ r o w ,n b _ c o l ) ;
We project the original image onto this new basis and discard half of the coefficients.
I _ p r o j e c t e d=d o u b l e ( b a s i s _ m a t r i x ) * I ; I _ p r o j e c t e d ( 2 0 4 9 : e n d )=0 ;
We invert the transform and display the resulting image.
I _ a p p r o x=i n v ( b a s i s _ m a t r i x ) * I _ p r o j e c t e d ; I _ a p p r o x=r e s h a p e ( I _ a p p r o x ,n b _ r o w ,n b _ c o l ) ; f i g u r e i m a g e s c ( I _ a p p r o x ) c o l o r m a p ( g r a y ) a x i so f f a x i ss q u a r e
This image looks like
We notice that we still recover most of the image, despite the very coarse approximation.
Created Mon 8 Apr 2013 1:45 AM PET Last Modified Thu 21 Nov 2013 9:57 AM PET