-
Notifications
You must be signed in to change notification settings - Fork 0
/
contour.py
35 lines (24 loc) · 844 Bytes
/
contour.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import cv2 as cv
import numpy as np
#Contours are the boundaries of an object. They are useful in object detection and shape analysis.
img = cv.imread('Photos/cats.jpg')
cv.imshow('Cats', img)
blank = np.zeros(img.shape, dtype='uint8')
cv.imshow('Blank', blank)
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)
# #canny
canny = cv.Canny(blur, 125, 175)
cv.imshow('Canny Edges', canny)
#Thresholding
# ret, thresh = cv.threshold(blur, 125, 255, cv.THRESH_BINARY)
# cv.imshow('Thresh', thresh)
#Contours
contours, heirarchies = cv.findContours(canny, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE)
print(f'{len(contours)} contour(s) found!')
cv.drawContours(blank, contours, -1, (0,0,255), 1)
cv.imshow('Contours Drawn', blank)
cv.waitKey(0)