6
 ASSIGNMENT 5 GRADIENT AND EDGE DETECTION I. Calculate Gradient in X and in Y dir ectio n: This is rather simple, I just iterate through every pixel, and set it to the difference of the pixel in the next column/ next row. Below is the code. Also I made sure to assign the boundary values to zeros. def imageGradientX(image): ret = np.copy(image) for i in range(0, ret.shape[0]): for j in range(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 return ret def imageGradientY(image): ret = np.copy(image) for i in range(0, image.shape[0] -1): for j in range(0, image.shape[1]): ret[i][j] = abs(int(ret[i+1][j]) - int(ret[i][j])) for j in range(0, image.shape[1]): ret[image.shape[0] -1][j] = 0 return ret II. Calc ulat e Corr elat ion by appl ying a kern el to the pictu re: This is also straight forward. I just iterate through every pixel, and for each pixel, apply the formula from the lecture. Below is the code def computeGradient(image, kernel): ret = np.copy(image) for j in range(0, image.shape[1]): ret[0][j] = 0 ret[image.shape[0] -1][j] = 0 for i in range(0, image.shape[0]): ret[i][0] = 0 ret[i][image.shape[1] -1] = 0

assignment5 (1)

Embed Size (px)

DESCRIPTION

Several methods for Gradient and Edge Detection

Citation preview

  • ASSIGNMENT 5

    GRADIENT AND EDGE DETECTION I. CalculateGradientinXandinYdirection:

    Thisisrathersimple,Ijustiteratethrougheverypixel,andsetittothedifferenceofthepixelinthenextcolumn/nextrow.Belowisthecode.AlsoImadesuretoassigntheboundaryvaluestozeros.

    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

    returnretdefimageGradientY(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

    returnretII. CalculateCorrelationbyapplyingakerneltothepicture:

    Thisisalsostraightforward.Ijustiteratethrougheverypixel,andforeachpixel,applytheformulafromthelecture.BelowisthecodedefcomputeGradient(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]): 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)

    returnretIII. EdgeDetection:

    1. OriginalPicture:

    2.Methods:

  • ItriedseveralmethodsotherthanCannysEdgeDetector,andhavefoundtwowaystodeliverdecentresults.

    A. Method1:a. CalculateGradientinXandinYdirectionb. Foreachpixel,getthemaxofthevalues.Theresultisshownbelow

    c. Now,toshowtheedgesinthepicture,Iusethekernelofsize3x3tosmoothoutthenoisefirst,thenchoosethethresholdtobetheaverageofallthepixel.I

    d. Iteratethroughallpixel,assignitto255ifitsvalueisgreaterthan3timestheaverage,and0otherwise.Belowisthepicture

  • B.Method2:

    a. CalculateGradientinXandinYdirection(Similartomethod1)b. Foreachpixel,getthemaxofthevalues.(Similartomethod1)c. Now,toshowtheedgesinthepicture,Iusethekernelofsize3x3tosmoothoutthe

    noisefirst,thenchoosethethresholdtobetheaverageofallthepixel.Id. Now,treattheproblemofedgedetectorasanomalydetection.Wewillusearolling

    windows(Ipickedthesize30x30)tomoveacrossthepicture.e. Calculatemeanandstandarddeviationofallvaluesfromthatwindows.Anypixelwith

    valuegreaterthanmean+stdisconsideredasanomaly,andhencechangethevalueto255.Otherwise,weassign0toothers.Belowistheresult

  • 3.CannyEdgeDetectionForcomparisonpurposes,IhaveattachedtheresultofCannysEdgeDetectionbelowtoshowthedifferences.

  • 4.FurtherDevelopment:Asonecansee,mymethodsdonthavetheresultasgoodasCannysEdgeDetectionyet.Howevertheyhavealotofpotentialandcanbeimprove.Wecanchangethesizeofthekerneltoimprovesmoothing,wecanalsofinetunethethresholdtogetbetterresults.ThebestimprovementIbelieveistocombinemultiplemethodtogether,andhavesomevotingmechanismtotellwhetherapixelshouldbelongtoanedgeornot.