Weitere ähnliche Inhalte Ähnlich wie RoboCV Module 5: Contours using OpenCV (20) Mehr von roboVITics club (6) Kürzlich hochgeladen (20) RoboCV Module 5: Contours using OpenCV2. Content Information
• This set of slides has been made with respect to the
popular book Learning OpenCV by Gary Bradski
and Adrian Kaehler, O’Reilly Media, Inc.
• The slides are to be used along with the Code
Gallery shared along with it
• Slides made by Mayank Prasad, B.Tech (IVth
Year), ECE, VIT University
© Copyright Mayank Prasad
2
roboVITics, 2012
3. Outline
• Memory Storage
• Sequences
o Creating a Sequence
o Deleting a Sequence
o Direct Access
o Slicing, Copying and Moving Data
o Sequences as Stack (Not Discussed)
o Sequences and Arrays (Not Discussed)
• Contour Finding
o Contours and Sequences
o Freeman Chain Codes
o Drawing Contours
• More to do with Contours (Not Discussed)
© Copyright Mayank Prasad
3
roboVITics, 2012
4. Introduction
• Algorithms like the Canny edge detector used to
find edge pixels that separate different segments in
an image, but nothing about them as entities.
• Next step – assemble those edge pixels into
contours
• cvFindContours() is used
© Copyright Mayank Prasad
4
roboVITics, 2012
5. Memory Storage
Handle memory allocation for dynamic objects in OpenCV
© Copyright Mayank Prasad
5
roboVITics, 2012
6. Memory Storage
• Linked lists of memory blocks that allow for fast
allocation and de-allocation of continuous sets of
blocks
• Accessed by OpenCV function which require
memory allocation as a part of their normal
functionality
© Copyright Mayank Prasad
6
roboVITics, 2012
8. Sequences
Sequences stored inside memory storage
© Copyright Mayank Prasad
8
roboVITics, 2012
9. Sequences
• Stored inside memory storage
• Linked lists of other structures
• Analogous to generic container classes
• Sequence structure in OpenCV is actually a deque
o Very fast for random access and add/delete from either end
o Little slow for such things in the middle
• Direct access to sequence elements (More Later)
o The most direct way – and the correct way to access any random
element (apart from accessing from the ends)
© Copyright Mayank Prasad
9
roboVITics, 2012
12. Flags – Set 1
Flag Type of Object
CV_SEQ_ELTYPE_POINT (x,y)
CV_SEQ_ELTYPE_CODE Freeman Code: 0…7
CV_SEQ_ELTYPE_POINT Pointer to a point: &(x,y)
CV_SEQ_ELTYPE_INDEX Integer Index of a point: #(x,y)
CV_SEQ_ELTYPE_GRAPH_EDGE &next_o, &next_d, &vtx_o, &vtx_d
CV_SEQ_ELTYPE_GRAPH_VERTEX first_edge, &(x,y)
CV_SEQ_ELTYPE_TRIAN_ATR Vertex of a binary tree
CV_SEQ_ELTYPE_CONNECTED_COMP Connected Component
CV_SEQ_ELTYPE_POINT3D (x,y,z)
© Copyright Mayank Prasad
12
roboVITics, 2012
13. Flags – Set 2/3
Flags Nature of Object
CV_SEQ_KIND_SET A set of objects
CV_SEQ_KIND_CURVE A curve defined by the objects
CV_SEQ_KIND_BIN_TREE A binary tree of the objects
CV_SEQ_KIND_GRAPH A graph with objects as nodes
Flags Other Property of Object
CV_SEQ_FLAG_CLOSED Sequence is closed (polygons)
CV_SEQ_FLAG_SIMPLE Sequence is simple (polygons)
CV_SEQ_FLAG_CONVEX Sequence is convex (polygons)
CV_SEQ_FLAG_HOLE Sequence is a hole (polygons)
© Copyright Mayank Prasad
13
roboVITics, 2012
14. Direct Access to Sequence
Elements
• The most direct way – and correct way to access
random elements of a sequence (apart from
accessing at the ends)
• Example usage to print elements as a sequence of
points (typecast required)
© Copyright Mayank Prasad
14
roboVITics, 2012
15. Copying Sequences
• To deep copy sequences, use cvCloneSeq()
• More general routine is cvSeqSlice()
© Copyright Mayank Prasad
15
roboVITics, 2012
16. Slicing Sequences
• Slice can be defined using cvSlice(a,b) or the
macro CV_WHOLE_SEQ
• b can also be set to CV_WHOLE_SEQ_END_INDEX
• Slices used to specify elements to remove/insert into
a sequence
© Copyright Mayank Prasad
16
roboVITics, 2012
17. More on Sequences
• Sorting, Searching & Inverting
• Sequences as Stack (Deque)
• Inserting & Removing Elements
• Sequence Block Size
• Sequence Readers/Writers
• Sequences and Arrays
These topics may not be necessary, but will be
discussed as and when necessary
© Copyright Mayank Prasad
17
roboVITics, 2012
18. Contour Finding
Contour
List of points representing curve of an Image
© Copyright Mayank Prasad
18
roboVITics, 2012
19. Intro to Contours
• List of points representing the curve of an image
• Represented as sequences in which every entry in
the sequence encodes information about the
location of the next point on the curve
• cvFindContours() finds contours from binary
images – working shown in next slide
© Copyright Mayank Prasad
19
roboVITics, 2012
20. Contour Trees
Working of
cvFindContours()
Dashed lines represent
the exterior boundaries
of white region
(contours, c)
Dotted lines represent
the exterior boundaries
of black region
(holes, h)
© Copyright Mayank Prasad
20
roboVITics, 2012
21. cvFindContours()
• img is treated as binary and used as scratch space
• storage – location for the sequence
• firstContour – pointer to the head of contour tree
• headerSize – tells more about allocated objects
• mode – what to compute
• method – how to compute
• Returns total number of contours found
© Copyright Mayank Prasad
21
roboVITics, 2012
22. mode of
cvFindContours()
• Indicates the types of contours we want to be
found and how the result is presented to us
• Determines how tree node variables are hooked up
together
mode Description
CV_RETR_EXTERNAL Retrieves only extreme outer
contours
CV_RETR_LIST Retrieves all the contours and puts
them in the list
CV_RETR_CCOMP Retrieves all the contours and
organizes them into a two-level
hierarchy
CV_RETR_TREE Retrieves all the contours and
reconstructs the full hierarchy of
© Copyright Mayank Prasad
nested contours 22
roboVITics, 2012
23. method of
cvFindContours()
• Determines how contours are approximated
method Description
CV_CHAIN_CODE Outputs contours in the Freeman
chain code; all other methods output
polygons
CV_CHAIN_APPROX_NONE Translates all the points from the
chain code into points
CV_CHAIN_APPROX_SIMPLE Compresses horizontal, vertical, and
diagonal segments, leaving only
their ending points
CV_CHAIN_APPROX_TC89_L1 Applies one of the flavors of the Teh-
CV_CHAIN_APPROX_TC89_KCOS Chin chain approximation algorithm
CV_LINK_RUNS Completely different algorithm that
links horizontal segments of 1s
© Copyright Mayank Prasad
23
roboVITics, 2012
24. Contours are Sequences
• Contours are special types of sequences
• Other functions to manipulate them
o cvFindContours()
o cvStartFindContours()
o cvFindNextContour()
o cvSubstituteContour()
o cvEndFindContour()
o cvApproxChains()
o cvApproxPoly()
© Copyright Mayank Prasad
24
roboVITics, 2012
27. Acknowledgements
Image Courtesy References
• Learning OpenCV by • Learning OpenCV by
Gary Bradski and Gary Bradski and
Adrian Kaehler, O’Reilly Adrian Kaehler, O’Reilly
Media, Inc. Media, Inc.
© Copyright Mayank Prasad
27
roboVITics, 2012
28. Up Next
Module 6
Hardware Interfacing using Visual C++
© Copyright Mayank Prasad
28
roboVITics, 2012
29. Contacts
• Mayank Prasad
President, roboVITics
mayank@robovitics.in
• Akshat Wahi
Asst. Project Manager, roboVITics
+91 909 250 3053
akshat@core.robovitics.in
• Akash Kashyap
President, TEC – The Electronics Club of VIT
akash130791@gmail.com
© Copyright Mayank Prasad
29
roboVITics, 2012