I'vegot a redrectanglestucktomyhead, andtheideaofopticflowis I cancreate a velocitymapthatallowsmetointeractwiththingsonthescenerysoyoucansee I canmanipulatetherectanglewithmyhands.
I'm doingitrathergingerlyandslowly, becauseif I hittherectangle, itgetsmorevelocity.
It's not a verycomputational, efficientalgorithm, butsecondly, wecanassumethatthedifferencesbetweenframesonthewholeareminimal, particularly, it's a humanbeing.
I can't movemyarmssofast, butitmakes a massivedifferenceat, say, 30 framespersecondForeachpatchinthesearcharea, I recordthesimilaritywiththebasepatchonDDE.
Inthisinstance, itwouldbesomewherelikehere.
They'renotexactlythesame, buttheyrepresent a similarpartofsimilarfeatureoffthearmthanthereforeforthispatch.
Ifyou'veseentheWebcamvideo, we'regoingtouse a livelycalled S CappieTauhandlethewebcamforus, and I'vederived a classfromthecouncilgameenginecalledOpticFlowbecausetheperformanceisgoingtosufferinthisalgorithm, I'm usingquite a lowresolutionconsole, 80 by 60 characterswhereeachcharacteris 16 by 16 pixelsand I'vetakenthelibertyofalreadyenteringtheWebcamcapturecode.
Wedon't needtoseeitagain, andthisinvolved a union.
Usingthescreenwidthandscreenheightfunctionsonintheunusedupdatefunction, I'vecreated a Lambdafunctiontosimplydrawonimagetothescreen.
Inthiscase, animageisgoingtobe a twodimensionalfloatingpointofrape.
Itjustit's a ratesthroughthearrayandchoosestheappropriatecombinationofcharactersandcolorsthatrepresentthatpixel.
Thefirstthingtheyarenewsupdatefunctionisgoingtodoiscapturetheimageandconvertitintonormalizedgrayscalevalues, whichwillusefortherestofthealgorithmontheframereturnedbythecameraafterprocessingforLouEminenceisgoingtobestoredin a two d raycalled F NewCamera.
Solet's addthatnowfortheRays.
I'm justcreating a pointerto a floatingpointvariableandintheunusedcreatefunction.
I'm goingtoallocatethememorydependingonthesizeoftheconsole, and I'm goingtoinitializeit 20 justtomakesureeverythingsofarisinorder.
Butjustbecause I'm talkingmeansit's movingaround a littlebit, and I don't reallythinkthatthisshouldinteractwithworld.
So I wanttoknow, removesmallmovementsandbysmallmovements, I meanrapidandlargechangesbetweenframes.
Theseairnotlikelytobenaturaltoremovethetemporal, highfrequencycomponentsoftheimage, I'm goingtoadd a lowpassfilter, lowpassfilteredimageisgoingtobeyetanothertwo d ray.
I'vecreatedthatfurtherupinthecode.
Intothisimage, I accumulate a smallamountoffthedifferencebetweenthefilteredimageonthenewimage.
Let's create 24 loopsthatit's a rateacrossthescreenondownthescreen.
I'm goingtoinitializethevariablesforthispixel.
Andforeachpixel, we'regoingtostorevariablecalledPatchDifferenceMax, whichissetto a verylargenumber, whichisgoingtokeeptrackofwhichoneofoursearchpatchesistheclosestfittoourbasepatch.
I'm alsogoingtoinitialize 202 moretwodimensionalvectorsflow, axeandflowwhite, whichofthe X and Y componentsofouroverallmotionforthatpixel.
Now I needthenextstageofmyfourloopsforeachpixelweneedtogoover a searcharea.
Inthiscase, thesearchareaisjustgoingtobe a rectanglesurroundingtheoriginalpixel, sogoingtocreate a vectorthatrepresentsthevectorfromtheoriginalpixeltothecenteroffthesearcharea.
I wenttocreateanothervariable F accumulateddifference, whichisgoingtobethetotaldifferencefor a singlepatchatthissearchlocation.
Sincethesearchvectorrepresentsthemiddleoffthepatchthat's beingsearched, I cancreatetheactualpixelcoordinatesasanoffsetofthatsearchfactor, assumingthatthesearchvectorisinthemiddleofthepatch.
Soit's verysimilartowhatwehadbeforeandin a similarway I canworkouttheindicesrequiredforthebasepatch, whichisbasicallyjustthepatchsizearoundthepixelthatweretesting.
Nowthat I'veworkedouttheindicesforthepixelsinboththesearchpatchlocationonthebasepatchlocation, I canextracttheluminousvaluesofthetwosuccessiveframesatthoselocationsandthenupdatemycumulateddifference.
Nowweeffectivelyhave a to D mapoffvelocityperpixel.
Wecanhavesomefunwiththisbyusingthosevectorsaspartof a physicscalculationtocontrolanobject I theRedSquareyousawmemanipulatingatthestartofthevideo.
So I'vecreatedtwosetsofvectors, obviouslythepears, becauseit's an X and Y componentonewhichrepresentsthepositionononewhichrepresentsthevelocityonDenonusercreate.
Sointhiscase, inour X componentfield, I usedtheballscurrentpositiontofindthecorrectindexintothatfield, whichwillgivemethe X componentofftheopticflowvector.
Andi.
I multiplythatby a constantthatrepresentssomesortofspeed.
Thiswillneedtobetuneddependingonyourapplication, and I alsoinclude F elapsedtimejusttokeepthingsrunningsmoothly.