I'vealreadydonethatinothervideos, andit's enoughtosaythatwesimplyusetheescapeelibrarytocapture a framefrom a webcam.
Soinonusercreate, thewebcamisinitializedandinonuserupdate, I capturetheimagefromthewebcamperframeandconvertthepixelstofloatingpointandstoreitin a framecalledinput.
Fortunatelywithvideo, thisisquitesimplebecause a videocamerareturnssuccessiveframesintime, whichmeanswehave a builtinDeltatimebetweeneachframealongsidemovementintime.
Forexample, I mightwant a systemtoshutdownifnothinginthissceneismoving, I mean, whybothertakingmoreimagesifnothinghaschanged?
So I coulddetectthatbyaccumulatingthesumofallofthepixelsintheoutputimageandthencheckingthatagainst a thresholdvaluetotellme, hastherebeenenoughmotionintheimageforthesystemtoswitchonbitthreeLowpasttemporalfiltering, aswe'vejustseeninbittothevalueof a pixelchangesovertime.
Andifwelookover a longerperiodoftime, wemightseethatthepixelschangevaluesquiterapidlybetweenframes.
Thisiscallednoisebecausesensorsaren't perfect.
Lightingconditions, electronicsandallsortsofthingscouldinfluencethevalueof a pixel.
Inthelowpassedsectionoftheprogram, I'm doingsomeuserinputsso I canchangethevalueofthistempleconstant.
Andthen I iteratedthroughallofthepixelsin a frame.
I lookatthedifferencebetweentheinputontheoutput.
I scalethedifferencewithourtemporalcoefficient, andthen I accumulatethatdifferencebackintotheoutputframewiththisplussymbolforthisalgorithm.
Theoutputframeispersistentbetweenupdatesofthevideocamerafeed, meaningthatoutputpixelsouronlychangedby a smallamount, dependingonhowlargethechangewaasofftheinput.
Eventuallyitfeedsintotheimage, sothisisexaggeratedin a way I canusetheSETindexkeystochangethevalueofthisconstant, but I canmakeitveryslowindeed, whichmightnotimmediatelyseem a usefulthingtodo.
Butifyouwantedtodosomebackgroundsubtractionalgorithmsovermovingimages, thisisquite a nicewaytodoit.
Youcanaccumulatethebackgroundofanimageovertimeandthenusethatas a waytoisolatethingsintheforeground.
If I increasethevalueoftheconstant, itbecomesfarmorelife.
Let's keepgoing a bituntilthetwoimageslookverysimilarindeed.
Butifyougetittoohighthisconcert, you'llstartseeingtheperpixelcameranoisecomingbackintotheoutputimagesolowpassTemplefilteringis a greatwaytofilternoise, anditalsolooksallghostlyandcoolbitforconvolution.
IfwecanVolvotheimagewiththehorizontalcolonel, wewillseethehorizontaledges, andthenwe'llconveyalltheimagewiththeverticalcolonel, andwe'llseetheverticaledgeswillthencombinethehorizontaledgesandtheverticaledgesinto a singleoutputimagetoshowusalledgesasbeforewiththemotiondetection.
Thesignbitoftheresultoftheseconvolutionscancontainusefulandinterestinginformation, but I'm goingtothrowitawaybytakingtheabsolutevalueoftheresultsoftheseconvolutions.
Thesub l partofthecodeisexactlythesameastheconvolutionpartofthecodebefore, except I'm doingthetwothingsatonce.
Solookatmyreallyhurryarmthatit's quite a visiblething.
Sothat's that's reallyindicatedtextureBitsixmorphologicaloperations, eventhoughthatsoundslike a mouthfulatmorphologicaloperations, arereally a studyofhowthingslookspeciallyintheimage.
Sothisallowsmetofill a secondaryimagewithjustthespaceoccupiedbysingleshapeinbinaryspaceoftheinputimageonthisis a greatwayfordoingimagesegmentation, theextractionofobjectsorlabelingimageparts.
Infact, I findthistobereallyinteresting, and I thinkit's worthyof a videoinitsownright.
So I'm lookingatallofthevaluesofmyneighborsonalloftheonesthathaveone.
I'm swimmingtogetherNow.
I knowthatinmyactivityframe, they'regoingtobeonesorzeroes.
Anyway, we'vefinalizedtheimage, so I'm justgoingtoaddthem.
I'm goingtochecktoseeifmyactivityisoneandifitis, butnotallofmyneighborsaresettoone, then I'm goingtosetmyselftozero, putmyselfoutthis 3rd 1 which I havenotdrawnup.
So I'm goingtodosomethingreallyhorrendousandcreativevectorofthefloatingpointvalues, whichrepresentsallofthepixelssurroundingthepixeloncurrentlyinvestigatingoverthisfivebyfiveareaon.
All I'm doingisextractingthosepixelsinmyneighborhoodonpushingthemintothevector.
Once I'vegot 25 pixelsinmyfactor, I thensortsthemusingStandardSort.
Since I'vegot 25 pixels, theoneinthemiddle, itwillbeatthe 12thlocationoffmyvector.
Workingunderthesameprincipleasthemedianfilter, wemakeanassumptionthat, overall, for a smallregionofimage, there's notgoingtobe a greatdealofspatialvariance.
Soit's bettertochoose a freshholdvaluebasedontheinformationinyourlocality, oratleastbiastowards a thresholdvaluefoundfromtheinformationofyourneighbors.
So I know, forexample, inthisregionoftheimage, if I taketheaveragevalueofmyneighborhood, thenthingsthatarestatisticallyinteresting, maybe a certainlevelabovethataverage, andthataveragemightbedifferentfordifferentpartsoftheimage.
Whichmeansif I used a globalfreshhold, thatspecialvaluemaynotbeappropriatefordifferentregionsoftheimage.
Itmightnotbeimmediatelyobviouswhatlocallyadaptedfreshholdingbuysyoucomparedto a globalfreshhold, but I finditreallyusefulwhenyougotchangeinlieueminenceacrossyourscene.