Contours in Images
Making computer vision easy with Monk, low code Deep Learning tool and a unified wrapper for Computer Vision.
Contours
Why Contours?
Issues with edges
- Edges have no identity.
- Edges have no continuity.
- Edges cannot be easily counted and worked upon.
- Edges cannot be differentiated from one another.
- Edges in themselves have no defined shape.
Contours
- Contours are an outline representing or bounding the shape or form of something.
Contours in image processing
- Contours are designed using edges.
- They are edges with an identity, geometrical parameters and are continuous.
- They are useful for shape analysis and object recognition.
Contour detection using OpenCV
Retrieval methods
- CV_RETR_EXTERNAL gives “outer” contours, so if you have (say) one contour enclosing another (like concentric circles), only the outermost is given.
- CV_RETR_LIST gives all the contours and doesn’t even bother calculating the hierarchy — good if you only want the contours and don’t care whether one is nested inside another.
- CV_RETR_CCOMP gives contours and organizes them into outer and inner contours. Every contour is either the outline of an object or the outline of an object inside another object (i.e. hole). The hierarchy is adjusted accordingly. This can be useful if (say) you want to find all the holes.
- CV_RETR_TREE calculates the full hierarchy of the contours. So you can say that object1 is nested 4 levels deep within object2 and object3 is also nested 4 levels deep.
Contour properties
- Contour Moments
2. Contour Area -> Area Enclosed inside a closed contour in pixels
3. Contour Perimeter — Number of pixels a contour covers in pixels
4. Contour approximation — Approximating the shape of a contour from an irregular shape.
5. Convex Hull
6. Bounding rectangle, circle, ellipse fitting across a contour.
Contour Moments
- Moments are a set of statistical parameters to measure a distribution.
Three moments are commonly used:
1. Mean: the average.
2. Variance: an indication of how closely the values are spread about the mean.
3. Standard deviation: It is the square root of the variance.
Output
Number of contours = 262
Image contours
Image moments help you to calculate some features like the center of mass of the object, area of the object.
Contour Area
# Contour Area
cnt = contours[109]
area = cv2.contourArea(cnt)
print(area)
Output
6045.0
List of areas of all contours
lst_arr = [];
for cnt in contours:
lst_arr.append(cv2.contourArea(cnt))
print("Minimum area = {}".format(min(lst_arr)))
print("Maximum area = {}".format(max(lst_arr)))
Output
Minimum area = 0.0
Maximum area = 152881.0
Contour Perimeter
cnt = contours[109]
perimeter = cv2.arcLength(cnt, True) # True -> closed length
print(perimeter)
Output
326.48528122901917
List of perimeters of all contours
lst_per = [];
for cnt in contours:
lst_per.append(cv2.arcLength(cnt, True))
print("Minimum perimeter = {}".format(min(lst_per)))
print("Maximum perimeter = {}".format(max(lst_per)))
Output
Minimum perimeter = 0.0
Maximum perimeter = 1564.0
Contour Approximation based on length
Convex Hull
- The convex hull or convex envelope or convex closure of a set X points in the Euclidean plane or in a Euclidean space is the smallest convex set that contains X.
Bounding a contour
Straight rectangle
Rotated rectangle
Circular bounding
Elliptical bounding
You can find the complete jupyter notebook on Github.
If you have any questions, you can reach Abhishek and Akash. Feel free to reach out to them.
I am extremely passionate about computer vision and deep learning in general. I am an open-source contributor to Monk Libraries.
You can also see my other writings at: