Академический Документы
Профессиональный Документы
Культура Документы
I.
CalculateGradientinXandinYdirection:
Thisisrathersimple,Ijustiteratethrougheverypixel,andsetittothedifferenceofthe
pixelinthenextcolumn/nextrow.Belowisthecode.AlsoImadesuretoassigntheboundary
valuestozeros.
defimageGradientX(image):
ret=np.copy(image)
foriinrange(0,ret.shape[0]):
forjinrange(0,ret.shape[1]1):
ret[i][j]=abs(int(ret[i][j+1])int(ret[i][j]))
ret[i][ret.shape[1]1]=0
returnret
defimageGradientY(image):
ret=np.copy(image)
foriinrange(0,image.shape[0]1):
forjinrange(0,image.shape[1]):
ret[i][j]=abs(int(ret[i+1][j])int(ret[i][j]))
forjinrange(0,image.shape[1]):
ret[image.shape[0]1][j]=0
returnret
II.
CalculateCorrelationbyapplyingakerneltothepicture:
Thisisalsostraightforward.Ijustiteratethrougheverypixel,andforeachpixel,apply
theformulafromthelecture.Belowisthecode
defcomputeGradient(image,kernel):
ret=np.copy(image)
forjinrange(0,image.shape[1]):
ret[0][j]=0
ret[image.shape[0]1][j]=0
foriinrange(0,image.shape[0]):
III.
ret[i][0]=0
ret[i][image.shape[1]1]=0
foriinrange(1,image.shape[0]1):
forjinrange(1,image.shape[1]1):
val=0
foruinrange(0,3):
forvinrange(0,3):
val=val+kernel[u][v]*image[i+u1][j+v1]
ret[i][j]=int(val)
returnret
EdgeDetection:
1. OriginalPicture:
2.Methods:
ItriedseveralmethodsotherthanCannysEdgeDetector,andhavefoundtwowaystodeliver
decentresults.
A. Method1:
a. CalculateGradientinXandinYdirection
b. Foreachpixel,getthemaxofthevalues.Theresultisshownbelow
c. Now,toshowtheedgesinthepicture,Iusethekernelofsize3x3tosmoothout
thenoisefirst,thenchoosethethresholdtobetheaverageofallthepixel.I
d. Iteratethroughallpixel,assignitto255ifitsvalueisgreaterthan3timesthe
average,and0otherwise.Belowisthepicture
B.Method2:
a. CalculateGradientinXandinYdirection(Similartomethod1)
b. Foreachpixel,getthemaxofthevalues.(Similartomethod1)
c. Now,toshowtheedgesinthepicture,Iusethekernelofsize3x3tosmoothoutthe
noisefirst,thenchoosethethresholdtobetheaverageofallthepixel.I
d. Now,treattheproblemofedgedetectorasanomalydetection.Wewillusearolling
windows(Ipickedthesize30x30)tomoveacrossthepicture.
e. Calculatemeanandstandarddeviationofallvaluesfromthatwindows.Anypixelwith
valuegreaterthan
mean+std
isconsideredasanomaly,andhencechangethevalue
to255.Otherwise,weassign0toothers.Belowistheresult
3.CannyEdgeDetection
Forcomparisonpurposes,IhaveattachedtheresultofCannysEdgeDetectionbelowtoshow
thedifferences.
4.FurtherDevelopment:
Asonecansee,mymethodsdonthavetheresultasgoodasCannysEdgeDetectionyet.
Howevertheyhavealotofpotentialandcanbeimprove.Wecanchangethesizeofthekernel
toimprovesmoothing,wecanalsofinetunethethresholdtogetbetterresults.Thebest
improvementIbelieveistocombinemultiplemethodtogether,andhavesomevoting
mechanismtotellwhetherapixelshouldbelongtoanedgeornot.