You are on page 1of 5

Numerical Examples: Module 8

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

given the original image $I$ is expressed by

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

The original image looks like

whereas the transformed image looks like

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 .

This is thus a simple linear filter applied on the image.

Let us look at the effect of this filter on the following image

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.

I=d o u b l e ( i m r e a d ( ' c a m e r a m a n . j p g ' ) ) ;


Then, we define the impulse response of the Gaussian filter, parametrized by N and
,

S I G M A=1 2 ;

N=1 0 ; h=z e r o s ( 2 * N + 1 ) ; f o rn 1=N : N f o rn 2=N : N h ( ( n 1+N+1 ) ,( n 2+N+1 ) )=1 / ( 2 * p i * S I G M A ^ 2 ) * e x p ( ( n 1 ^ 2+n 2 ^ 2 ) / ( 2 * S I G M A ^ 2 ) ) ; e n d e n d


The image is then filtered and the result subtracted to the original image

I _ b l u r r e d=c o n v 2 ( I ,h ) ; I _ b l u r r e d=I _ b l u r r e d ( ( N + 1 ) : ( s i z e ( I ,1 )+N ) ,( N+1 ) : ( s i z e ( I ,2 )+N ) ) ; I _ s h a r p h e n e d=I-I _ b l u r r e d ;


Finally, to display the result

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.

DCT transform and JPEG compression


In the JPEG compression standard, the image is projected onto another space using the discrete cosine transform (DCT). In this space, the information in the image is concentrated on few coefficients. Most of the transform coefficients can then be discarded, as they convey little additional information. There are of course other technical aspects of great practical interest, like smart quantization transform coefficients coding, but this simple idea is at the heart of the JPEG compression standard. Let us illustrate it through a simple example. To run this example, we provide you a Matlab function that computes the orthonormal basis vectors corresponding to the DCT. This file can be downloaded here.

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.

I=d o u b l e ( i m r e a d ( ' c a m e r a . j p g ' ) ) ; n b _ r o w=s i z e ( I ,1 ) ; n b _ c o l=s i z e ( I ,2 ) ; I=I ( : ) ;


We then compute the vectors forming an orthonormal basis corresponding to the 2D DCT. These vectors arranged as column vectors form the columns of a basis matrix.

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

You might also like