Home Contours
Post
Cancel

Contours

What is Contours?

Contours are continous lines or curves that bound or cover the full boundary of an object. It can be used as
cv2.findContours(image, mode, method) -> contours, hierarchy where

  • image = input image.
  • mode = Contour retrieval mode. There is five algorithms in mode of the contour retrieval
    • RETR_EXTERNAL : Returns only extreme outer flags. All child contours are left behind.
    • RETR_LIST : Retrieves all the contours, but doesn’t create any parent-child relationship. Parents and kids are equal under this rule, and they are just contours.
    • RETR_CCOMP : Retrieves all the contours and arranges them to a 2-level hierarchy.
      For example, external contours of the object (its boundary) are placed in hierarchy-1. And the contours of holes inside object (if any) is placed in hierarchy-2. If any object inside it, its contour is placed again in hierarchy-1 only. And its hole in hierarchy-2 and so on.
    • RETR_TREE : Retrieves all the contours and creates a full family hierarchy list.
    • RETR_FLOODFILL

  • method = Contour approximation method. There are 4 algorithms in the contour approximation method.
    • CHAIN_APPROX_NONE : Stores absolutely all the contour points.
    • CHAIN_APPROX_SIMPLE : Compresses horizontal, vertical, and diagonal segments and leaves only their end points.
    • CHAIN_APPROX_TC89_L1 : Applies one of the flavors of the Teh-Chin chain approximation algorithm
    • CHAIN_APPROX_TC89_KCOS : Applies one of the flavors of the Teh-Chin chain approximation algorithm

  • contours = Each contour is stored as a vector of points.
  • hierarchy = Optional output vector containing information about the image topology. It has as many elements as the number of contours.

And through that contours, we can draw contours by:
drawContours(image, contours, contouridx, color, thickness)

  • image : Destination image.
  • contours : All the input contours.
  • contouridx : Parameter indicating a contour to draw. If it is negative, all the contours are drawn.
  • color : Color of the contours.
  • thickness : Thickness of lines the contours are drawn with. If it is negative, the contour interiors are drawn.



Steps of Contouring



  1. Grayscale
  2. Threshold or Canny Edge Detection to Binarize image.





How to Matching Contours

Matching Contours can be done by cv2.matchShapes(contour1, contour2, method, parameter) where

  • contour1, contour2 : The individual contour we are checking against.
  • method : Types of contour matching (1, 2, 3)
  • parameter : not supported now.



Types of contour matching



  • CONTOURS_MATCH_I1(1) : $I_{1}(A,B)\ =\ \sum_{i=1 \ldots 7}\frac{1}{m_{i}^{A}}\ -\ \frac{1}{m_{i}^{B}}$
  • CONTOURS_MATCH_I2(2) : $I_{2}(A,B)\ =\ \sum_{i=1 \ldots 7}m_{i}^{A}\ -\ m_{i}^{B}$
  • CONTOURS_MATCH_I3(3) : $I_{3}(A,B)\ =\ max_{i=1 \ldots 7}\frac{m_{i}^{A}\ -\ m_{i}^{B}}{m_{i}^{A}}$


where
$ M_{i}^{A}\ = sin(h_{i}^{A})\ *\ logh_{i}^{A} \ M_{i}^{B}\ = sin(h_{i}^{B})\ *\ logh_{i}^{B}$
and $ h_{i}^{A},\ h_{i}^{B} $ are the Hu moments of A and B.



Transforming perspective

We can transform perspective through contours that we extracted.



Steps of transforming perspective.



  1. Approxiamte our contour above to just 4 points using approxPolyDP
  2. Use getPerspectiveTransform() and warpPerspective() to change perspective.

Then, let’s see how to use both methods above.

  • cv2.getPerspectiveTransform(src, dst) : Calculates a perspective transform from four pairs of the corresponding points. where
    • src : Coordinates of quadrangle vertices in the source image.
    • dst : Coordinates of the corresponding quadrangle vertices in the destination image.
  • cv2.warpPerspective(src, M, dsize) : Applies a perspective transformation to an image. where
    • src : Input image.
    • M : 3×3 transformation matrix.
    • dsize : Size of the output image.





Implementation

This post is licensed under CC BY 4.0 by the author.