Basis for Grayscale Images
date post
17-Jul-2016Category
Documents
view
10download
2
Embed Size (px)
description
Transcript of Basis for Grayscale Images
LinkYourCourseworktoYourIdentity GetStarted(https://www.coursera.org/signature/course/dsp/974034?utm_source=spark&utm_medium=banner)
(https://courserahelp.zendesk.com/hc/requests/new)
HelpCenter(https://accounts.coursera.org/i/zendesk/courserahelp?
return_to=https://courserahelp.zendesk.com/hc/)
NumericalExamples:BasisforGrayscaleImages(Python)
Thegoalofthisexampleistoillustratethetheoreticalconceptsstudiedinclass(vectorspace,scalarproduct,basisand approximation)with a specific class of signals, grayscale images.Consider the following grayscale image ofsize64by64pixels.
In[1]: fromIPython.displayimportImageImage(filename='Num_Ex_3/camera_blurred_big.jpg')
It is representedbya square matrix ,where eachelement corresponds to the intensity of a pixel. ThematrixIisdeterminedfromtheimageusingtheimreadfunction.
In[2]: %pylabinlineimportmatplotlib.pylabaspltI=np.array(plt.imread('Num_Ex_3/camera_blurred.jpg'),dtype=float64)
Forexample,thefirstcolumnofthisimageisa vector
In[3]: I[:,0]
Out[1]:
g
Populatingtheinteractivenamespacefromnumpyandmatplotlib
g
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.,
Conversely,onecandisplaythearrayasanimagebyusingthepylab.imshowfunctionfrommatplotlib
In[4]: importmatplotlib.pylabaspltplt.imshow(I,cmap=plt.cm.gray,interpolation='none')#Thecmap=plt.cm.grayrenderstheimageingrayplt.show()
Wecandefinetwooperationsonthisclassofsignals,theadditionandmultiplicationbyascalar.Theadditionoftwoimagesisdefinedlikethestandardmatrixaddition
Themultiplicationbyascalarisalsodefinedlikethecorrespondingmatrixoperation
Wecanverifythatthespaceof64by64imagesendowedwiththesetwooperationsdefinesanappropriatevectorspace, that is that it satisfiedproperties (1) to (8) inslide42.Wecanalsoendow thisvectorspacewithascalarproductdefinedas
Observethatthisdefinitionofthescalarproductcorrespondstotheubiquitousonein .Thisresultisobtainedbystackingthecolumnsofthe matrixina vector.
Anatural(canonical)basisforthisspaceisformedbythesetofmatriceswhereonlyoneelementequalstooneandalltheothersequal0.Thefollowingtablerepresentsthesebasisvectors.Ofcourse,itiscumbersometoenumeratethemall.Wedisplaythetwofirstones, and ,aswellasthelastone .Thisgivesalreadyagoodidea.On
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.])
C C
C
C
C
C
*
)
*)
*)
g g
!
!
!
eachrow,werepresent,ontheright,thebasisvectorand,ontheleft,thecorrespondingimage.Intheseimages,ablackpixelcorrespondstoavalueof1,agrayoneto0andawhiteoneto1.
andsoonuntil
Itlooksasifallimagesareidentical.Lookmoreclosely,youwillnoticethatthereisineachasmallwhitepixel,onthe1strowand1stcolumninthefirstimage,forexample.
Suppose we would like to transmit this image over a communication channel. At the sender side, the image isprojected in the canonical basis and the resulting coefficients are sent. This simply correspond to sendingindividuallytheintensityofeachpixel.Supposethereisanerrorduringtransmissionsuchthatonlythefirsthalfofthecoefficients iscorrectly transmitted.The received images isonlyanapproximationof theoriginaloneand themissingpixelsarereplacedbya0correspondtoholesintheimage(inthiscasetheentirerighthalf)
Wecancomputethedistancebetweenthetwoimages,i.e.,thenormoftheerror.
In[5]:
!
!
!
In[5]: #Initializationofimageimportmatplotlib.pylabaspltI_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#Errorcalculationerror=II_approxdistance=m.sqrt(sum(sum(error*error)))print'Thedistancebetweentheoriginalandapproximateimageis:',distance
The question is whether we can do better by using another basis than the canonical one. The answer is yes.Consider for example theHaarbasis definedby the set ofmatrices.The following table represents the first foursbasis vectors . Again, we display on each row, on the right, the basis vector and, on the left, thecorrespondingimage.Ineachimage,ablackpixelcorrespondstoavalueof ,agrayoneto andawhiteoneto.
Thedistancebetweentheoriginalandapproximateimageis:6586.03697226
Z
Z
Z
WeobservethatHaarbasisiscomposedofthescaledandshiftedversionofthesamesignal
Wecanverify that thisbasis is indeedorthogonalbycomputing thescalarproductbetweenany twomatricesand ,forexample,
(psi_i*psi_j).sum()=0//whenorthogonal
Asbeforeweprojecttheimageontothisbasisandsendthecoefficientsoveracommunicationchannelthatloosesthesecondhalfofthecoefficients.Thisisimplementedinthefollowingcode.
In[6]: defhaar(N):#AssumingNisapowerof2importnumpyasnpimportmathasmimportscipyassch=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))
Z
Z
Z
Z0
"
!
`JG U
`JG U
`PUIFSXJTF
Z
%
Z
&
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#Loadimageimportmatplotlib.pylabaspltI=np.array(plt.imread('Num_Ex_3/camera_blurred.jpg'),dtype='float64')size=I.shape
#ArrangeimageincolumnvectorI=I.flatten()#GenerateHaarbasisvector(rowsofH)H=haar(4096)#ProjectimageonthenewbasisI_Haar=np.dot(H,I)#RemovethesecondhalfofthecoefficientI_Haar[2048:4095]=0
#RecovertheimagebyinvertingchangeofbasisI_Haar=np.dot(H.T,I_Haar)
#RearrangepixelsoftheimageI_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_Haarimportmathasmdistance=m.sqrt(sum(sum(error_h*error_h)))print'ThedistancebetweentheoriginalimageandtheHaarapproximationis',distance
This happens because the Haar basis decomposes the image into a successive approximation. The firstcoefficients in the basis correspond to a coarse and smoothed version of the image (low frequency)whereas thesubsequent coefficients represent the details in the image (high frequency). Then, in our scenario, we recover acoarseryetcompleteversionoftheimage.
GramSchmidtorthonormalizationprocedure
Inthepreviousexample,wehaveseenhowanorthonormalbasisisusedtocomputeasuccessiveapproximationofa vector. But what happens if the set of vector spanning the space under consideration is not orthonormal? TheGramSchmidtorthonormalizationprocedureenablestofindtheorthonormalbaseofthisspace.
In[9]: defgs_orthonormalization(V):#Visamatrixwhereeachcolumncontains#thevectorsspanningthespaceofwhichwewanttocomputetheorthonormalbase#WillreturnamatrixwhereeachcolumncontainsanorthonormalvectorofthebaseofthespacenumberLines=V.shape[0]numberColumns=V.shape[1]#Uisamatrixcontainingtheorthogonalvectors(nonnormalized)fromnumpy.linalgimportnormimportnumpyasnpU=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]
ThedistancebetweentheoriginalimageandtheHaarapproximationis1319.4676957
R[indexColumn,indexColumn]=norm(U[:,indexColumn])U[:,indexColumn]=U[:,indexColumn]/float(R[indexColumn,indexColumn])returnU
Letusconsiderasimpleyetinterestingexample.Taketheorthonormalbasein ,whichreads
, , .
Thefigurebelowdepictsthevectorsoftheorthogonalbasis.
Thefollowingrotationmatrices
!
4
!
5
!
6
J
4
DPT J
TJO J
TJO J
DPT J
C
5
DPT C
TJO C
TJO C
DPT C
D
6
DPT D TJO D
performacounterclockwiserotationofanangle , ,and withrespecttotheaxis , ,and ,respectively.
Let's rotate the w.r.t. the axis, w.r.t. the axis,and w.r.t. the axis,with , , and
Thesoobtainedthreevectors,depictedinthefigureabove,donotformanorthogonalbasisanymore.Forinstance.
D
6
DPT D
TJO D
TJO D
DPT D
J C D 4 5 6
!
4
6 !
5
4 !
6
5 J R C R
D R
D 2
6
!
4
J 2
4
!
5
C 2
5
!
6
2
2
We can use the GramSchmidt procedure to reobtain an orthonormal basis. By feeding the procedure with thematrixcomposedofthethreevectors , ,and weobtain
Thefigurebelowpresentstheneworthogonalvectors.
Onecaneasilycheckthatthecolumsofthematrix formanorthonormalbasis,i.e.
Finally,letuscheckthecode:
In[10]: v1=np.array([[0.866,0.5,0.0]]).Tv2=np.array([[0.0,0.5,0.866]]).Tv3=np.array([[0.7071,0.0,0.7071]]).TV=np.concatenate((v1,v2,v3),axis=1)print'matrixV'printV
2
2
2
CreatedFri11Apr20148:57AMEDT
E=gs_orthonormalization(V)
print'Orthonormalizedbasis:'printEprint'Checktheorthonormality:',(np.dot(E.T,E)np.eye(3)).sum()
Exercises
Considerthethreevectors
ApplytheGramSchmidtprocessonsuchvectorsandprovidetheoutputmatrix .
Doestheoutputmatrix representasetoforthonormalvectors?
Ifyes,why?
1.dot(E.T,E)==I2.det(E)==03.r