Você está na página 1de 12

LinkYourCourseworktoYourIdentity

GetStarted(https://www.coursera.org/signature/course/dsp/974034?

utm_source=spark&utm_medium=banner)
(https://courserahelp.zendesk.com/hc/requests/new)

NumericalExamples:BasisforGrayscaleImages(Python)
HelpCenter(https://accounts.coursera.org/i/zendesk/courserahelp?
return_to=https://courserahelp.zendesk.com/hc/)
Thegoalofthisexampleistoillustratethetheoreticalconceptsstudiedinclass(vectorspace,scalarproduct,basis
and approximation) with a specific class of signals, grayscale images. Consider the following grayscale image of
size64by64pixels.
In[1]: fromIPython.displayimportImage
Image(filename='Num_Ex_3/camera_blurred_big.jpg')
Out[1]:

It is represented by a square 64 64 matrix I , where each element corresponds to the intensity of a pixel. The
matrixIisdeterminedfromtheimageusingtheimreadfunction.
In[2]: %pylabinline
importmatplotlib.pylabasplt
I=np.array(plt.imread('Num_Ex_3/camera_blurred.jpg'),dtype=float64)
Populatingtheinteractivenamespacefromnumpyandmatplotlib

Forexample,thefirstcolumnofthisimageisa 64 1 vector
In[3]: I[:,0]
Out[3]: array([156.,157.,157.,152.,154.,155.,151.,157.,152.,
155.,158.,159.,159.,160.,160.,161.,155.,160.,
161.,161.,164.,162.,160.,162.,158.,160.,158.,
157.,160.,160.,159.,158.,163.,162.,162.,157.,

160.,114.,114.,103.,88.,62.,109.,82.,108.,
128.,138.,140.,136.,128.,122.,137.,147.,114.,
114.,144.,112.,115.,117.,131.,112.,141.,99.,97.])
Conversely,onecandisplaythearrayasanimagebyusingthepylab.imshowfunctionfrommatplotlib
In[4]: importmatplotlib.pylabasplt
plt.imshow(I,cmap=plt.cm.gray,interpolation='none')#Thecmap=plt.cm.grayrender
stheimageingray
plt.show()

Wecandefinetwooperationsonthisclassofsignals,theadditionandmultiplicationbyascalar.Theadditionoftwo
imagesisdefinedlikethestandardmatrixaddition
I 11,1

I1 + I2 =

I 11,64

I 164,1

I 164,64

I 21,1

I 264,1

I 21,64

I 11,1 + I 21,1

I 264,64

I 164,1 + I 264,1

I 11,64 + I 21,64

I 164,64 + I 264,64

Themultiplicationbyascalarisalsodefinedlikethecorrespondingmatrixoperation

I 11,1

I 1 =

I 164,1

I 11,64

I 164,64

I 11,1

I 164,1

I 11,64

I 164,64

Wecanverifythatthespaceof64by64imagesendowedwiththesetwooperationsdefinesanappropriatevector
space, that is that it satisfied properties (1) to (8) in slide 42. We can also endow this vector space with a scalar
productdefinedas

I 1, I 2 =

64
n=1

64

m=1

I 1n,m I 2n,m .

Observethatthisdefinitionofthescalarproductcorrespondstotheubiquitousonein R4096 .Thisresultisobtained


bystackingthecolumnsofthe 64 64 matrixina 64 64

= 4096 1

vector.

Anatural(canonical)basisforthisspaceisformedbythesetofmatriceswhereonlyoneelementequalstooneand
alltheothersequal0.Thefollowingtablerepresentsthesebasisvectors.Ofcourse,itiscumbersometoenumerate
themall.Wedisplaythetwofirstones, e1 and e2 ,aswellasthelastone e4096 .Thisgivesalreadyagoodidea.On

eachrow,werepresent,ontheright,thebasisvectorand,ontheleft,thecorrespondingimage.Intheseimages,a
blackpixelcorrespondstoavalueof1,agrayoneto0andawhiteoneto1.

0
=

1
=

e1

e2

andsoonuntil

0
=

e4096

Itlooksasifallimagesareidentical.Lookmoreclosely,youwillnoticethatthereisineachasmallwhitepixel,on
the1strowand1stcolumninthefirstimage,forexample.
Suppose we would like to transmit this image over a communication channel. At the sender side, the image is
projected in the canonical basis and the resulting coefficients are sent. This simply correspond to sending
individuallytheintensityofeachpixel.Supposethereisanerrorduringtransmissionsuchthatonlythefirsthalfof
the coefficients is correctly transmitted. The received images is only an approximation of the original one and the
missingpixelsarereplacedbya0correspondtoholesintheimage(inthiscasetheentirerighthalf)
Wecancomputethedistancebetweenthetwoimages,i.e.,thenormoftheerror.
In[5]:

In[5]: #Initializationofimage
importmatplotlib.pylabasplt
I_approx=np.array(plt.imread('Num_Ex_3/camera_blurred.jpg'),dtype=float64)
I_approx[:,I_approx.shape[1]/2:]=0
plt.imshow(I_approx,cmap=plt.cm.gray,interpolation='none')
plt.title('Approximation')
plt.show()
importmathasm
#Errorcalculation
error=II_approx
distance=m.sqrt(sum(sum(error*error)))
print'Thedistancebetweentheoriginalandapproximateimageis:',distance

Thedistancebetweentheoriginalandapproximateimageis:6586.03697226

The question is whether we can do better by using another basis than the canonical one. The answer is yes.
Consider for example the Haar basis defined by the set of matrices. The following table represents the first fours
basis vectors 1 , , 4 . Again, we display on each row, on the right, the basis vector and, on the left, the
correspondingimage.Ineachimage,ablackpixelcorrespondstoavalueof 1,agrayoneto 0 andawhiteoneto
1

4 =

3 =

WeobservethatHaarbasisiscomposedofthescaledandshiftedversionofthesamesignal
1if 0 t < 1/2
(t) = 1if 1/2 t 1

0otherwise

We can verify that this basis is indeed orthogonal by computing the scalar product between any two matrices i
and j ,forexample,
(psi_i*psi_j).sum()=0//whenorthogonal
Asbeforeweprojecttheimageontothisbasisandsendthecoefficientsoveracommunicationchannelthatlooses
thesecondhalfofthecoefficients.Thisisimplementedinthefollowingcode.
In[6]: defhaar(N):

#AssumingNisapowerof2
importnumpyasnp
importmathasm
importscipyassc
h=np.zeros((N,N),dtype=float)
h[0]=np.ones(N)/m.sqrt(N)
forkinrange(1,N):

p=sc.fix(m.log(k)/m.log(2))
q=float(kpow(2,p))
k1=float(pow(2,p))

t1=float(N/k1)
k2=float(pow(2,p+1))
t2=float(N/k2)

foriinrange(1,int(sc.fix(t2))+1):
h[k,i+q*t11]=pow(2,(p/2))/m.sqrt(N)
h[k,i+q*t1+t21]=pow(2,(p/2))/m.sqrt(N)

returnh

importnumpyasnp

#Loadimage
importmatplotlib.pylabasplt
I=np.array(plt.imread('Num_Ex_3/camera_blurred.jpg'),dtype='float64')
size=I.shape
#Arrangeimageincolumnvector
I=I.flatten()
#GenerateHaarbasisvector(rowsofH)
H=haar(4096)

#Projectimageonthenewbasis
I_Haar=np.dot(H,I)

#Removethesecondhalfofthecoefficient
I_Haar[2048:4095]=0
#Recovertheimagebyinvertingchangeofbasis
I_Haar=np.dot(H.T,I_Haar)
#Rearrangepixelsoftheimage
I_Haar=I_Haar.reshape(size)
Inthiscase,theimagerecoveredatthereceiversidelookslike
In[7]: imshow(I_Haar,cmap=plt.cm.gray)
show()

withadistancetotheoriginalimageof
In[8]: I=np.array(plt.imread('Num_Ex_3/camera_blurred.jpg'),dtype='float64')
error_h=II_Haar
importmathasm
distance=m.sqrt(sum(sum(error_h*error_h)))
print'ThedistancebetweentheoriginalimageandtheHaarapproximationis',dist
ance
ThedistancebetweentheoriginalimageandtheHaarapproximationis1319.4676957

This happens because the Haar basis decomposes the image into a successive approximation. The first
coefficients in the basis correspond to a coarse and smoothed version of the image (low frequency) whereas the
subsequent coefficients represent the details in the image (high frequency). Then, in our scenario, we recover a
coarseryetcompleteversionoftheimage.

GramSchmidtorthonormalizationprocedure
Inthepreviousexample,wehaveseenhowanorthonormalbasisisusedtocomputeasuccessiveapproximationof
a vector. But what happens if the set of vector spanning the space under consideration is not orthonormal? The
GramSchmidtorthonormalizationprocedureenablestofindtheorthonormalbaseofthisspace.
In[9]: defgs_orthonormalization(V):

#Visamatrixwhereeachcolumncontains
#thevectorsspanningthespaceofwhichwewanttocomputetheorthonormalbas
e
#Willreturnamatrixwhereeachcolumncontainsanorthonormalvectorofthe
baseofthespace

numberLines=V.shape[0]
numberColumns=V.shape[1]

#Uisamatrixcontainingtheorthogonalvectors(nonnormalized)
fromnumpy.linalgimportnorm
importnumpyasnp
U=np.zeros((numberLines,numberColumns))
R=np.zeros((numberLines,numberColumns))

forindexColumninrange(0,numberColumns):
U[:,indexColumn]=V[:,indexColumn]

forindexinrange(0,indexColumn):
R[index,indexColumn]=np.dot(U[:,index],V[:,indexColumn])
U[:,indexColumn]=U[:,indexColumn]R[index,indexColumn]*U[:,index]


R[indexColumn,indexColumn]=norm(U[:,indexColumn])
U[:,indexColumn]=U[:,indexColumn]/float(R[indexColumn,indexColumn])

returnU
Letusconsiderasimpleyetinterestingexample.Taketheorthonormalbasein R3 ,whichreads

ex = 0

, ey

= 1

, ez

= 0

Thefigurebelowdepictsthevectorsoftheorthogonalbasis.

Thefollowingrotationmatrices

Rx () = 0

Ry () =

cos

sin

sin

cos

cos

sin

sin

cos

cos

sin

cos

sin

Rz () = sin

cos
0

performacounterclockwiserotationofanangle , ,and withrespecttotheaxis x, y ,and z ,respectively.


Let's rotate the ex w.r.t. the z axis, ey w.r.t. the x axis, and ez w.r.t. the y axis, with
= /6

= /3

= /4

, and

0.8660

v 1 = Rz ()ex = 0.5000

0
0

v 2 = Rx ()ey = 0.5000

v 3 = Ry ()ez =

0.8660
0.7071
0
0.7071

Thesoobtainedthreevectors,depictedinthefigureabove,donotformanorthogonalbasisanymore.Forinstance
< v 1 , v 2 >= 0.25

We can use the GramSchmidt procedure to reobtain an orthonormal basis. By feeding the procedure with the
matrixcomposedofthethreevectors v 1 , v 2 ,and v 3 weobtain

E =

0.866

0.2236

0.5

0.3873

0.8944

0.4472

0.7746 .
0.4472

Thefigurebelowpresentstheneworthogonalvectors.

Onecaneasilycheckthatthecolumsofthematrix E formanorthonormalbasis,i.e.
1

E = 0

0 .

Finally,letuscheckthecode:
In[10]: v1=np.array([[0.866,0.5,0.0]]).T
v2=np.array([[0.0,0.5,0.866]]).T
v3=np.array([[0.7071,0.0,0.7071]]).T
V=np.concatenate((v1,v2,v3),axis=1)
print'matrixV'
printV

E=gs_orthonormalization(V)
print'Orthonormalizedbasis:'
printE
print'Checktheorthonormality:',(np.dot(E.T,E)np.eye(3)).sum()
matrixV
[[0.8660.0.7071]
[0.50.50.]
[0.0.8660.7071]]
Orthonormalizedbasis:
[[0.866019050.223615650.44722147]
[0.5000110.387302310.77458758]
[0.0.894423260.44722147]]
Checktheorthonormality:1.66533453694e16

Exercises
Considerthethreevectors

0.8660

v 1 = 0.5000

0
0

v 2 = 0.5000

0.8660
1.7320

v 3 = 3.0000

3.4640

ApplytheGramSchmidtprocessonsuchvectorsandprovidetheoutputmatrix E .
Doestheoutputmatrix E representasetoforthonormalvectors?
Ifyes,why?
1.dot(E.T,E)==I
2.det(E)==0
3.rank(E)==3
Ifnot,why?
1.dot(E.T,E)isnottheidentitymatrix
2.det(E)>0
3.rank(E)>1

CreatedFri11Apr20148:57AMEDT

LastModifiedTue20Jan20155:07AMEST

Você também pode gostar