OQ poe 3a She ene. Ofsu thresheldiny
C :
Cc
C
C
«
«
@
@
@
C
C
bbw, C2b3 Se Pore r ne
titles = L’Oniginal Noisy lage’, Wistgoon Global Threghelding Grong
Dees! ‘Gaussian Filtered Image’, Histegram' “Otsu's Aresloly:
fen epmnpla 2nd complefe %
Foci ia xvange (3+
plt. subplot C3,3, 7% 341), plb. imshory Gmazes £243], “sray’)
Neston ple. bible itl L2*3)), ple. xteicks CLI) , pl yticks CLI)
subplot (5,3, (#342), plt-bist Gnages£i*33.ravel (), 256)
plb. bebe ChitlesEi*3 14)) ; plt-xbrchs £3), pit. yticks (£1),
plt. subplot C3,3, 1% 343), ple -imshors Cimages [é°342], ' fray!)
plt-title Citlesli*3+ 2), pit. xtielss (£1) pli, gdicbs C01)
plt-show O :
Otsu's binavizahion vvork ower tha bis les
Re hishigran ds hee porkans he dileeces Yoo ater Inages
Biles L'0riginet Noisy Meage Hs eg ran’, ‘Global hres holding Cy: 122)
| ‘Original Netty Image’, Histogram’, “Obs ubMresheldiins”
© ‘Gaussian Bilbced Inage! ‘WShoner,, "Obou's Dyveshatl |
| en te Prego—————
leet Noisy Histegraws Qleoad Tees) di D
aim
riginel Noisy Image Hishey raw Obsu's Therheoldrng
5
ausginn Filltced Inez __Hishegcon Obs Bresheli,
= rm)
|
tus Bresholding 13 Far prove belter thaw nena] ae
t Hefocsise we Rk Dredelding Nnlae. sisting He hey
O
P78
00000000000000000000000000000000000O90
a
Oe
O°
oO
Oo
oO
oO
Oo
Oo
O°
Oo
Oo
Oo
C
Oo
°
C
oO
oO
C
oO
0
O
@
O
oO
é
Open Ned OGject Defect
\-Tr:°':''_ +>
license Free
© Mosecrtan bing Open Cu libenrs —
SWe First have te instal Open CX library.
7 Cponc COpen Source Compufer Vision Librony) is an epen source
conpulie Vision and machine learning setter library.
Ope was built fo prvide 2 commen inbrashuchwe for Smpuke-
Vision applestions and te accelerate He use of machine percephion
ia Commercial products . A
He libracy hes wert han 2500 ophirized algoriburs, Which neha
a comprelensive set of bok classi anh shaft Phe ac? ccupubi
“i and moching fenening aljoritims .
Applicebions of nc
ate defect ud recognize faces
Identi#y objects
() Pckssify human ackons in videos
Strack camera movenonts
> tencle mowing objects
erhock 3D msdels of objects
> follow ex movenuts
Drevegnize Scenery and eshablish mackees fe overlay it wit
augonted reality, ofc
| N Functions Growre of te)
> ineead(): It leads an image from the specified file and réluns it. Achuoly,
it reaks animaze as an array of RGR values é
ides Gophue £9: Class Far video Capturing from vides Piles image sepuenas cot
Ni desCophare- open 6)? Opan Vidleo File or a coplicing bvice for video capburing
PNidee Captioce eleese Closes video file vv caghicing dewicg
NideseterC: Gefe object bo ovrite video Piles. Bales sawny any vdeo File
PNideoWeifer-woeile): Weifes Fhe vox xideo Frame
Visual cufent con be prodeled ag a Witrarchy =F abstraction:
MEAS rst level ove Pe raw pixels wilh color or brightness inforey fen.
> fuchePpcersing yields featives such as edges, corners, Lines, curves, o
ond goler vegions _
=p lute bebonchion Inger mas Conbing and interpret Bese feabes os
objects and Keir alibules
ae ig hest kevel ore Hie human level concepts luvoluing one vo more
obfects ond velofionsbip s aveens tren.
Te Select amy ideo, you wast” Frow Ke inner pact or inate levels ov
mnec abstractions .
ee0000000
Oo000000
| »)
> Object Lelechion in videar Inuslves weeifying “he presence Fan object ©
in image sequences od possibly lecaling it “preiily for vegogritien. ¢
These mage Sequences will have obveof which is owing So we [eee
fe locale 5 position in exch Prame,of fe video and ten ie can feck —
ib of we can recognize ib .
> Objet braching ts te menifer an objects spakial aud temporal changes
dering a video sequence, cathe including. its prereset SRS
Ky including these
| Fiahees, we can frack
any object.
|
—
oO
E
C
DOC0O000000000®
© OOOOH OOOOOOOHOOO
é | —
D | ing Mprowch Using Object Dekctian
Now lel’ see difftrect examples of how we con delet ebjeck i
ory ides of auy image
Feahue—besed Object Defecfion
One op wore features ave extracted ond a objects of ialerest me modded
in ferns of Kese Peahures,
Lis feafioe means chanacleristics of okpect
pe a red cole, shepe ea swe
& ne ove dlefeching en object Losed on fe Pealnes of af object
Sub-Gtegors 7D Shaped- Based defection
ln order te defect ond clefermine Te becder of an object,an image May
need be be preprocessed.
whe peers ies oc filter clepends on FE applicotion . Di flecent-
objec’ 5 such as persons, Flowers, and ainplanes May regu!
O Be ees nial its
Once te object 1s defected ad located, ifs boundary can be found by
edgt defection and boundory-Felleding algeritms
1 ® Gor Cnsed defection
iS simple Tracking melked is Lased on tracking vegions of similar
os
5 cole jhe fore te frome . fi
Each region is characherized by a color vector computed by sub-sampls
te pels yakin tre vegion, Which “represents averaged Cel ye of pixels
vikin fies region . ta our Object -
Lowell subserplt Ke pixels present
_Subo-Cabesoes :® Temmplale -based Object Defechon
Fa pomp describing a spechc objec is available, object defechen
(anes a press of Rakching fealiores Gehween tke perp lett ond
ingle SEQUEL Um dey “analysis,
| LataPng a Femplote sinc lee be ont objet
PTOused when abject shape ale vot change
rf
ee
a, veyect fr wiewsind angle. a
D) Fixed Tenplafe Matching
Fixed Teuplotes ave useful when object shapes cd wh change wit
respect te The Viewing angle oF the Camera. we tajer technigues
hote Leen used in Fix template mafeling.
—elnage Subjiaction? n ths echuigue, Ge template position is
letermined From minimizing The distance function betyeen The
| template and Various positions jm Te image -
— Geet 3 Matching he ances og sbzes ition of te
wormalged ore ss - Correlation ween a fe and an imra,
EIS bad nach * 3e
De feraolale Tenplafe Matebing
SB peformable template matching appre aches oe nore suibeble Foy
leases whore ebjects Wy deeb rigid and nonrigid chforafions,
Were variabions “can be caused & citer Hh. cbfommaion of Fe
eat pore or Just by Wlerent bpd pose relative KAZ camera.
Pla Bas apprach) a teup he 1 represented as a biltiap cksce tng Fe
dare cleristi contour ledges of an abject Shape. Pi prebabslistic
ae nthe fot toe conhur is yphtd Jo abhi Fe toap/de
To Bit saliert e iw leput jnaa
ak wbjectiae Oe, 5 eorteciged Heratnaely spdobng te
fronshoematien poramelecs to best match te object.
object shape i changing
| Lisit i defrmagle -
_Lesentny Coyfiating ideo Free Canery
When dealing Wik Vidor Be fiat banc fimefion 1% to cphive
ides Fron camcra or ge. many fi Let é
OTe cyt 0 ides, a Video Gphice “abject is weded- 1 argument con
elBer te WA inder or the yore of a ~ete Fue.
ania dex is Just Jee numbur to specify which camera,
= Is, ove comet. will be Connected - £
DCOCOODVDOVDOVDVODVODOODDVONDDNONDNVNDVDOOGCNN0N000oO you we ie
Oo | ake, comers PE .
°° | So peer Lapher
e -
C > 0 simpy pars O Gr ~i)
oO Tew om select che second canern by passing Lodse on
O pete Bot, yeu com caphee Frone-by fauce
7 Biller eapheing the wide, 7 ye ts wend vr of
oO You con vers Use See infernal Yer N Fpawas. 5 Vk
7 funchions on te object . ca cap bor Frane Ly
© (> LapisOpencdl) >To chocle wheber — OF ing Video phe
= it ts jnidielized oe not bythe mothed «1, wit
° ot poyen() 2 Te open camera "SG hirgtrlenn
Oe Cap.get Gpropld) > To access Fealiwes of video woe propld ix «
OQ number from © to 8. Each number denstes a property of Hy video.
O > apset Cormgld, value) To medify, ‘value js Bee alue you wand
o f= a
© \O/ Letoaes pre 1 beperty 1D
Oo ~ es can wodify any paperhy of He vider upg set finclion
a Cap Weans Video caphure object
© ‘aptwing Nideo from Fle
: Nlidee files ow be Mp3 by cher hype
©
OC SEF came 05 capheing Prom Cancra, just change Camera index
Oo Wik Sidee Fle name ~ Initials we parsed © bubnovs video File none
@ > Aloe Wale displaying The Feome, use appropriate time for gv2- wait key J
oO > rt 15 tro less ~Videe will Le vey fast and if it too high,
© Nideo will & slow oo
© ~~ :
| ts func)! on
C | spor ee Pe ess ieee
een dt PS for disploying
oe wet 3 Frae|
|
| -
Lets see can example Por caphuing vides from Camara
Lingort numpy os np \ lepert Open CV mada Ce
Linpet cv J Wmper} nunpy todulp
| Cap = cv 2. NideoGplue (6) < bate ig
| UL copfae Nidew file fon Camera
ae pie a E
| Caphure Frame -by~ Frame ey perce
cel, Prerc = caporend C) rged Fanehion & Ber cophatng
4 Dee operations on Te Krave come
graye NZ ENEG bor Frame, cv 2- CoteRASRA 26RBY)
WH Dieplay tke vesulbing Frame D Gomterts yor France
292 Lule Cifrwee ehee fe BSrey scale
7 if cod waitkey Ci) § O¥FF = = ord (12°) . l
LyeaF Opency
Fs tke seshing dene, veleose fe cagfice by ab fault
Cap. Se. 2 will give yo
cd destroy Bly indewss C) BGR pede!
one just
aki Fe Fee cisphying ie
Fron wing for Nid pryperly
ingles furchow Closing
| our
| Camera
2 fe ran Be code, ewe Camera will pen od shot recording.
AF Video sail Le gray cole.
| Gh else it
press
Pinchow +R oO
Cra) +18
rye ab fe capte weidto From file ,
oct fle PRE oP On” Nietlaphuve (file ne 14)
oo
OCODDD0000000000
Oo
OOOCDDDOOOOQ0CO00000° —hesening fo Save Video
After applying, any algorBe on your video, you west hace fe save
wide SB Sontinng Video
> Niderifer objecF
RR Output File nome
Fou l€0 co
Nunber of Frames per second Ctps) od frame Size.
2H isGlor Flog If it is Truk, encoder expect color Frame,
ohenoise it vwecks wilt grayscale Frome . eeepc
NideeWIerer gives you Your Oubpat Fileyawe and Ken ed
thave fe inpuk yer FourCe code then fps, frome spe
nectally if would be alee frame if if ts fue
> Four Co fs a 4-byte code te speciky te video codec
@® he list of aveileble codes can ean Jonrce. xg
elk is platform dependent. Now leis see an example :
Jugert aumpy as np
. Tree are
Orgect v2 wulfirle codec
Twperk cv2-ew ad you
ou
cap = V2 Nideclaptuce Co) an
BDebive The Codec ond creele VideoWciter object any
fource = Cv2-cv- CV FouRce CB Kia") AIRE Frecce cole XID
“out = ev2-Videoleitee C‘cutputd avi", fource, 20.0, C640, uge))
bak. ap uibile Coop-is Opened C)):
A ageo Famer
cil ek, enene = emp -c00h 0) aun MP slape and
a] if wtes Tene SO ied Fe rn Tiros he \ding
vorpal hraree < cy2 File Grane, 0)
Dub ori ke Chee) oy foslins ke video frm corora in te odput veriable
EQ esIoo ['Frama!, Frew
IF cy2-poaitkey(i) $ OXFF cz ord LB’)!
= brea.
se:
ponanetitihg ifanb oy Piniaed
og HP uve is Pini .
Wek ele ce Oe, let let
Ae REE Qn oinbessTT
|
|
|
| °
“shite yeu click ven, or :
Ake Camera of you sysben @ laphop wil twa on Sides ing
jie colwred Wie.
[Frau -a od
-~ | Prevs Funchon # 6
video | te cee.
|
——————— }
the ani file would & sewed in R ctr divechyy .
| Inewing Bet, Selection
Shaferualy, a bbl, iy a repion of an image in which some properties
ort congtant oc agprevivately tonshint ; al te points Ih a Mok cn)
bel considered in some sense fe & similar tb each cher.
IF a pecson iz walking in a shreel; Kab person umuld be considered a bhd
alt a group of comedted pixels in an image Rat share some
Comteon, peoperty (es grayscale value)
> Openly provides a comeniont, way be defect bhbs and Filler Hem based
on Pret characeristics. .
Lilt pevides Re simple clars wus name is Simple Blt Deteckr
CODDDDOCOCNDNDNDOVNOONDONOO0N00000
Some Jmpectent Panchions ! Gis preset jn
g ings Convert The source images to sevecel OpenlV tool For
Linney reaper by hresholding Te seurce image is
veshelds Sinery image is Kah ivrage which is eifier while erGleck; Oor A OC
1 Iw each Lnory image , Connected white pivels are grouped Teg elec. O
) these binocy Licks.
reas re of He binary blobs in Te Linney huages art eorpube, -
land Ulebs leceted closer than ein DistbetweenBlels are merged, ( )
Glee K fadius Gleulation + Te centers ans endiy of fe new woeged
blobs are compute ond refurned.
AE rr ree multige bibs, we will fink #2 cae of each blob. PF te Wistance,
loetwees Habe 1 les Bags uit em sralense GtToden blebs, Ren ne wil bist ext Bits
We “nein MARAE,
va
Oo0000Hy > Now let's see an eearyp le’
ByGlor? Figst yeu veed to set €:lterByGleg = |
Set blebGlor= © te select darker kiabse .
and bhbelr = BR 255 for lightebhbs .
a is for detecting bib by Che.
By Sige! You cam Filler Be Clits based on size by selling the poruméles
filterByAvea =A, and apprepriale values for winAea and wax Aree
Panay sie Mm porton Ben you amas nae Cuan Ae
lrogine you
By Shape
TRdeeculority? Tis jor} wenswes haw close bo circle te Ubb i.
w]e GiVfee by cicculcily, set FibterByGrculwity =1. Ken set agprepciake
values for winGrouleit ma wax Grculerity .
> Gonsexity? Coven is defined as the Brea of the Ble Hren of its cmvex
hull). Now, Gover Hull sf a shape isthe tighlest conven shape that complelely
: encses Fe shape «Jo Filter by coonexity, et FiltecByGnuexity <1,
/ Followed Ly seling OL min Covtely 6 4 and maxConyexity C22)
Loe
ectia Rabie: Tig measuts how elongated a shape is ey Pera dle
BG value rs 4, for an ellipse it i between O and 1°, and for
a Lue ibis ©. To filjer by mertia ratio, set filkerByhechia 21,
and Geb OF minlerbiahaho AS and waxlnediakdio SS aprropriakely.YANDNDNINDI
Cc
-
. pn Exanple C de : de C
OTH Sfanderk imports : nt) (
ab) Papert ove owe Swot Salt 5
Set | import numpy as np; | cae 4e Ce ogighe es
Bherd nage ewe ~
ime ev2 inread C*blob* jpg cv. IMGEAD_GRAYSCALE )
#Seb up He Wik default parameters.
dehecloo= Gye: Suple Bll Detector OC) dostgelling FE detedor
detect Lobs —— Fimchion fee gebhng cletector
Keypoints 2 cetector. deledt Cin) We vse debeck fncdion to detect Llope
HBcaw deleted blobs as ced circles. > dcawing Keypunts as red circles
4H cv2-DRAW_ MATCHES. FLAGS _DRAWRICH FEYPOINTS Cntwes FR rize of,
the circle corresponds to Ke size of bbb i we C
imi Kegpoiats = Cy 2+ ddan Keypoints Cin, Feypeints, np. array LI), C2,8, 288) ©
fas Cy 2D) MATCHES FLA GF DRA RICH
Wee cee set Pungithy ha dean hom blebs ~ FEY POINTS )
wa Keypoints
cy2- shies Ciheypoints Tasegtht Fey peints) Pte sha our image
(N2- ait ey (2) AR aed
means Mb wh: pos awelers
O) je et ie seh AO Reba lt-
But we com set poratrefers like Inertia
vatio, droslecity, yt, Glov, conven ty
fy-°
OO000000008@
to
Oo
oO
©GO0O
QO
O°
°.
Oo
Oo
Oo
oO
@
ww
wer get to agerting Re widen
hohe casing tre vider
Simple Blk Dekehee Reject) _
impork ew?
pete Cte ae import his
impor’ mumpy os ap ee
impoct imageio input outpub of image You can use
vide imageio. get render (Sampled mp! ‘Pempeg’) Tyieeeig, artes
“F foucce, 2 evZ-cy UACC C “iV FERS your idee File.
“Hilo = cv? Videobeiter Clhgsub-mp) fource, 30, (120, |200))
(oz image io. get_weiter Chgsub-me 4” is 1% for Gouing
Final 2 imagere-geEvarllie Conant) NE Yael 9
Fgbg = cv2- Background Subhractor MOG C)
count = ©
for img in Vid:
Fomask = Fgha- apply Ling)
Faras N2- medianBluc Cfynask 7)
Fgmask = V2 -GavssianBlur fgmask (3,3), 0)
| Masl€= cx 2- erode CHfgmask, Nove, iterations =10, bordkeType=1 )
mask = cy2- dilate Cmask Nowe, iterabions 219, LocderType =4)
0- append_ data Cras k)
H set up te Single Blabcletector witk cefaulk pacarelers.
prem Single BleL Detecks Params () ;
2 Change thresrelds a, wrled
parares ialbvesheld =o fon
parats: maxDresheld = 256;
# Filter by Area, |
paracs Fi Be Byftrea = True an
acamns. minften = JoToe - ‘
oe wat Arg =!5000 - 7 Cae false
# Biller Ly Greulacity
params. FilterByGreulecity =True Wis #t 95 True fecouse we
parcins. min Greulacity = 2-1 hawt be change ts deFault
H Filler by Cnvexity sale
params PilterBs Conrerity = Jone
| params. minConveriby So $F PTO| O
| —
LH Filter by Inertia
parans.filterBy Inerbia = True
params: minhnertia habe 20-5 ;
params. min Dist Bebween blebs = 150-0, ace bow POND
cletechoc = eN2- Simple BbDetector Cparams) YE SE puree 7
Detect Mets. imag Keogh de SP
“reversemask = 25S ~ mask J flee peter
\Feypoinds = detects t Geversemask) defeet YOO
laine Jee Cheypoints)) ai ae wactable O55 = ieee :
Lim wik_ Feypomts = €v2 clrawHeypoints Cuask Heypeints, np. array (LI), & ors
i ere Ban soe fuse lube detected
# Show beypeints ising dranFeyporate
GHZ -inshows C“beypoints” inn wi Feypeints)
oe Cin swith Feypeints) @
# print Feypomt Here we owe passing a
oe Cheygeints) iso): Nideo Ket will fms iB ileke
|
t
ip Chew
| Count =eourt + len (Leyprints)
IF Cov. waitkey (29) | 2-4)»
break
| Kis is Por displaying tre
nid - close) Mlle properly -
10. close C)
Final -close ©
| print Count
|ey2. deskeyAlltyindows C)
priat (Bone!)
> Now run te code. @
| Lsng wideo rill tun, you con gee bleds
| im which pergons ove Werr'ng .
|
|
OOCOCOODODNDODOOOCOCOCOCVODOOGO0G0O0000~
Tracking Using lor Spmces
a hae Tent lifer F $ colow wedels alvendy, Hs, HsV, RGB
So how Cam We ee using hese wodels”
5 ast a oniring, vides, and ging
Get a singe frawe of the video dee Feed fact oe Aigo "hood Free
Cow? 5 Col-v mode! From ROB which Oo? uses te veprem
meses by defult be HSS sre re cn OR book at jut &
gl value te Aetemine Me Hue .
— Apply a threshold to eliminate Get heic value &0) all pixels
hak “dewt meet ove criteria
TBs will leove only the object we aim to track se then te detrmns
its location we can get OpencY be calculate ifs momente and
\compute is cn-ardinal. position.
oO | i eeetles toe Teg bing sing Color Spaces
& nt eve ia OpenX and nuspy > Fe eaherelicl
AEB kya inet money as np
WE Eine a Rnckon toe ke excrete frame from Ka, wlan
bea
Pst t Frame Coop, sealing fader) Shares SE SS
Rad “he camert a cone oa Vilen caplure object
afc — Frome = cap read C)
cacek Ht Resize Ke image
eave Frame = ev 2-resize. Frame, None, f= Scaling factor Fy=3caling tack
interpolation = C2. INTER. Anca) First step
W comect fhe iwage to SV colorspace (oéter sthhing
Wenge
hyve cu2-cut Glee Grome, cv2- Colon B6R 2HSV) | conurh ts
HDeFine ange of skin color in_ HSV ore
| Jover = np-arcay LL 0,70, 66])
upper = 4p. acray (L$0, 150, 2sS aD)
HT hesheld He FSV image be get only skin color be el
| susk= 02 ahs Oy, eee, upp) Tod Roe Faschon
HB itwise~AND Lefineen Be mask and original image
ing. Lifwise_and = Webi twise_and Brame, frame mask= mask)
H hun median blucring hrs is For gelling cur merge clear
ing median blued = ev2- ethan Blan Co Dijtune ant 2)
A Display he input ond oyu ‘ ees
CXR -imshous C'lnput' Pease) ee Sypleging »)
Cyr-Imshows C' Output’ Img median bluyred) inpul and city.
# Check if the user hit tHe ‘Ese! bey
C= evZ-waitkey (5)
| if.c- 223:
| break
“H Close all be twindous.
| 4 2-desfroyfll Windows s C)
—eNewo ron te bede.
)
DOVDDODOVOVDQGDODDOOOCOOCOVCVVGOOO0G000000©9008 OOHHHHHHDHOHOHOHOO®D
©0680 0@
@e@
eae We ee bro in or at Ag any teegeye cae
articular image, we Picsk gureoh Hose junges For clear tracking
y tee eet ” —
Melheds For Smocking Inasos
Averaging = This is dong by convelving e imaye wih a normalized
boy Flber Ik simply takes he overage of all te pixels uncke Fone! area
and replaces Ke central eloment wih tres eieraye .
Tis is dene by Ke fine tion *
| eve-blar() oe C42. boxFillee () |
Gaussi beeing
Tn Res aappronch, insPead of a bex Filter consisting of egunl filéer
coefficients, a Gaussian Kewell rs used.
2 -GausstanBlur () for smocking He image
CD Gaussian filtering is highly ect ne in removing Gaussian
porse From Ke image.
IF you want, you can create a Gaussian Fernel with Tay Punebiien,
ext gel Gaussian Kernel C)
SIE conputes Ke pedion ofall The pixels under Be Femel windows and
the central pixel & replaced vik Kis wodien valie.
> Ove interesting hing bo wete ts Bat, in KE Gaussian 8 bre fillers
ha Filtered value For The central element con avelue which tay ast
_ eHisE Inte origin image. However this is wal Ke case in median
< filtering, since He centred elment 17 ahoays replaced hy sone pixel
alu ia The image
This veduces Re weise effectively It ta more ebective way,
_ v2-medianBur C)
—+ You hae fo ust bese jurcohying
weheds bebore applying any
algers tm|
|
|
|
‘unglien: Coding for Smoothing mages @
tpt cuz
import nompy as vp
form wralpictlib impock pyplet os ple ;
jing > ev2-impead C'F *\\ MLN images\ Vet. We reading Image
blur= cv2- blur Ging, (55)) avenge mehed SSK shoe
Gaussian? Cyr Gaussian Glue Ling, (5.5), 0)
median = CVZ- medianklue Cing, 5)
medrans—Cu2smedian Blue GugrD— use t once
plt- subplot C120), plt-imshow Cng) , ple. bitle Coriginal’)
ple -xticks (LI), plt-ytiets CEI)
plt-subplot C122), ple inshew Chloe), pit. ditle C'B luered’)
pl -wticks (LI), plé-yticks (£3)
plt. show C)
ple- subplot C122), plt.inshers Gaussian), plé. title Cgansrcan't) (>)
plt- show C) this is fo slors the Subpot
| ple subplot £122), ple. ‘mshors Gredian), pli-bitle Cwedian’)
| pit -shevol)
fun tee ede
Original Blurred
DODVDODNDDVNDDDONDNDONDVC OOOO OVOGOG00000000@OG OOOO 809099098 OHOOD
©e@e@
O
aD
7 Undersband ng Cafour Detect on
pOoakouss Con be explained suply as a Corte joining olf Be continuous
points Calong ie Lomdag), having same color oe infenoiby.
rit i wed For shape emabns and object dedection ond recegnibien
if we connact Ke
points of its Loundy,
we ore geling its
Weife perkron.
f
lk OpenCV, Finding contours is like finding white object from
black Lacksvound. So object t. be Found should be arcibe bite and
background should be Llack. For geting fat hpe of image, wr use
Oo) Brresolding
—eTreshelding: IF pixel value is greater Man @ thresheld value, if es
assigned me value (may be ubite), else it rs assigned ancbier vale
Goa be Uack) . -
| For Breshelding > ev2-thvesteldC) ; :
eee. ao —> eve-find pare 0 ie Fi ie
recting Those contours yeu are actually’ clefecting objec
Sedalia Boe cots Sere act elt ot
x| CanSlaft Blgori >
[Riats te remember >
> Clee space based tmehing alavs us te track colored objects, but we
hose be define Ke color Fiest
IF xe rae bo fed aw object in a video /. wie can use Mes
alysrifim. In he algorifur, ne dev't reed fe specify any cleus.
ATs i Gasiealy toed be tonck objects. °
—*Cratinuwesg Adaptive Mean Shift,
>the adaption vernon of Be Jean Sli algorihuy
means that first you must Fan
the wean SFE algecihim
eo ——_]
Be tiborial For Mean Shift Algonky is Kee
http ://clocs. opency-org/3-1-0 [dk/3 8/tutorinl py taonashif eh tind
Mere. SUPE Aloo Poon .
5 es a . 5
- . ‘ rr x \ : ‘ ry
¢ : /, : . '
, | sete Cet \ «
fo. 2 a ete . ‘ f
—— ae . a7
eC lL_—rC_—llC
SHH @ wind oyor How prints suc phat canter of these paints Ms tous)
te Lerher of thot winkew
We bok baltin hove circle shaped window. -
Pe Ke jer of Bat window or circle sheuld Se porged wik Te
Lerter of Kose points . ,
AAhe confer of the blue civde 1s Cho Luk centroid i Cie. $ your
Cantears v4 wot watching wik the center oft ciccle. at's why ye have
Gsflel ovr winks Poe Aron Ciecle Be colar Gcedheal are matching
QDODONDDOOOVOCONDDOOOCNONDONVVCVGOGO0OG000000©0000 00900000000909900098
6000088 CO®O
eRERA
@
2 Se we woemally pass the lishegram Lackprjedted image ard imitial
teget Ce sbjecl wos, Peroealy Be pocemed
reflected jn hishegeau bockpesjected imag? Ds a vesull meonshipt
algoribu, moves our windeww E he new Siow wil prowinauey
density. TE, iteration will confinue until Ke center of Be windon
atdes Ke catecid .
> Ove winds ahuase hase sane sige when car is farther away
ond ibis wexy close ty Camera
Liye eed fo adapt Ke window size wik sige and rotation of
rhe edaphive camshift algoribua applies memsliPt first. Dnee
) raeanshiFt comege sit updates The size, of He window.
—> I alse calculates Ke orientation of best fifin ellipse fo it.
Again apples Ke mouslsfl wi new seeded sevch. window
and previous Window Jcation. Le process is confinwed yah
required accuracy is me
nstialzing “jhe Video Cophure Object
import cy
import numpy as np
BDeFing & class to haredle object tracking wghled Funct malty
jolass Qbjedt Trac tee Cok Shh sre loss
a def iniF— Cself, scaling fades &5)
Vids \| Ht Dnhalige Be Flex caphice object
we selF cag = v2NidecGyhut LD 7 Emport video
be B Geyhuce he Frame From te weborn
= SRP. Frame =P. cap-read b) > captive frame Frou webcam
Sol H Scaling fader for the caphied rare
aw se)F. scaling acfer = scaling fachr
HResge the Frame
PheTis [BH Check iF tke user has stufed selecting TR vezion
Gas If self. drag _shact +
i if Flags KR owz. EVENT _FLAG_LBUTToN :
fo # Figs Exleact the dimension of he frame
yw h, wo = selP. fnaee « shape [2 23
@
Self. Feane = cv2- resize Cell. frame, Nene, Fx=self-sealing. factor
| ; Fy self. sealing fag inbecpolation = ex2. INTER AKER)
# Geale a window bo diylay the Frome
advonedebedacs COGCE Hacked ewe
ASet the mouse eallhack Funchion to track Ke mouse
ex2-gcbMeuseCallback (“Object Tracker self mouse. event )
Hbitiablge Noriable related fe rectangular region selech'ed
self. seleckion = None
HIntialze Variable related to vestrygutor sfochisg posihien
“self. drag —slact = None
[Bnitinlige sorindle related fe te shale of tracking.
sell-tracking shake = ©
#BDeFine a meted te tinck Fa muse events
(ae mouse event Cself, event, %, 3 7 Flags, pero) :
# Comert x ands) coordinates inte M-kiE nunpy iadepers
| x ye mp. int lO (Lx, sD
F Check iP a mouse bullon clown event has ocautred
IF euant == cr2. EVENT_LBUTIONQOWN ¢
Self deag_ start > C9) MFwe played en dey will slact
self-tracking stale = 0 and sabe Lecomes zero
# Get te mibial position
Xi, 36 = selP dong shart
Hak te moe and mia valves -
XO, 90> pp. maximum (O, np. minimum Coch, yt] &xsIDO
X21, 94 = np miniwum (Coy, hI, np maxima ¥4,9 4), Lx, 9) ~
# Reet He stledion vaniable t
self. gledion = None
U
QOVDKDODCNDDVDDODVNONDVVOOVOVVOGO0000000000)}@
(Finalize Ke redungular selechon
| iF AL-KO> © ans YL-JO D0 %
| Self. selection = (x0, Jo, x4, yn)
eke
HE tre selection & dene , shack tencling
self. Seng start > Nowe
iP self-selection % nol Neng t
self. Epacking shate=4
HMekod be stark tracking Ke object
ef shart tracks CselP )
H Iteede untildte user presses te bse
=7 HIF. Frane = IF. cap. read C)
4 Resize the input Frame
© | «lt feet: resize CselP. frame, None,
| Fue sell. sealing facker, Fy= elf. scaling_facker,
_ . | invecpolaken > cw2- INTER AREA )
Tis | (HGeate a copy of Ke trame
fet vis = self. frame- copy C) os
she | A Gmvert tke Frane te HSW coleespace
Feat hey = cy2- cubbolor Cself. Brame, C¥2- COLOR BGR 25)
"” # Greate the mask based on predefined threshalds
25, \ mask = Cv 2, jahange Chsu, np-arraylC0,, 60., 22.)),
Ted np-array (C18, 258., 258. )))
# Check if Re user has selected Ke region
AATF galtscedion
eid # Extack Ke wordicates of he selected rechingle
alle
XO, 30, x4, 94 = RIF selection
NPE xtoact Re tracking winduo
wed) Self track winders = Cxo, 90, x2, 39)
H Extead te regions of
© hey ipei = hsv [9o:99 Ao xAy
mask noi = mask [50:54 , xo: xt
UO# iatecest in Te HSV image
hist = cv2> caleffist C Crsv-roi J, Co, mask voi,
L163, fo, 1963)
FB Nerwalize and yeskape the bishegran
CNZ- novmalize Chist, hist, 0, 255, ev2-NeaM_ MIN MAY) ¢
sdf-hist = bist - yeshape C-1)
| # Extract fhe vegion of salerest from He frane
Nis-rei? vis Gyo: ya, xo: xt
| # Cougute the inrage negabice Cfor display only)
cve- Vhwise_ not (visor, visret
vis Tmosk==0)> ©
H Check if tke sysfem im ke “Lrecking " node
iF self. Eencking_sfale => 45 oO
H Reset the selection variable -
self celeckon = Nene
|B Goupide the histegeam back pesjection
| hsv backpes} = c¥2-calchack bject Dasw 3; Cod,
| self.nist, Co, 1807, 1)
# Gupte Litwise AND Lelieen histogram
# Lackprajection and the mask
hev_hactpre) ¥ = mask
HDePine Leruination criteria for the tracker
bee crit = Cov2-TERM_CpiTERIA-EPS ) cv2-TéRM CRITERIA
~CeunT 10,2)
|
|
|
| Pewute, the Weshgrar, 3
|
|
|
H Roply CAMShIFt ow 'hey_backpro}’
| trad box, self feack windows = ¥2-CamChitt Chev. backpry,
| gelf. track windevo, term crit
| # Draw on ellipse avamd the object O
etches Giis, track box, (0; 255,06), 2) 01 & ;.t :
A Show the oufpul Dive video Tris ellipse Bunce
| AAR -jmghow C ‘Object Tracker", vis) eet: aang
COOODVDDVDNOVDNDVVVDNOVONN0GN0900000
Co
OCOCO0000t-——
aa iF Re user WQGR. ‘Exc’ bey
= €N2. wok key CF
73 c=rz 7 « Oo
break
# Close all FR windows
1 name == ‘mein’:
# Stack the trackee
Object Tracker (). shact_ tracking 6)
hun Fe cde
Wer you run tke code, he webcam shows TR video of
3° @
Ike object Cperren) Pacing He cepara. You can Use your
mouse te draw an ellipse around yor Face. then velaace Ie
Ueld onthe mouse: Now as you mene, the ellipse will algo woce
rT ] [ ~ / f OO /
/ / |
Lica) Flow Algorikm — CLE PlgoriAu.)
Optical Flew ie Tre ppalfecn oF appace:
befareen bie consecedive frames caused ly the move
or Gamora,
L
weotion 97Te arpe 1F deneting
R sl thing fh am eb fect
From position 4 Brough fe
: Feanes.
FP Yfou how to track it's bcation in all Fe Cranes.
PIs can be done very your ophical fips alzeri Aun,
— Optical flaw evil! show you Ke woh'on of your object iy
conse cubive fyrawes
}
L Le Opticol Flas in CpenC¥ LE mens Lucas Kanade a!gerifinn
ee ite be Fist frnea , deleck Gone. soyaee~ se in it, Ken we
itersfively track hose points using Lk optical Flevs ry,
> for owe prints Sh good FealivesTe Track) leech 23
> For Optical Fino? v2. cale OpticalFlow PyrLk C) bE
@ Prguments - Ke previons Frare, previous points, and next Frame
®D Pet value — next points and slafus [number which is A i F- next
point is Found, else zero.
We ifecatively pass these next punts las prewious points im next shy.
To read move about prea
IAs algeria, 9° to: eal
lie: Mesemeb- wesd edu /classes Ap 0 2/tse?52/]ucas Fanad S)- pat
position & in S conzecuhive
(aed. objec
¢
q
¢
¢
Cc
OOO
COoOoOot
ooooc¢} © @ © © @ © © © © @ @ © OOO © GO OHHOOHHHHHOHOHOO!
O
@
poss
—hresrams of Orhical Flow Algerie
Gwpect ov ) importing Wedules
import numpy as np
F Debine a Fandiion te track the object
ckf ae O:
H Initialige tRe video captive obje
Cap = CN 2Video Capture Coy Capture tha video Frou system
H Define the sealing fachor foc Ke frames
scaling faclee 20'S — Frare will Le resized using His sealing facie
# Number of Frames te brack
num fremes_to_track = 5 the number 5 1s jusf choosen ** “referee
# Shipping Packer fo aren
num Frames— jump = 2 (jocBen
hitial’ze varrables
racking. palts = £9
Frame index = ©
B Define tracking paramelens
cacking. params = dict GoinSize = CAL, 92), max level = 2
ccilecra= Covt-Ténm Critenuers ) 8% JERM Celie
- CBUNT, Jo, 0-03
# Iterate until the user hits the ‘Ee’ key
While True
# Caphure the current Frame
a, Frame = cap. read C)
# Resize the frane
Frame = v2 .yesize (Frame, None, Fx = Sealing. Fachor
fy> scaling facteo interpolation = cv2 INTER AREA)
vet/ # Gouvect te grayscale
bo
frame — gray = CyZ-cut Glor Frame, ev 2-CoLok £5426RA 7)
HP Grable % copy of he Frane
oulput ing = Frame. copy) '
iF len Ceacking_ path > °
A Geb images| 4 whol) a oc0
veh ayer er PeniiowS
| Var 3K wpe ts watt Seat Oo
Brun Goset Fug [fm :
preveing, current ing < prey—gray, Frawe— gray
Piereceties eater :
Fealuce_ points _O = np-Float 82 (Ltpl-1J Fac tp in \ teaching pobsJ.c
oo reshape (1,9, 2) 5
H Compute optical Flas > tren apply Oplical Flew algeribin
Feafwe_ points_4, -,— = x2-cale Optical Flow RyrLk C preying,
| aurert ing, Feature points 0, Nore, inching
| = params)
# Coupite reverse optical Flow ;
Feahure _poiats~ O_rev, —,— = €¥2- Cale Optical levohy rt Courrent. ing,
prev_ing, Fenlurepeiats 1, Nowe, toackiag
: = params
Ht Compide the ti ference belineen Forard and
|
|
|
|
| Ht reveese opbical Flow
|
|
|
|
|
|
00
CODD000000000
@
AA featuee_ points = abs CFeatucepoints0-\ Feabie_ poiats 0.
rey): reshape El, %)- max Ce)
H Extract the geod points if ke difference 15 gmaller Bhan | hen
good _priads = diff feature points <1 Beg ect goed
H hitialize variable nan
new-tracking_ pas = LI
# Iberate Trough all Ke geod Featwe points
| for bp, Cx,3), goed points flag in zip Céracking- pats,
| Peake =points. reshape C-4,2) , goed. points):
| Hp He flag ts net true, then continue
| IF not geod_poiats - Flag t
Continue
|. F Myperd the K and Y coordinates and check iF
jf Hits Leagk greater Ban the Bresheld
\ tp. append (07%, 3)) @
|
|
|
OC00000000
Oo
tf Len tp) > rum frames. tertrack:
del tpLeJ
new tracking _paks- append Lép)
OO000000
Oo
)©@00000000 3
Oo
Oo
COSCOOODDOQGOOOOOO
Q |
#
+
e
8
&
&
noe
ach
ON,
pe
# Draw aq circle around the Feotiye prints
N2.ciecle (output ing , Cx), 3, Co 255, P>, -1)
| ¥ Update the Lracking paths
tracking. pabis = new tracking. pals
# Deaw Lines
ev polylines Coutput ing , Unp.int3z Llp) for tp in\ tracking. pabs
False, Co, 1S0,0))
Geo inte this ‘iP! pondition after skipping tke
vighh yunber oF Frames
IF not Frame index Z num frames jung!
+ es a Mask and ae e cavcles
Mask = np-zeros_ Litt Frame
| Beer 25S te
Foo 2,9 in Tip, int 32 (bp C3) Far bp in tomcking paths 3:
Ox 2- circle Cuesk, Od, Go-)
H Compute good Features te track
Teabuc. peice F ev? -geodFecduces TeTrack (Frame gray, mask = mask
warComers =S500, quality Level = 3,
minDistance = 7, block Size = 2)
its # Check if Feature points exist. IF se, aypend them
B te the tracking palks
Re grees jf Featue points 1 mat Neng?
points
see For 9 in np. float 32 ( Fealurt points). reshage (-(, 2):
ode iS
fun 19
tracking_palts. append (Lb3))
H Updte voriel be
actual frame index. t= 4.
Reve prey gray = Fraregmy aad aysin wt will Itecat Hiough the
u H Display ouput algerubn
@ cyt-imskew (' Optical, Flow’, output_jng)
Pr Check Fhe user hit te ‘Ese! key
= Cd-mait key C2)
ifesz2?:
breakRe gree
your
alse [be wetting oyefher. Ihe Corner prints aye teving because of itera fron
O
ieee ee ee een eee
H Stack lhe tracker
| slact_teacking ()
H Close all Ke windows
eve. destroy All Windows C)
Now vun #e code
os peinks owe Kyat ing
corner points IF object is moving, The comer geints wi)/
had ix applied im oar alyerifirn.
’
>
hee Den Ar TRackiNG
ce Acfection and Face vecognition ove di Feen€.
oF
Steg Ueteckion veeans ne ave decablon the puition of « face in a
Veale Image -We saying uboter image consist of face or uel.
se faa fs fhab i Froe récegnition,
Tis ledue Focuses on force detechion.
>}
re
vb)
ve
ical bromebric system utilizes elk face detection and Face
hom de perPom Bee
LE wes Face detection te locate the face and then uses face (>)
eecagnition bo identity he perrea.
ecbion
pdb t agtection using Hace Featine hosed cascade classifiers iy an ofebive
et wah
COODODOCOODDOOOOOOOODDCDDDDDD0000 OF
<8 proposed by aul Wola ad Michael Jeng in Hees O
it tna pid Dusect Debetiln wing A Beestel Godt +B Single Pestincs” in veel.wv
Heo feative
Eis a machine learning based approach where a cascade funrclie
6 tmivad from a bet “of positive ad negadiWe images -
eee we will werk pik face detection. lnifialy, Ke algoriten needs a
Vat of positive } 5s Uh s of Facs) and negative images
Guages eee ps te Clacsifiew Toon we need be
extract feahwes from '€ The input 5 multiple images to brain fe classy
> For his, haar Feahures ave used. They ave just lie cur convelutonal
Kernel Each feative ts single value obbained by sublracting swe
of pivels under white rectangle From su of pixels under black
veclangl -
We ae Using A mochine [earning algerie se we have
te Eran ke preded wik a porhicolor dafa wich in thr case
Chicas
Hane feohines! @ live Feahices eg eyes
| ® Edge
| D Few-eecangle Rathees
> We hove more fan 6000 Pabyes te check in a winders Kor face
detection and Rat bask is very Line baking- -
DV Se, wow wt will use cascade classifier
SJuat ts we will check feates in sfages
SIF a Window fail in fint stage, Useacd f and so on.
Lott inks fist shege, Be medel Fails fo
eter these Peaches. Ken ih will yo peborn
Be vtwaining shager That's udhy Be name
Cascade classifier
| rhe>
O
~
( +) @
| ©
Cascade Clessifier Inbuilé Funchion _ O
TWpert eve }
import aumpy as np DBs class Kren )
a Iood fhe Haar cascode File ree -
Face_ cascade = CV2- Cascade Clossifier C O°
| ‘heer. cascade Pile hanr casende -Frortal face ckfaulenmt!) ~
A Check if the cascade file has Leen leaded correctly
“if face cascade. enply C) + °
| raise L0Emor CUndle to load the face cascade classifier ©
enl file’ @
¥ Initialize he Video caphure objest oO
Cap = cv2Nideo Capture Co) we ae capticing pur vides Usag camera OQ
B Define fhe scaling Factor @
(Scaling Packer = O-S , , oO
H Iterafe until the user hits te ‘Esc’ key oO
While True t rn 7 Q
— # Gaplice tke current Frame >=
| a, feane= cap.read C) rg grt capri Fane by front foun the =
|B Resize be Frame oa sos :
| Frame= cv2- resize Crue, Nene, fx = Scaling factor, o
| Fy = scaling - Factor, Oo
| _ inberpalabn=cv2-INTER AREA)
| # Govert be grayscale he wider iy gotten as BGK mode! 22 Me levee O
© gray cove ct Color (Foam, our Coron RoRZERNY) IMIA(@
Hhun the Face cetecter on the grayscale image oO
| face reds = face cascade - ete MulScale Cray, 1-3, $) °
| BDran a yedanglk around the face O
| Bee Gag, h) iy Face are cls: O
oe (Frame Cx), CtWYIh) , C0, 255,32) O
| #H Diplay he outpu
| pots eopilh Detector’, Frame) all fee feames will be ie
| Check iP the user hit he ‘Be! ke ay, just Uke
Chae the user se! key le played jus @
@OSS OOOOO SHOE HOH OOH HOH HO HONDO HOOHOOO®D
b= exz. waitFey C1)
ieee aaeo:
break
Relesse Hie video cxphine object
cap. release ()
H Close aN Te windows
eX. destreyfll Windows C)
pete
Ke rechangle will be Hhe leeching yor face
Be end Tthe vider, click on te plus sign0000000090000 000090009090000000900000000