Contour Detection
import cv2 as cv
import numpy as np
# Reading an image
img = cv.imread('Photos/cats.jpg')
cv.imshow('Cats', img)
# Blank image
blank = np.zeros(img.shape, dtype='uint8')
cv.imshow('Blank', blank)
# Converting to grayscale
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow('Gray', gray)
# Blur
blur = cv.GaussianBlur(gray, (5,5), cv.BORDER_DEFAULT)
cv.imshow('Blur', blur)
# Edge cascade
canny = cv.Canny(blur, 125, 175)
cv.imshow('Canny Edges', canny)
# Contours Detection
# 1. Threshhold: Convert an image to a binary form
ret, thresh = cv.threshold(gray, 125, 255, cv.THRESH_BINARY)
cv.imshow('Thresh', thresh)
# 2. Contours
contours, hierarchies = cv.findContours(thresh, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
print(f'{len(contours)} contour(s) found!')
# Draw contours on the blank image
cv.drawContours(blank, contours, -1, (255,125,0), 1)
cv.imshow('Coutours Drawn', blank)
cv.waitKey(0)