-
Notifications
You must be signed in to change notification settings - Fork 0
/
contours.py
41 lines (26 loc) · 1.18 KB
/
contours.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
36
37
38
39
40
41
# Contours are defined as the line joining all the points along the boundary
# of an image that are having the same intensity. Contours come handy in
# shape analysis, finding the size of the object of interest, and object detection.
# Contour is the edge closing an object. So you can think as higher level of edge detection.
# So if an edge define an object it becomes a contour.
import cv2 as cv
import numpy as np
img = cv.imread('./Resources/Photos/cats.jpg')
cv.imshow("cat", img)
blank = np.zeros(img.shape, dtype="uint8")
cv.imshow("blank", blank)
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.imshow("gray", gray)
blur = cv.GaussianBlur(gray, (5,5), cv.BORDER_DEFAULT)
cv.imshow("blur", blur)
canny = cv.Canny(blur, 125, 175)
cv.imshow("canny Edges", canny)
# # instead of blur and canny we can use threshold, it binarilize the image
# but first check the canny
# ret, thresh = cv.threshold(gray, 125, 255, cv.THRESH_BINARY)
# cv.imshow("thresh", thresh)
contours, hierarchies = 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), 2)
cv.imshow("Contours Drawn", blank)
cv.waitKey(0)