Working with curves - approximation, length, and area
This recipe covers OpenCV functionality related to, features of curves. We will review the routines for computing a curve's length and area, getting the convex hull, and checking whether a curve is convex or not. Also, we will study how to approximate the contour with a smaller number of points. All of these things can be useful when you're developing an algorithm based on contour handling. By finding different features of the contour, you can build heuristics to filter out false contours. So, let's get started.
Getting ready
You need to have OpenCV 3.x installed, with Python API support.
How to do it...
- Import all of the necessary modules, open an image, and display it on the screen:
import cv2, random import numpy as np img = cv2.imread('bw.png', cv2.IMREAD_GRAYSCALE)
- Find the contours of the loaded image, draw them, and show the result:
im2, contours, hierarchy = cv2.findContours(img, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) color = cv2.cvtColor...