RoboCV Module 5: Contours using OpenCV

Uploaded on

These are the slides of the RoboCV Workshop organized by roboVITics on August 11th-12th, 2012 in TT311 Smart Classroom, VIT University, Vellore. …

These are the slides of the RoboCV Workshop organized by roboVITics on August 11th-12th, 2012 in TT311 Smart Classroom, VIT University, Vellore.

The workshop was delivered by the following people:
1. Mayank Prasad, President of roboVITics
2. Akash Kashyap, President of TEC – The Electronics Club of VIT
3. Akshat Wahi, Asst. Project Manager of roboVITics

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads


Total Views
On Slideshare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. ContoursModule 5Mayank PrasadPresident, roboVITics © Copyright Mayank Prasad 1 roboVITics, 2012
  • 2. 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 5roboVITics, 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
  • 7. Routines© Copyright Mayank Prasad 7roboVITics, 2012
  • 8. Sequences Sequences stored inside memory storage© Copyright Mayank Prasad 8roboVITics, 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
  • 10. Sequence Structure© Copyright Mayank Prasad 10roboVITics, 2012
  • 11. Creating/Deleting Sequence© Copyright Mayank Prasad 11roboVITics, 2012
  • 12. Flags – Set 1Flag Type of ObjectCV_SEQ_ELTYPE_POINT (x,y)CV_SEQ_ELTYPE_CODE Freeman Code: 0…7CV_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_dCV_SEQ_ELTYPE_GRAPH_VERTEX first_edge, &(x,y)CV_SEQ_ELTYPE_TRIAN_ATR Vertex of a binary treeCV_SEQ_ELTYPE_CONNECTED_COMP Connected ComponentCV_SEQ_ELTYPE_POINT3D (x,y,z) © Copyright Mayank Prasad 12 roboVITics, 2012
  • 13. Flags – Set 2/3Flags Nature of ObjectCV_SEQ_KIND_SET A set of objectsCV_SEQ_KIND_CURVE A curve defined by the objectsCV_SEQ_KIND_BIN_TREE A binary tree of the objectsCV_SEQ_KIND_GRAPH A graph with objects as nodesFlags Other Property of ObjectCV_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 ArraysThese topics may not be necessary, but will bediscussed 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 18roboVITics, 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 20roboVITics, 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 togethermode DescriptionCV_RETR_EXTERNAL Retrieves only extreme outer contoursCV_RETR_LIST Retrieves all the contours and puts them in the listCV_RETR_CCOMP Retrieves all the contours and organizes them into a two-level hierarchyCV_RETR_TREE Retrieves all the contours and reconstructs the full hierarchy of© Copyright Mayank Prasad nested contours 22roboVITics, 2012
  • 23. method of cvFindContours()• Determines how contours are approximatedmethod DescriptionCV_CHAIN_CODE Outputs contours in the Freeman chain code; all other methods output polygonsCV_CHAIN_APPROX_NONE Translates all the points from the chain code into pointsCV_CHAIN_APPROX_SIMPLE Compresses horizontal, vertical, and diagonal segments, leaving only their ending pointsCV_CHAIN_APPROX_TC89_L1 Applies one of the flavors of the Teh-CV_CHAIN_APPROX_TC89_KCOS Chin chain approximation algorithmCV_LINK_RUNS Completely different algorithm that links horizontal segments of 1s© Copyright Mayank Prasad 23roboVITics, 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
  • 25. Freeman Chain Code© Copyright Mayank Prasad 25roboVITics, 2012
  • 26. Drawing Contours© Copyright Mayank Prasad 26roboVITics, 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 28roboVITics, 2012
  • 29. Contacts• Mayank Prasad President, roboVITics• Akshat Wahi Asst. Project Manager, roboVITics +91 909 250 3053• Akash Kashyap President, TEC – The Electronics Club of VIT© Copyright Mayank Prasad 29roboVITics, 2012