Solutions

Practice Midterm
CMSC426
Themidtermwillcovermaterialuptoandincludinghumanperceptualgrouping.
Herearesometopicsitwillbehelpfultomasterforthemidterm:
• Correlation
o Howdoyouperformcorrelationin1Dor2D?Whatisaboxfilter?
HowdoyoucreateaGaussianfilter?Howdoyoucreatefiltersto
computederivatives?Fouriertransforms(onlyforchallenge
problems).
• Edgedetection
o Imagegradients
§ Howtocomputetheimagegradientforacontinuousor
discreteimage.
§ Whatisthedirectionandmagnitudeofagradient?
§ Howtocomputethedirectionalderivativeofanimage.
§ Whatdoesthegradienttellyouaboutthedirectioninwhich
theimagechangesmostrapidly,andhowrapidlyitchangesin
thatdirection.
o Non-maximumsuppression
• Humanperceptualgrouping
o Basicgestaltgroupingcues.
• TherewillnotbequestionsaboutMatlabcoding.
Inparticular,youshouldunderstandafewequations:
1Dand2Dcorrelation
F ! I ( x) =
N
∑ F (i) I ( x + i)
F ! I ( x, y) =
i =− N
N
N
∑ ∑ F (i, j)I ( x + i, y + j)
j =− N i =− N
1D and 2D Convolution
F ∗ I ( x) =
N
∑ F (i) I ( x − i)
i =− N
Definition of a gradient:
⎛ ∂I ∂I ⎞
∇I = ⎜⎜ , ⎟⎟ ⎝ ∂x ∂y ⎠
N
F ∗ I ( x, y) =
N
∑ ∑ F (i, j) I ( x − i, y − j)
j =− N i =− N
Formula for using the gradient to determine how the image changes as you move in a
particular direction
I ( x + Δ cos θ , y + Δ sin θ ) − I ( x, y)
≈ v, ∇I
where v ≡ (Δ cosθ , Δ sin θ ) Definitionofthepartialderivative.
∂I ( x, y)
I ( x + Δx, y) − I ( x, y)
= lim
Δx→0
∂x
Δx
I’maskingyouto“memorize”theseformulasbecauseIfeelthatifyoureally
understandwhatisintheformula,youwillhaveitmemorized.
Belowaresomepracticeproblems.Thesearenotguaranteedtocovereverything
youneedtoknow,butshouldbehelpful.
PracticeProblems
1. Showtheresultofconvolvingthekernel,k,withtheimage,I.
Theoutputlookslike:
0,0,…,0,¼,1,2,3,4,5,6,7,8,9,7¼,2½,0,0,0…
2. Giveanexamplethatshowsthatcorrelationisnotassociative
Trycombiningthederivativefilterwithasmoothingfilter.
Tokeepthingsassimpleaspossible,use[0,-1,1]asthederivativefilter,and[1/3,1/3,
1/3]asthesmoothingfilter.
3. Supposewehaveanimagewhoseintensitiesaredescribedbytheequation
x3-2xy+7.
a) Whatisthemagnitudeoftheimagegradientatthepoint(7,4)?
Thegradientis(3x2-2y,-2x).At(7,4)thisis(138,-14).Themagnitudeofthatis
sqrt(1472+16).IfIaskedaquestionlikethatonthetest,thenumberswouldbekinder
toyou.ButitisfinetojustwritetheanswerasIjustdid,withoutsimplifying.
b) Supposewemoveintheimagetothepoint(7+δ, 4 + 2δ). Use the
image gradient to predict what the intensity will be at this point.
138δ - 14δ=124δ.
c) Challenge problem: What is the actual intensity at this point. What does
this tell you about the accuracy of your solution using the gradient?
Ifyouworkthisout,you’llfindit’sthesameastheansweryougotfor(b)plussome
termsthatarequadraticorcubicinδ.Whenδ issmall,thesetermsbecomeverysmall.
4. Considerthefollowing,smallimage:
4
6
9
12
15
3
4
6
9
12
3
3
4
6
9
2
3
3
4
6
2
1
3
3
4
a) Computethemagnitudeandthedirectionofthegradientforthe
centralpoint(4).
Let’susefilters[-1/2,0,½]and[-1/2;0;½]forthexandyderivatives.Thenweget
forthegradient(3/2,-3/2).Themagnitudeofthisissqrt(18)/2.Thedirectionisthe
gradientdividedbythemagnitude(aunitvectorinthatdirection).Oryoucouldsay
thedirectionisadiagonalvectorpointingtowardsthe9.
b) WouldtheCannyedgedetectorconsiderthispointtobeanedge?
Explainwhyorwhynotindetail,describingwhatconditionsare
neededtodecidethis.
Youneedtocomparethegradientmagnitudetothegradientmagnitudeatthe
diagonalswithvaluesof9and3(theitalicizednumbershere).Youcantelljustby
lookingthatthemagnitudeofthegradientatthe9willbelarger,sono,it’snotan
edge.
WorkshopQuestions
1. ConsideranimageinwhichtheintensitiesaregivenbytheequationI(x,y)=
x2+2y.
a. Whatisthegradientoftheimage?
(2x,2).
b. Whatisthegradientatthelocation(x,y)=(2,3)?
(4,2)
c. Atthelocation(x,y)=(2,3),whatisthedirectionalderivativeinthe
directionof(3,2)?Thatis,ifyouweretomoveasmallamountinthis
direction,howmuchwouldyouexpecttheimagetochange?
Ifyoumovealittleinthisdirection,saymovingby(3δ, 2δ), the change in intensity is
14δ. The distance you moved is sqrt(13)δ. So the direction derivative is:
14δ/sqrt(13)δ=14/sqrt(13).
2. Makepartofadiscreteimagewithintensitieschosenaccordingtothe
equationin1.Thatis,forexample,makeanimagesothatthepixelat
location(7,3)hasthevalue72+2*3=55.
3 6 11 18 27
5 8 13 20 29
7 10 15 22 31
9 12 17 24 33
11 14 19 26 35
a. Ifyouweretocomputethediscreteimagegradientateachpixelusing
derivativefilters,whatvalueswouldyougetfortheimagegradient
everywhere?
I’llusefilterslike[-1/2,0,½].Atthepointwiththevalue15(x=3,y=3),thegradient
is:(6,2).
b. Howdothesecomparetothegradientsyougetinproblem1?Ifthere
isadifference,whyisthis?
Theformulapredicted(6,2).Theyarethesame.Noticethisisn’talwaystrue.
Whenthiswillbetrueisalittletricky.Here’saquickdiscussion.Theycomponentof
thegradientwillalwaysmatchtheformulawhenwecomputeitdiscretely,because
the2ndderivativeoftheimagewithrespecttoyis0.Noticethatthe2ndderivative
withrespecttoxisn’t0,butit’sconstant.Thismeansthatusingthefilter[-1/2,0,½]
wegetthesameresultastheformula,butthiswouldnotbetrueforthefilter[0,-1,1].
Understandingwhythisistrueisalittlemoreadvanced,butinteresting.
3. Supposeyouhaveanimageinwhichtheintensitiesaregivenbytheequation
I(x,y)=100–x2–y2+xy+x+4y.Whatistheintensityofthebrightestpoint
intheimage?
Thekeyinsighthereisthatatthebrightestpoint,thegradientwillbe(0,0).
Otherwise,therewouldbeadirectionwecouldmoveintomakeitbrighter.Thisisjust
likethe1Dcase,wherethederivativeis0attheextremalvaluesofafunction.
ThegradientI(-2x+y+1,-2y+x+4).Settingthesebothto0givestheequations:
-2x+y+1=0
-2y+x+4=0.
Sowegety+1–4y+8=0.
9=3y.y=3.x=2.Intensity=100–4–9+6+2+12=107.
4. SupposegisaGaussianfilterwithastandarddeviationof1.
a. IfIfilteranimagetwicewithg,showwhythisisequivalenttofiltering
itoncewithalargerGaussian.
b. WhatshouldthestandarddeviationofthelargerGaussianfilterbe?
I’mgoingtoleavethisoneasachallengeproblem.Herearesomehints.Ifyou’re
interested,firsttrycombiningthefiltersusingMatlab.Plottheresult,andseethatit
lookslikeaGaussian.UseMatlabtoseewhatthevarianceofthenewfilteris.
Toshowthisanalytically,youneedtowriteconvolutionasacontinuousoperation,
usingintegrals,Thenyoucanplayaroundwiththisexpression,removingoneofthe
integralsyouget,andputtingitintheformofasingleconvolutionwithalarger
Gaussian.
5. UsingMatlab,createafilterthathasthesameeffectasapplyinga1x3
averagingfilter20times.Whatistherelationshipbetweenthisfilteranda
Gaussianfilter?
a=ones(1,3)*(1/3);
f=a;
forI=1:19f=imfilter(a,f);end
figure;
plot(f);
6. Createa1Dfilterthatsmoothsanimageandtakesaderivativeatthesame
time.
a. Dothisusinganaveragingfiltertosmooththeimage.
[0,-1,1]*[1/3,1/3,1/3]lookslike[-1/3,0,0,1/3].Notsosmooth.
b. DoitusingaGaussiantosmooth.
DothisinMatlab.
c. Whatdoyouthinkaboutthedifferences?
LooksalotsmootherwiththeGaussian.Thisgetsevenworsewhenyouuseavery
wideaveragingfilter,andawideGaussian.