opencv contours python code example

Example: how to draw contours in python opencv

import cv2
# Load an image
image = cv2.imread(“path to image file)
# Changing the colour-space
LUV = cv2.cvtColor(image, cv2.COLOR_BGR2LUV)
# Find edges
edges = cv2.Canny(LUV, 10, 100)
# Find Contours
contours, hierarchy = cv2.findContours(edges,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
# Find Number of contours
print("Number of Contours is: " + str(len(contours)))
# Draw yellow border around two contours
cv2.drawContours(image, contours, 0, (0, 230, 255), 6)
cv2.drawContours(image, contours, 2, (0, 230, 255), 6)
# Show the image with contours
cv2.imshow('Contours', image)
cv2.waitKey(0)