100% found this document useful (1 vote)
221 views43 pages

Computer Vision - Lab Manual

The document outlines the Computer Vision Laboratory curriculum for the B.E. Computer Science & Engineering program at Anna University for the academic year 2023-24. It includes program educational objectives, outcomes, a list of experiments to be conducted in the lab, and the necessary software and hardware requirements. The syllabus covers various topics such as OpenCV installation, basic image processing, image enhancement, and object detection techniques.

Uploaded by

dwarakasekar0531
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
221 views43 pages

Computer Vision - Lab Manual

The document outlines the Computer Vision Laboratory curriculum for the B.E. Computer Science & Engineering program at Anna University for the academic year 2023-24. It includes program educational objectives, outcomes, a list of experiments to be conducted in the lab, and the necessary software and hardware requirements. The syllabus covers various topics such as OpenCV installation, basic image processing, image enhancement, and object detection techniques.

Uploaded by

dwarakasekar0531
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

lOMoARcPSD|40491420

LABORATORY RECORD
For

CCS338– COMPUTER VISION LABORATORY


Of

B.E Computer Science & Engineering

(Anna University Regulation 2021)

SEMESTER: VI

ACADEMIC YEAR: 2023-24

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING


CHENNAI – 603103
lOMoARcPSD|40491420

PROGRAMME EDUCATIONAL OBJECTIVES:


The Graduates of BE (Computer Science & Engineering) will:

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.

PEO4 Exhibit ethical attitude and social responsibility in their profession.

PROGRAMME OUTCOMES AND PROGRAMME SPECIFIC OUTCOMES


After successful completion of BE Computer Science & Engineering program, the students will be
able to:

PO No. Description of the PO


Engineering knowledge: Apply the knowledge of mathematics, science, engineering
PO 1 fundamentals, and an engineering specialization for the solution of complex engineering
problems.
Problem analysis: Identify, formulate, research literature, and analyze complex engineering
PO 2 problems reaching substantiated conclusions using first principles of mathematics, natural
sciences, and engineering sciences.
Design/development of solutions: Design solutions for complex engineering problems and design
PO 3 system components or processes that meet t h e specified needs with appropriate consideration
for public health and safety, and cultural, societal, and environmental considerations.
Conduct investigations of complex problems: Use research-based knowledge and research
PO 4 methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.
Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
PO 5 engineering and IT tools, including prediction and modeling to complex engineering activities,
with an understanding of the limitations.
The engineer and society: Apply reasoning informed by the contextual knowledge to assess
PO 6 societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.
lOMoARcPSD|40491420

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.

PROGRAMME SPECIFIC OUTCOMES (PSO)

PSO Description of PSO


d
PSO 1

PSO 2

PSO 3
lOMoARcPSD|40491420

SYLLABUS

CCS338 – COMPUTER VISION

LABORATORY LIST OF EXPERIMENTS

1. OpenCV Installation and working with Python


2. Basic Image Processing - loading images, Cropping, Resizing, Thresholding, Contour analysis, Bolb
detection
3. Image Annotation – Drawing lines, text circle, rectangle, ellipse on images
4. Image Enhancement - Understanding Color spaces, color space conversion, Histogram equalization,
Convolution, Image smoothing, Gradients, Edge Detection
5. Image Features and Image Alignment – Image transforms – Fourier, Hough, Extract ORB Image
features, Feature matching, cloning, Feature matching-based image alignment
6. Image segmentation using Graphcut / Grabcut
7. Camera Calibration with circular grid
8. Pose Estimation
9. 3D Reconstruction – Creating Depth map from stereo images
10. Object Detection and Tracking using Kalman Filter, Camshift

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

Basic Image Processing - loading images, Cropping,


2
Resizing, Thresholding, Contour analysis, Bolb
detection
Image Annotation – Drawing lines, text circle,
3
rectangle, ellipse on images
4 Image Enhancement - Understanding Color spaces,
color space conversion, Histogram equalization,
Convolution, Image smoothing, Gradients, Edge
Detection
5 Image Features and Image Alignment – Image
transforms – Fourier, Hough, Extract ORB Image
features, Feature matching, cloning, Feature matching-
based image alignment
6 Image segmentation using Graphcut / Grabcut
7 Camera Calibration with circular grid
8 Pose Estimation
3D Reconstruction – Creating Depth map from stereo
9
images
Object Detection and Tracking using Kalman Filter,
10
Camshift
lOMoARcPSD|40491420

Ex 1
OpenCV Installation and working with Python
Date

Ai T i sta pe CV a d w r i g with pyth

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

3) T i sta pe CV )ust g t the c a d* i e a d type the f wi g c a d!


pip i sta pe cv pyth
4) Type the c a d i the Ter i a a d pr ceed!

5) C ecti g I f r ati a dd w adi g data!

6) I sta i g Pac ages!

1
lOMoARcPSD|40491420

7) Fi ished I sta ati !

8) T chec if pe CV is c rrect y i sta ed )ust ru the f wi g c a ds t perf r a


versi chec !
python
>>>import cv2
>>>print(cv2.__version__)

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

Ex 2 Basic Image Processing - loading images, Cropping, Resizing,


Date Thresholding, Contour analysis, Bolb detection

Ai T i p e e t Basic I age Pr cessi g i e adi g i ages cr ppi g resi2i g


thresh di g c t ur a a ysis a d b b detecti

a) adi g I ages
The steps t 3 ad a d disp ay a i age i pe CV are!

1 Read a i age usi g i read() fu cti

2 Create a GUI wi d w a d disp ay i age usi g i sh w() fu cti

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

4 De ete i age wi d w fr the e ry after disp ayi g usi g destr yA Wi d ws()


fu cti

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

Steps t Cr p the i age


Step 1! Read the i age
Step 2! Get the i age Di e si s
Step 3! S ice the i age

3
lOMoARcPSD|40491420

C di g
i p rt cv2

i g $ cv2 i read("test 3peg")


pri t(type(i g))

# Shape f the i age


pri t("Shape f the i age") i g shape)

# 5r ws) c u s6
cr p $ i g550 180) 100 3006

cv2 i sh w(' rigi a ') i g)


cv2 i sh w('cr pped') cr p)
cv2 wait.ey(0)
cv2 destr yA Wi d ws()

,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

ha f $ cv2 resi;e(i age) (0) 0)) fx $ 0 1) fy $ 0 1)


bigger $ cv2 resi;e(i age) (1050) 1610))

stretch+ ear $ cv2 resi;e(i age) (780) 540))

4
lOMoARcPSD|40491420

i terp ati $ cv2 I TER+ I EAR)

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

# path t i put i age is specified a d


# i age is aded with i read c a d
i age1 $ cv2 i read('i put1 3pg')

# cv2 cvtC r is app ied ver the


# i age i put with app ied para eters
# t c vert the i age i graysca e
i g $ cv2 cvtC r(i age1) cv2 C, ,R+BGR2GRAY)

# app yi g differe t thresh di g


# tech iques the i put i age
# a pixe s va ue ab ve 120 wi
# be set t 255
ret) thresh1 $ cv2 thresh d(i g) 120) 255) cv2 THRESH+BI ARY)
ret) thresh2 $ cv2 thresh d(i g) 120) 255) cv2 THRESH+BI ARY+I V)
ret) thresh3 $ cv2 thresh d(i g) 120) 255) cv2 THRESH+TRU C)
ret) thresh4 $ cv2 thresh d(i g) 120) 255) cv2 THRESH+T,KER,)
ret) thresh5 $ cv2 thresh d(i g) 120) 255) cv2 THRESH+T,KER,+I V)

# the wi d w sh wi g utput i ages


# with the c rresp di g thresh di g
# tech iques app ied t the i put i ages
cv2 i sh w('Bi ary Thresh d') thresh1)
cv2 i sh w('Bi ary Thresh d I verted') thresh2)
cv2 i sh w('Tru cated Thresh d') thresh3)
cv2 i sh w('Set t 0') thresh4)
cv2 i sh w('Set t 0 I verted') thresh5)

# De a cate a y ass ciated e ry usage


if cv2 wait.ey(0) & 0xff $$ 27
cv2 destr yA Wi d ws()

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)

# Set ur fi teri g para eters


# I itia i;e para eter setti g usi g cv2 Si p eB bDetect r
para s $ cv2 Si p eB bDetect r+Para s()

# Set Area fi teri g para eters


para s fi terByArea $ True
para s i Area $ 100

# Set Circu arity fi teri g para eters


para s fi terByCircu arity $ True
para s i Circu arity $ 0 9

# Set C vexity fi teri g para eters


para s fi terByC vexity $ True
para s i C vexity $ 0 2

8
lOMoARcPSD|40491420

# Set i ertia fi teri g para eters


para s fi terByI ertia $ True
para s i I ertiaRati $ 0 01

# Create a detect r with the para eters


detect r $ cv2 Si p eB bDetect r+create(para s)

# Detect b bs
'eyp i ts $ detect r detect(i age)

# Draw b bs ur i age as red circ es


b a ' $ p ;er s((1) 1))
b bs $ cv2 draw.eyp i ts(i age) 'eyp i ts) b a ') (0) 0) 255))
cv2 DRAW+*ATCHES+F AGS+DRAW+RICH+.EYP,I TS)

u ber+ f+b bs $ e ('eyp i ts)


text $ " u ber f Circu ar B bs " F str( e ('eyp i ts))
cv2 putText(b bs) text) (20) 550))
cv2 F, T+HERSHEY+SI*P EX) 1) (0) 100) 255)) 2)

# 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

Ex 3 Image Annotation – Drawing lines, text circle, rectangle, ellipse on


Date images

Ai T i p e e t I age A tati f r i es Text Circ e recta g e e ipse i ages

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

# defi e the starti g a d e d p i ts f the recta g e


start+p i t $(300)115)
e d+p i t $(475)225)
# draw the recta g e
cv2 recta g e(i ageRecta g e) start+p i t) e d+p i t) (0) 0) 255)) thic' ess$ 3)
i eType$cv2 I E+8)
# disp ay the utput
cv2 i sh w('i ageRecta g e') i ageRecta g e)
cv2 wait.ey(0)
# a'e a c py f the rigi a i age
i ageE ipse $ i g c py()
# defi e the ce ter p i t f e ipse
e ipse+ce ter $ (415)190)
# defi e the a3 r a d i r axes f the e ipse
axis1 $ (100)50)
axis2 $ (125)50)
# draw the e ipse
#H ri; ta
cv2 e ipse(i ageE ipse) ce ter) axis1) 0) 0) 360) (255) 0) 0)) thic' ess$3)
#Vertica
cv2 e ipse(i ageE ipse) ce ter) axis2) 90) 0) 360) (0) 0) 255)) thic' ess$3)
# disp ay the utput
cv2 i sh w('e ipse I age')i ageE ipse)
cv2 wait.ey(0)
# a'e a c py f the rigi a i age
i ageText $ i g c py()
# et's write the text y u wa t t put the i age
text $ 'I a a Happy d g!'
# rg Where y u wa t t put the text
rg $ (50)350)
# write the text the i put i age
cv2 putText(i ageText) text) rg) f tFace $ cv2 F, T+HERSHEY+C,*P EX)
f tSca e $ 1 5) c r $ (250)225)100))))
# disp ay the utput i age with text ver it
cv2 i sh w("I age Text")i ageText)
cv2 wait.ey(0)
cv2 destr yA Wi d ws()

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

Ex 4 Image Enhancement - Understanding Color spaces, color space


Date conversion, Histogram equalization, Convolution, Image smoothing,
Gradients, Edge Detection

Ai T i p e e t I age E ha ce e t tech iques f r C r spaces Hist gra i age


S thi g a d Edge detecti

a) U dersta di g C r Spaces

C de
i p rt cv2

i age $ cv2 i read('C NNUsersNNGfgNNrgb p g')


B) G) R $ cv2 sp it(i age)
# C rresp di g cha e s are separated

cv2 i sh w(" rigi a ") i age)


cv2 wait.ey(0)

cv2 i sh w("b ue") B)


cv2 wait.ey(0)

cv2 i sh w("Gree ") G)


cv2 wait.ey(0)

cv2 i sh w("red") R)
cv2 wait.ey(0)

cv2 destr yA Wi d ws()

,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

src $ cv2 i read(path)


# Wi d w a e i which i age is disp ayed
wi d w+ a e $ 'I age'
# Usi g cv2 cvtC r() eth d
# Usi g cv2 C, ,R+BGR2GRAY c r space
# c versi c de
i age $ cv2 cvtC r(src) cv2 C, ,R+BGR2GRAY )
# Disp ayi g the i age
cv2 i sh w(wi d w+ a e) i age)

I put I age

,utput I age

c) Hist gra Equa i;ati

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

Ex 5 Image Features and Image Alignment – Image transforms – Fourier,


Date Hough, Extract ORB Image features, Feature matching, cloning,
Feature matching-based image alignment

Ai T i p e e t I age Features a d I age A ig e t f r vari us tech iques

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

Downloaded by Vimal MSAJCE ([email protected])


lOMoARcPSD|40491420

,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

b) Extract ORB Image features

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

# Readi g the i age a d c verti g i t BNW


i age $ cv2 i read('b ' p g')
gray+i age $ cv2 cvtC r(i age) cv2 C, ,R+BGR2GRAY)
# App yi g the fu cti
fast $ cv2 FastFeatureDetect r+create()
fast set axSuppressi (Fa se)
# Drawi g the 'eyp i ts
'p $ fast detect(gray+i age) e)
'p+i age $ cv2 draw.eyp i ts(i age) 'p) e) c r$(0) 255) 0))
cv2 i sh w('FAST') 'p+i age)
cv2 wait.ey()

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

src+ as' $ p ;er s(src shape) src dtype)


p y $ p array(5 54)806) 530)546) 5151)636) 5254)376) 5298)906) 5272)1346) 543)1226 6)
p i t32)
cv2 fi P y(src+ as') 5p y6) (255) 255) 255))
# This is where the CE TER f the airp a e wi be p aced
ce ter $ (800)100)
# C e sea ess y
utput $ cv2 sea essC e(src) dst) src+ as') ce ter) cv2 ,R*A +C , E)
# Save resu t
cv2 i write("i agesN pe cv sea ess c i g exa p e 3pg") utput)Q

,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

Ai T i p e e t I age Seg e tati usi g Graphcut tech ique

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

bac'gr u d* de $ p ;er s((1) 65)) p f at64)


f regr u d* de $ p ;er s((1) 65)) p f at64)
# defi e the Regi f I terest (R,I)
# as the c rdi ates f the recta g e
# where the va ues are e tered as
# (starti gP i t+x) starti gP i t+y) width) height)
# these c rdi ates are acc rdi g t the i put i age
# it ay vary f r differe t i ages
recta g e $ (20) 100) 150) 150)
# app y the grabcut a g rith with appr priate
# va ues as para eters) u ber f iterati s $ 3
# cv2 GC+I IT+WITH+RECT is used because
# f the recta g e de is used
cv2 grabCut(i age) as') recta g e)
bac'gr u d* de ) f regr u d* de )
3) cv2 GC+I IT+WITH+RECT)

# I the ew as' i age) pixe s wi


# be ar'ed with f ur f ags
# f ur f ags de te the bac'gr u d N f regr u d
# as' is cha ged) a the 0 a d 2 pixe s
# are c verted t the bac'gr u d
# as' is cha ged) a the 1 a d 3 pixe s
# are w the part f the f regr u d
# the retur type is a s e ti ed)
# this gives us the fi a as'
as'2 $ p where(( as' $$ 2)R( as' $$ 0)) 0) 1) astype('ui t8')
# The fi a as' is u tip ied with
# the i put i age t give the seg e ted i age
i age $ i age P as'25 ) ) p ewaxis6
# utput seg e ted i age with c rbar
p t i sh w(i age)
p t c rbar()
p t sh w()

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

Ai T i p e e t Ca era Ca ibrati with Circu ar grid usi g pe CV i pyth

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

# f u d i the i age the ret $ true


ret) c r ers $ cv2 fi dChessb ardC r ers(
grayC r) CHEC.ERB,ARD)
cv2 CA IB+CB+ADAPTIVE+THRESH
F cv2 CA IB+CB+FAST+CHEC. F
cv2 CA IB+CB+ ,R*A IKE+I*AGE)
# If desired u ber f c r ers ca be detected the )
# refi e the pixe c rdi ates a d disp ay
# the the i ages f chec'er b ard
if ret $$ True
threedp i ts appe d( b3ectp3d)
# Refi i g pixe c rdi ates
# f r give 2d p i ts
c r ers2 $ cv2 c r erSubPix(
grayC r) c r ers) (11) 11)) ( 1) 1)) criteria)

tw dp i ts appe d(c r ers2)


# Draw a d disp ay the c r ers
i age $ cv2 drawChessb ardC r ers(i age)
CHEC.ERB,ARD)
c r ers2) ret)

cv2 i sh w('i g') i age)


cv2 wait.ey(0)

cv2 destr yA Wi d ws()

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

Dist rti c efficie t


55 1 25491812e 03 9 89269357e 05 2 89077718e 03 4 52760939e 04
3 29964245e 0666

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

Tra s ati Vect rs


5array(55 4 630473516)
5 3 742813866)
5 1 6423810866)) array(552 316487376)
53 988015216)
51 6458462266)) array(55 3 175488086)
5 3 460224666)
5 1 6820015766)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

Ai T I p e e t P se esti ati f r the Vide

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)

# pr cess the fra e f r p se detecti


p se+resu ts $ p se pr cess(fra e+rgb)
# pri t(p se+resu ts p se+ a d ar's)

# draw s'e et the fra e


p+drawi g draw+ a d ar's(fra e) p se+resu ts p se+ a d ar's)
p+p se P,SE+C, ECTI, S)
# disp ay the fra e
cv2 i sh w(',utput') fra e)
except
brea'

if cv2 wait.ey(1) $$ rd('q')


brea'

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

Ai T i p e e t 3D Rec structi creati g depth ap fr stere i ages usi g pe


CV

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

Ai T i p e e t b)ect detecti a d trac i g usi g Ga a fi ter Ca shift usi g pe CV

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)))

r i+hist $ cv ca cHist(5hsv+r i6)


506) as')

35
lOMoARcPSD|40491420

51806)
50) 1806)

cv r a i;e(r i+hist) r i+hist)


0) 255) cv ,R*+*I *AX)
# Setup the ter i ati criteria)
# either 15 iterati r ve by
# at east 2 pt
ter +crit $ ( cv TER*+CRITERIA+EPS R
cv TER*+CRITERIA+C,U T) 15) 2)
whi e(1)
ret) fra e $ cap read()
# Resi;e the vide fra es
fra e $ cv resi;e(fra e)
(720) 720))
fx $ 0) fy $ 0)
i terp ati $ cv I TER+CUBIC)
cv i sh w(',rigi a ') fra e)
# perf r thresh di g
# the vide fra es
ret1) fra e1 $ cv thresh d(fra e)
180) 155)
cv THRESH+T,KER,+I V)
#c vert fr BGR t HSV
# f r at
hsv $ cv cvtC r(fra e1)
cv C, ,R+BGR2HSV)
dst $ cv ca cBac'Pr 3ect(5hsv6)
506)
r i+hist)
50) 1806) 1)
# app y Ca shift t get the

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

You might also like