Computer Vision - Lab Manual
Computer Vision - Lab Manual
LABORATORY RECORD
For
SEMESTER: VI
Have successful career as software professional (or) entrepreneur (or) researcher in computer
PEO 1
science and relevant disciplines.
Analyse, design, develop, test and deploy appropriate solutions for real world computing
PEO 2
problems.
PEO 3 Apply software engineering principles at process, project and product levels.
Environment and sustainability: Understand the impact of the professional engineering solutions
PO 7 in societal and environmental contexts, and demonstrate the knowledge of, and need for
sustainable development.
Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms
PO 8 of the engineering practice.
Individual and team work: Function effectively as an individual, and as a member or leader in
PO 9 diverse teams, and in multidisciplinary settings.
Communication: Communicate effectively on complex engineering activities with the
engineering community and with the society at large, such as, being able to comprehend and write
PO 10 effective reports and design documentation, make effective presentations, and give and receive
clear instructions.
Project management and finance: Demonstrate knowledge and understanding of t h e engineering
PO 11 and management principles and apply these to one’s own work, as a member and leader in a team,
to manage projects and in multidisciplinary environments.
Life-long learning: Recognize the need for, and have the preparation and ability to engage in
PO 12 independent and life-long learning in the broadest context of technological change.
PSO 2
PSO 3
lOMoARcPSD|40491420
SYLLABUS
SOFTWARE
OpenCV computer vision Library for OpenCV in Python / PyCharm or C++ / Visual Studio
or equivalent
HARDWARE
Standalone desktops – 30 Nos. (or) Server supporting 30 terminals or more
lOMoARcPSD|40491420
INDEX
SL.
NAME OF THE PROGRAM DATE MARKS SIGNATURE
NO.
1 OpenCV Installation and working with Python
Ex 1
OpenCV Installation and working with Python
Date
Pr cedure
1) T i sta pe CV e ust have Pyth a d PIP prei sta ed their syste T chec
if y ur syste a ready c tai s Pyth g thr ugh the f wi g i structi s! pe the
C a d i e (search f r c d i the Ru dia g ($ R) % w ru the f wi g c a d!
pyth versi
2) If Pyth is a ready i sta ed it wi ge erate a essage with the Pyth versi avai ab e
1
lOMoARcPSD|40491420
Resu t
Thus the I sta ati f pe CV a d w r i g with pyth is successfu y verified
2
lOMoARcPSD|40491420
a) adi g I ages
The steps t 3 ad a d disp ay a i age i pe CV are!
3 Use fu cti wait ey(0) t h d the i age wi d w the scree by the specified u ber
f sec ds ea s ti the user c ses it it wi h d GUI wi d w the scree
C di g
i p rt cv2
i g $ cv2 i read("gee'sf rgee's p g") cv2 I*READ+C, ,R)
cv2 i sh w("i age") i g)
cv2 wait.ey(0)
cv2 destr yA Wi d ws()
,utput I age
b) Cr ppi g I age
3
lOMoARcPSD|40491420
C di g
i p rt cv2
# 5r ws) c u s6
cr p $ i g550 180) 100 3006
,utput I age
c) Resi;i g
Steps f r Resi;i g I age
s urce! I put I age array (Si g e*cha e 8*bit r f ati g*p i t)
dsi2e! Si2e f the utput array
dest! utput array (Si i ar t the di e si s a d type f I put i age array) ; pti a<
fx! Sca e fact r a g the h ri2 ta axis ; pti a <
fy! Sca e fact r a g the vertica axis ; pti a <
i terp ati ! e f the ab ve i terp ati eth ds ; pti a <
C di g
i age $ cv2 i read(r"D <si s<eb<si 21<EB * 06 10 2022 Test ,utput
15<PERF,RATI, <,ver'i <Fai <B ister 1 2022 03 12 12 59 43 859 T0 *0 G0 3
PERF,RATI, * b p") 1)
# adi g the i age
4
lOMoARcPSD|40491420
Tit es $5",rigi a ") "Ha f") "Bigger") "I terp ati earest"6
i ages $5i age) ha f) bigger) stretch+ ear6
c u t$4
f r i i ra ge(c u t)
p t subp t(2) 2) i F 1)
p t tit e(Tit es5i6)
p t i sh w(i ages5i6)
p t sh w()
,utput
d) Thresh di g
Steps f r Thresh di g
cv2 THRESH@BI%ARY! If pixe i te sity is greater tha the set thresh d va ue set t 255
e se set t 0 (b ac )
cv2 THRESH@BI%ARY@I%V! I verted r pp site case f cv2 THRESH@BI%ARY
cv THRESH@TRU%C! If pixe i te sity va ue is greater tha thresh d it is tru cated t the
thresh d The pixe va ues are set t be the sa e as the thresh d A ther va ues re ai the
sa e
cv THRESH@T BER ! Pixe i te sity is set t 0 f r a the pixe s i te sity ess tha the
thresh d va ue
cv THRESH@T BER @I%V! I verted r pp site case f cv2 THRESH@T BER
5
lOMoARcPSD|40491420
C di g
i p rt cv2
i p rt u py as p
I put
6
lOMoARcPSD|40491420
,utput
e) C t ur A a ysis
Steps f r C t ur A a ysis
C vert the i age t a bi ary i age it is a c practice f r the i put i age t be a bi ary
i age (which sh u d be a resu t f a thresh ded i age r edge detecti )
Fi di g the c t urs usi g fi dC t urs() pe CV fu cti
Draw these c t urs a d sh w the i age
C de
i p rt cv2
i p rt atp t ib pyp t as p t
# read the i age
i age $ cv2 i read("thu bs+up+d w 3pg")
# c vert t RGB
i age $ cv2 cvtC r(i age) cv2 C, ,R+BGR2RGB)
# c vert t graysca e
gray $ cv2 cvtC r(i age) cv2 C, ,R+RGB2GRAY)
# create a bi ary thresh ded i age
+) bi ary $ cv2 thresh d(gray) 225) 255) cv2 THRESH+BI ARY+I V)
# sh w it
p t i sh w(bi ary) c ap$"gray")
p t sh w()
# fi d the c t urs fr the thresh ded i age
c t urs) hierarchy $ cv2 fi dC t urs(bi ary) cv2 RETR+TREE)
cv2 CHAI +APPR,X+SI*P E)
# draw a c t urs
i age $ cv2 drawC t urs(i age) c t urs) 1) (0) 255) 0)) 2)
# sh w the i age with the draw c t urs
p t i sh w(i age)
p t sh w()
7
lOMoARcPSD|40491420
I put
,utput
f) B b detecti
C de
i p rt cv2
i p rt u py as p
# ad i age
i age $ cv2 i read('C NNgfgNNi agesNNb bs 3pg') 0)
8
lOMoARcPSD|40491420
# Detect b bs
'eyp i ts $ detect r detect(i age)
# Sh w b bs
cv2 i sh w("Fi teri g Circu ar B bs , y") b bs)
cv2 wait.ey(0)
cv2 destr yA Wi d ws()
,utput
Resu t
Thus the Basic i age pr cessi g tech iques were i p e e ted successfu y
9
lOMoARcPSD|40491420
C de
# I p rt depe de cies
i p rt cv2
# Read I ages
i g $ cv2 i read('sa p e 3pg')
# Disp ay I age
cv2 i sh w(',rigi a I age')i g)
cv2 wait.ey(0)
# Pri t err r essage if i age is u
if i g is e
pri t('C u d t read i age')
# Draw i e i age
i age i e $ i g c py()
Draw the i age fr p i tAt B
p i tA $ (200)80)
p i tB $ (450)80)
cv2 i e(i age i e) p i tA) p i tB) (255) 255) 0)) thic' ess$3) i eType$cv2 I E+AA)
cv2 i sh w('I age i e') i age i e)
cv2 wait.ey(0)
# *a'e a c py f i age
i ageCirc e $ i g c py()
# defi e the ce ter f circ e
circ e+ce ter $ (415)190)
# defi e the radius f the circ e
radius $100
# Draw a circ e usi g the circ e() Fu cti
cv2 circ e(i ageCirc e) circ e+ce ter) radius) (0) 0) 255)) thic' ess$3)
i eType$cv2 I E+AA)
# Disp ay the resu t
cv2 i sh w("I age Circ e")i ageCirc e)
cv2 wait.ey(0)
# a'e a c py f the rigi a i age
i ageFi edCirc e $ i g c py()
# defi e ce ter f the circ e
circ e+ce ter $ (415)190)
# defi e the radius f the circ e
radius $100
# draw the fi ed circ e i put i age
cv2 circ e(i ageFi edCirc e) circ e+ce ter) radius) (255) 0) 0)) thic' ess$ 1)
i eType$cv2 I E+AA)
# disp ay the utput i age
cv2 i sh w('I age with Fi ed Circ e')i ageFi edCirc e)
cv2 wait.ey(0)
# a'e a c py f the rigi a i age
i ageRecta g e $ i g c py()
10
lOMoARcPSD|40491420
11
lOMoARcPSD|40491420
,utput
Resu t
Thus the I age A tati tech iques were i p e e ted successfu y
12
lOMoARcPSD|40491420
a) U dersta di g C r Spaces
C de
i p rt cv2
cv2 i sh w("red") R)
cv2 wait.ey(0)
,utput
b) C r Space C versi
C de
i p rt cv2
# path
path $ r'C <Users<Ad i istrat r<Des't p<gee's p g'
# Readi g a i age i defau t de
13
lOMoARcPSD|40491420
I put I age
,utput I age
C de
# i p rt ,pe cv
i p rt cv2
# i p rt u py
i p rt u py as p
# read a i age usi g i read
i g $ cv2 i read(<'F <<d + awab p g<') 0)
# creati g a Hist gra s Equa i;ati
# f a i age usi g cv2 equa i;eHist()
equ $ cv2 equa i;eHist(i g)
# stac'i g i ages side by side
res $ p hstac'((i g) equ))
# sh w i age i put vs utput
cv2 i sh w(<'i age<') res)
cv2 wait.ey(0)
cv2 destr yA Wi d ws()
14
lOMoARcPSD|40491420
I put I age
,utput I age
d) C v uti
C de
i p rt cv2
i p rt u py as p
i p rt atp t ib pyp t as p t
# Readi g the i age fr the dis' usi g cv2 i read() fu cti
# Sh wi g the rigi a i age usi g atp t ib ibrary fu cti p t i sh w()
i g $ cv2 i read('gee'sf rgee's p g')
p t i sh w(i g)
p t sh w()
# .er e f r b x b ur fi ter
# It is a u ity atrix which is divided by 9
b x+b ur+'er $ p array(550 1111111) 0 1111111) 0 11111116)
50 1111111) 0 1111111) 0 11111116)
50 1111111) 0 1111111) 0 111111166)
# App yi g B x B ur effect
# Usi g the cv2 fi ter2D() fu cti
# src is the s urce f i age(here) i g)
# ddepth is desti ati depth 1 wi ea utput i age wi have sa e depth as i put
i age
# 'er e is used f r specifyi g the 'er e perati (here) b x+b ur+'er)
B x+b ur $ cv2 fi ter2D(src$i g) ddepth$ 1) 'er e $b x+b ur+'er)
# Sh wi g the b x b ur i age usi g atp t ib ibrary fu cti p t i sh w()
p t i sh w(B x+b ur)
p t sh w()
15
lOMoARcPSD|40491420
,utput
e) Gradie ts
C de
i p rt u py as p
i p rt cv2 as cv
fr atp t ib i p rt pyp t as p t
i g $ cv i read('dave 3pg') cv I*READ+GRAYSCA E)
assert i g is t e) "fi e c u d t be read) chec' with s path exists()"
ap acia $ cv ap acia (i g)cv CV+64F)
s be x $ cv S be (i g)cv CV+64F)1)0)'si;e$5)
s be y $ cv S be (i g)cv CV+64F)0)1)'si;e$5)
p t subp t(2)2)1))p t i sh w(i g)c ap $ 'gray')
p t tit e(',rigi a ')) p t xtic's(56)) p t ytic's(56)
p t subp t(2)2)2))p t i sh w( ap acia )c ap $ 'gray')
p t tit e(' ap acia ')) p t xtic's(56)) p t ytic's(56)
p t subp t(2)2)3))p t i sh w(s be x)c ap $ 'gray')
p t tit e('S be X')) p t xtic's(56)) p t ytic's(56)
p t subp t(2)2)4))p t i sh w(s be y)c ap $ 'gray')
p t tit e('S be Y')) p t xtic's(56)) p t ytic's(56)
p t sh w()
,utput
16
lOMoARcPSD|40491420
f) Edge Detecti
C de
i p rt cv2
i p rt u py as p
i p rt atp t ib pyp t as p t
# read the i age
i age $ cv2 i read(" itt e+f wer 3pg")
# c vert it t graysca e
gray $ cv2 cvtC r(i age) cv2 C, ,R+BGR2GRAY)
# sh w the graysca e i age
p t i sh w(gray) c ap$"gray")
p t sh w()
# perf r the ca y edge detect r t detect i age edges
edges $ cv2 Ca y(gray) thresh d1$30) thresh d2$100)
I put I age
,utput I age
Resu t
Thus i p e e tati f i age e ha ce e t f r vari us tech iques was successfu y
verified
17
lOMoARcPSD|40491420
a) I age Tra sf r s
C de Tra s ati
i p rt u py as p
i p rt cv2 as cv
i g $ cv i read('gir I age 3pg') 0)
r ws) c s $ i g shape
* $ p f at32(551) 0) 1006) 50) 1) 5066)
dst $ cv warpAffi e(i g) *) (c s) r ws))
cv i sh w('i g') dst)
cv wait.ey(0)
cv destr yA Wi d ws()
,utput
C de Ref ecti
i p rt u py as p
i p rt cv2 as cv
i g $ cv i read('gir I age 3pg') 0)
r ws) c s $ i g shape
* $ p f at32(551) 0) 06)
50) 1) r ws6)
50) 0) 166)
ref ected+i g $ cv warpPerspective(i g) *)
(i t(c s))
i t(r ws)))
cv i sh w('i g') ref ected+i g)
cv i write('ref ecti + ut 3pg') ref ected+i g)
cv wait.ey(0)
cv destr yA Wi d ws()
18
lOMoARcPSD|40491420
,utput
C de R tati
i p rt u py as p
i p rt cv2 as cv
i g $ cv i read('gir I age 3pg') 0)
r ws) c s $ i g shape
* $ p f at32(551) 0) 06) 50) 1) r ws6) 50) 0) 166)
i g+r tati $ cv warpAffi e(i g)
cv getR tati *atrix2D((c sN2) r wsN2))
30) 0 6))
(c s) r ws))
cv i sh w('i g') i g+r tati )
cv i write('r tati + ut 3pg') i g+r tati )
cv wait.ey(0)
cv destr yA Wi d ws()
,utput
C de Sca i g
i p rt u py as p
i p rt cv2 as cv
i g $ cv i read('gir I age 3pg') 0)
r ws) c s $ i g shape
i g+shri 'ed $ cv resi;e(i g) (250) 200))
i terp ati $cv I TER+AREA)
cv i sh w('i g') i g+shri 'ed)
i g+e arged $ cv resi;e(i g+shri 'ed) e)
fx$1 5) fy$1 5)
i terp ati $cv I TER+CUBIC)
cv i sh w('i g') i g+e arged)
cv wait.ey(0)
cv destr yA Wi d ws()
19
,utput
C de Sheari g i X axis
i p rt u py as p
i p rt cv2 as cv
i g $ cv i read('gir I age 3pg') 0)
r ws) c s $ i g shape
* $ p f at32(551) 0 5) 06) 50) 1) 06) 50) 0) 166)
sheared+i g $ cv warpPerspective(i g) *) (i t(c sP1 5)) i t(r wsP1 5)))
cv i sh w('i g') sheared+i g)
cv wait.ey(0)
cv destr yA Wi d ws()
,utput
C de Sheari g i Y axis
i p rt u py as p
i p rt cv2 as cv
i g $ cv i read('gir I age 3pg') 0)
r ws) c s $ i g shape
* $ p f at32(551) 0) 06) 50 5) 1) 06) 50) 0) 166)
sheared+i g $ cv warpPerspective(i g) *) (i t(c sP1 5)) i t(r wsP1 5)))
cv i sh w('sheared+y axis+ ut 3pg') sheared+i g)
cv wait.ey(0)
cv destr yA Wi d ws()
,utput
20
lOMoARcPSD|40491420
Code:
# i p rt required ibraries
i p rt cv2
# read i put i age
i g $ cv2 i read('h use 3pg')
# c vert the i age t graysca e
gray $ cv2 cvtC r(i g) cv2 C, ,R+BGR2GRAY)
# I itiate ,RB b3ect with defau t va ues
rb $ cv2 ,RB+create( features$2000)
# detect a d c pute the 'eyp i ts i age (graysca e)
'p $ rb detect(gray) e)
'p) des $ rb c pute(gray) 'p)
# draw 'eyp i ts i i age
i g1 $ cv2 draw.eyp i ts(gray) 'p) e) (0)0)255)) f ags$0)
# disp ay the i age with 'eyp i ts draw it
cv2 i sh w(",RB .eyp i ts") i g1)
cv2 wait.ey(0)
cv2 destr yA Wi d ws()
I put
,utput
c) Feature atchi g
C de
# I p rti g the ibraries
i p rt cv2
21
lOMoARcPSD|40491420
I put
,utput
d) C i g
C de
# Sta dard i p rts
i p rt cv2
i p rt u py as p
# Read i ages
src $ cv2 i read("i agesNairp a e 3pg")
dst $ cv2 i read("i agesNs'y 3pg")
# Create a r ugh as' ar u d the airp a e
22
lOMoARcPSD|40491420
,utput
Resu t
Thus i p e e tati f i age features a d a ig e t f r vari us tech iques was
successfu y verified
23
lOMoARcPSD|40491420
Ex 6
Image segmentation using Graphcut / Grabcut
Date
Pr cedure
i age! I put 8*bit 3*cha e i age
as ! I putD utput 8*bit si g e*cha e as The as is i itia i2ed by the fu cti whe
de is set t GC@I%IT@WITH@RECT Its e e e ts ay have e f f wi g va ues!
GC@BGD defi es a bvi us bac gr u d pixe s
GC@FGD defi es a bvi us f regr u d ( b)ect) pixe
GC@PR@BGD defi es a p ssib e bac gr u d pixe
GC@PR@FGD defi es a p ssib e f regr u d pixe
recta g e! It is the regi f i terest c tai i g a seg e ted b)ect The pixe s utside f the
R I are ar ed as bvi us bac gr u d The para eter is y used whe
deEEGC@I%IT@WITH@RECT
bac gr u dF de ! Te p rary array f r the bac gr u d de
f regr u dF de ! Te p rary array f r the f regr u d de
iterati C u t! %u ber f iterati s the a g rith sh u d a e bef re retur i g the resu t
% te that the resu t ca be refi ed with further ca s with deEEGC@I%IT@WITH@FASG r
deEEGC@EVA3
de! It defi es the perati de It ca be e f the f wi g!
GC@I%IT@WITH@RECT! The fu cti i itia i2es the state a d the as usi g the pr vided
recta g e After that it ru s iterC u t iterati s f the a g rith
GC@I%IT@WITH@FASG! The fu cti i itia i2es the state usi g the pr vided as % te that
GC@I%IT@WITH@RECT a d GC@I%IT@WITH@FASG ca be c bi ed The a the pixe s
utside f the R I are aut atica y i itia i2ed with GC@BGD
GC@EVA3! The va ue ea s that the a g rith sh u d )ust resu e
C de
# Pyth pr gra t i ustrate
# f regr u d extracti usi g
# GrabCut a g rith
# rga i;e i p rts
i p rt u py as p
i p rt cv2
fr atp t ib i p rt pyp t as p t
# path t i put i age specified a d
# i age is aded with i read c a d
i age $ cv2 i read('i age 3pg')
# create a si p e as' i age si i ar
# t the aded i age) with the
# shape a d retur type
as' $ p ;er s(i age shape5 26) p ui t8)
# specify the bac'gr u d a d f regr u d de
# usi g u py the array is c structed f 1 r w
# a d 65 c u s) a d a array e e e ts are 0
# Data type f r the array is p f at64 (defau t)
24
lOMoARcPSD|40491420
I put
25
lOMoARcPSD|40491420
,utput
Resu t
Thus i p e e tati f i age seg e tati usi g Graphcut A g rith is verified
successfu y
26
lOMoARcPSD|40491420
Ex 7
Camera Calibration with circular grid
Date
Pr cedure
Step 1! First defi e rea w r d c rdi ates f 3D p i ts usi g w si2e f chec erb ard
patter
Step 2! Differe t viewp i ts f chec *b ard i age is captured
Step 3! fi dChessb ardC r ers() is a eth d i pe CV a d used t fi d pixe c rdi ates (u
v) f r each 3D p i t i differe t i ages
Step 4! The ca ibrateCa era() eth d is used t fi d ca era para eters
C de
# I p rt required du es
i p rt cv2
i p rt u py as p
i p rt s
i p rt g b
# Defi e the di e si s f chec'erb ard
CHEC.ERB,ARD $ (6) 9)
# st p the iterati whe specified
# accuracy) epsi ) is reached r
# specified u ber f iterati s are c p eted
criteria $ (cv2 TER*+CRITERIA+EPS F
cv2 TER*+CRITERIA+*AX+ITER) 30) 0 001)
# Vect r f r 3D p i ts
threedp i ts $ 56
# Vect r f r 2D p i ts
tw dp i ts $ 56
# 3D p i ts rea w r d c rdi ates
b3ectp3d $ p ;er s((1) CHEC.ERB,ARD506
P CHEC.ERB,ARD516)
3)) p f at32)
b3ectp3d50) ) 26 $ p grid50 CHEC.ERB,ARD506)
0 CHEC.ERB,ARD5166 T reshape( 1) 2)
prev+i g+shape $ e
# Extracti g path f i dividua i age st red
# i a give direct ry Si ce path is
# specified) it wi ta'e curre t direct ry
# 3pg fi es a e
i ages $ g b g b('P 3pg')
f r fi e a e i i ages
i age $ cv2 i read(fi e a e)
grayC r $ cv2 cvtC r(i age) cv2 C, ,R+BGR2GRAY)
# Fi d the chess b ard c r ers
# If desired u ber f c r ers are
27
lOMoARcPSD|40491420
h) w $ i age shape5 26
# Perf r ca era ca ibrati by
# passi g the va ue f ab ve f u d ut 3D p i ts (threedp i ts)
# a d its c rresp di g pixe c rdi ates f the
# detected c r ers (tw dp i ts)
ret) atrix) dist rti ) r+vecs) t+vecs $ cv2 ca ibrateCa era(
threedp i ts) tw dp i ts) grayC r shape5 16) e) e)
# Disp ayi g required utput
pri t(" Ca era atrix ")
pri t( atrix)
pri t("< Dist rti c efficie t ")
pri t(dist rti )
pri t("< R tati Vect rs ")
pri t(r+vecs)
pri t("< Tra s ati Vect rs ")
pri t(t+vecs)
28
lOMoARcPSD|40491420
I put
,utput
Ca era atrix
55 36 26378216 0 125 685391686
5 0 36 76607372 142 498211476
5 0 0 1 66
R tati Vect rs
5array(55 0 057674926)
5 0 035494976)
5 1 5090695366)) array(55 0 093019826)
5 0 010343216)
5 3 0773380566)) array(55 0 021753326)
29
lOMoARcPSD|40491420
5 0 056111056)
5 0 0730816166)6
Resu t
Thus i p e e tati f Ca era Ca ibrati with Circu ar Grid is verified successfu y
30
lOMoARcPSD|40491420
Ex 8
Pose Estimation
Date
C de
!pip i sta pe cv pyth
!pip i sta ediapipe
i p rt cv2
i p rt ediapipe as p
## i itia i;e p se esti at r
p+drawi g $ p s uti s drawi g+uti s
p+p se $ p s uti s p se
p se $ p+p se P se( i +detecti +c fide ce$0 5) i +trac'i g+c fide ce$0 5)
cap $ cv2 Vide Capture('test+vide p4')
whi e cap is,pe ed()
# read fra e
+) fra e $ cap read()
try
# resi;e the fra e f r p rtrait vide
fra e $ cv2 resi;e(fra e) (350) 600))
# c vert t RGB
fra e+rgb $ cv2 cvtC r(fra e) cv2 C, ,R+BGR2RGB)
cap re ease()
cv2 destr yA Wi d ws()
# get a d ar' f r a specific p i t
p se+resu ts p se+ a d ar's a d ar'5326
31
lOMoARcPSD|40491420
,utput
Resu t
Thus i p e e tati f P se Esti ati is verified successfu y
32
lOMoARcPSD|40491420
Ex 9
3D Reconstruction – Creating Depth map from stereo images
Date
Pr cedure
C ect r ta e stere i ages
I p rt pe CV a d atp t ib ibraries
Read b th eft a d right i ages
Ca cu ate disparity usi g stere c pute
C de
# i p rt ,pe CV a d pyp t
i p rt cv2 as cv
fr atp t ib i p rt pyp t as p t
# read eft a d right i ages
i gR $ cv i read('right p g') 0)
i g $ cv i read(' eft p g') 0)
# creates Stere B b3ect
stere $ cv Stere B*+create( u Disparities $ 16) b c'Si;e $ 15)
# c putes disparity
disparity $ stere c pute(i g ) i gR)
# disp ays i age as graysca e a d p tted
p t i sh w(disparity) 'gray')
p t sh w()
I put
eft Right
33
lOMoARcPSD|40491420
,utput
Resu t
Thus i p e e tati f 3D Rec structi creati g depth ap fr stere i ages is
verified successfu y
34
lOMoARcPSD|40491420
Ex 10
Object Detection and Tracking using Kalman Filter, Camshift
Date
C de
i p rt u py as p
i p rt cv2 as cv
# Read the i put vide
cap $ cv Vide Capture('sa p e p4')
# ta'e first fra e f the
# vide
ret) fra e $ cap read()
# setup i itia regi f
# trac'er
x) y) width) height $ 400) 440) 150) 150
trac'+wi d w $ (x) y)
width) height)
# set up the Regi f
# I terest f r trac'i g
r i $ fra e5y y F height)
x x F width6
#c vert R,I fr BGR t
# HSV f r at
hsv+r i $ cv cvtC r(r i)
cv C, ,R+BGR2HSV)
# perf r as'i g perati
as' $ cv i Ra ge(hsv+r i)
p array((0 ) 60 ) 32 )))
p array((180 ) 255 ) 255)))
35
lOMoARcPSD|40491420
51806)
50) 1806)
36
lOMoARcPSD|40491420
# ew cati
ret2) trac'+wi d w $ cv Ca Shift(dst)
trac'+wi d w)
ter +crit)
# Draw it i age
pts $ cv b xP i ts(ret2)
#c vert fr f ati g
# t i teger
pts $ p i t0(pts)
# Draw Trac'i g wi d w the
# vide fra e
Resu t $ cv p y i es(fra e)
5pts6)
True)
(0) 255) 255))
2)
cv i sh w('Ca shift') Resu t)
# set ESC 'ey as the
# exit butt
' $ cv wait.ey(30) & 0xff
if ' $$ 27
brea'
# Re ease the cap b3ect
cap re ease()
# c se a pe ed wi d ws
cv destr yA Wi d ws()
37
lOMoARcPSD|40491420
,utput
Resu t
Thus i p e e tati f b)ect detecti a d Trac i g usi g Ca shift sssis verified
successfu y
38