-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathcontours.py
28 lines (21 loc) · 834 Bytes
/
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
import cv2
import numpy as np
def thresh_callback(thresh):
edges = cv2.Canny(blur,thresh,thresh*2)
drawing = np.zeros(img.shape,np.uint8) # Image to draw the contours
contours,hierarchy = cv2.findContours(edges,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
for cnt in contours:
color = np.random.randint(0,255,(3)).tolist() # Select a random color
cv2.drawContours(drawing,[cnt],0,color,2)
cv2.imshow('output',drawing)
cv2.imshow('input',img)
img = cv2.imread('plate.png')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray,(5,5),0)
cv2.namedWindow('input',cv2.WINDOW_AUTOSIZE)
thresh = 100
max_thresh = 255
cv2.createTrackbar('canny thresh:','input',thresh,max_thresh,thresh_callback)
thresh_callback(thresh)
if cv2.waitKey(0) == 27:
cv2.destroyAllWindows()