Skip to content

Latest commit

 

History

History
81 lines (50 loc) · 2.73 KB

File metadata and controls

81 lines (50 loc) · 2.73 KB

Contours & Shape Detection


Brief Intro

  1. Finding Contours -> cv2.findContours to locate object boundaries
  2. Shape Detection -> Approximate shapes using approxPolyDP for object recognition
  3. Drawing Contours -> Highlight detected objects by drawing around their edges.

Finding and Drawing Contours in OpenCV

Contour -> is the curve holding all the continuous points having same color or intensity of edges.

contours , hierarchy = cv2.findContours(img , mode , method)

[!note] Use only binary images

mode:

In here we need to tell two things about contours;

  1. How many contours?
  2. What kind of contours ?

Then these are the two most frequently used mode of retrieval;

  1. Retrieval mode tree (RETR_TREE) : hierarchical shapes (shaped inside shapes)
  2. Retrieval mode external (RETR_EXTERNAL) : Only outermost shapes
  3. Retrieval list (RETR_LIST) : all contours without hierachy

many more are there also.

method :

Approximation method used which tells us how much detailed to be returned. (CHAIN_APPROX_SIMPLE) -> only stores info regarding corner points (CHAIN_APPROX_NONE) -> every pixel

contours: list of continuous points will be returned , or list of all detected outlines.

hierarchy : will return the contour level information , parent -> child etc.

Till this we are able to find contours now to display we need to move onto the next function.


Drawing Contours

cv2.drawContours(img , contours , contour_index , color , thickness)

It basically draws over contours to show them clearly and easily using color and thickness.

contour_index tells OpenCV which contour to be shown;

  • 0 -> only first shape
  • 1 -> second shape
  • -1 -> all shapes

Shape Detection

approx = cv2.approxPolyDP(contour , epsilon , True)

Shape detection i.e. now OpenCV will automatically detect the shape during contours with the help of approxPolyDP() by counting their corners.

approxPolyDP() -> function for approving polygonal curve in shape detection

Some functionalities:

  1. Shape Approximation -> reduces vertices while preserving objects overall shape
  2. Object Recognition -> facilitates identification of geometric shapes in images
  3. Classification -> enable categorization of objects based on shape analysis
  4. Accuracy Enhancement -> improves precision of shape detection algorithm
  5. Performance Improvement -> enhance efficiency of shape detection process.

epsilon = 0.01 * cv2.arclength(contour , True) it tells that how much similarity to have to get same as original one.

if smaller -> more precise , more points else rough , fewer points.