Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/samples/python/edge.py
16337 views
1
#!/usr/bin/env python
2
3
'''
4
This sample demonstrates Canny edge detection.
5
6
Usage:
7
edge.py [<video source>]
8
9
Trackbars control edge thresholds.
10
11
'''
12
13
# Python 2/3 compatibility
14
from __future__ import print_function
15
16
import cv2 as cv
17
import numpy as np
18
19
# relative module
20
import video
21
22
# built-in module
23
import sys
24
25
26
if __name__ == '__main__':
27
print(__doc__)
28
29
try:
30
fn = sys.argv[1]
31
except:
32
fn = 0
33
34
def nothing(*arg):
35
pass
36
37
cv.namedWindow('edge')
38
cv.createTrackbar('thrs1', 'edge', 2000, 5000, nothing)
39
cv.createTrackbar('thrs2', 'edge', 4000, 5000, nothing)
40
41
cap = video.create_capture(fn)
42
while True:
43
flag, img = cap.read()
44
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
45
thrs1 = cv.getTrackbarPos('thrs1', 'edge')
46
thrs2 = cv.getTrackbarPos('thrs2', 'edge')
47
edge = cv.Canny(gray, thrs1, thrs2, apertureSize=5)
48
vis = img.copy()
49
vis = np.uint8(vis/2.)
50
vis[edge != 0] = (0, 255, 0)
51
cv.imshow('edge', vis)
52
ch = cv.waitKey(5)
53
if ch == 27:
54
break
55
cv.destroyAllWindows()
56
57