-
Notifications
You must be signed in to change notification settings - Fork 33
/
opencv_canny.py
86 lines (59 loc) · 2.12 KB
/
opencv_canny.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import numpy as np
import cv2
# get source webcam or video
#
#cap = cv2.VideoCapture(0)
cap = cv2.VideoCapture('weirdfaces.mov')
# Define the codec and create VideoWriter object
#fourcc = cv2.VideoWriter_fourcc(*'XVID')
#fourcc = cv2.VideoWriter_fourcc('m','p','4','v')
#out = cv2.VideoWriter('output.m4v',fourcc, 30.0, (1280,720))
# https://www.pyimagesearch.com/2015/04/06/zero-parameter-automatic-canny-edge-detection-with-python-and-opencv/
def auto_canny(image, sigma=0.33):
# compute the median of the single channel pixel intensities
v = np.median(image)
# apply automatic Canny edge detection using the computed median
lower = int(max(0, (1.0 - sigma) * v))
upper = int(min(255, (1.0 + sigma) * v))
edged = cv2.Canny(image, lower, upper)
# return the edged image
return edged
# COUNTING FRAME
count = 0
while(cap.isOpened()):
# Return frame by frame
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
# Convert grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Blur to reduce noise
blurred = cv2.GaussianBlur(gray, (3, 3), 0)
# apply Canny edge detection using a wide threshold, tight
# threshold, and automatically determined threshold
#wide = cv2.Canny(blurred, 10, 200)
#tight = cv2.Canny(blurred, 225, 250)
#auto = auto_canny(blurred)
#auto = auto_canny(blurred, sigma=0.69)
auto = cv2.Canny(blurred, 10, 50)
# WRITE JPEG
cv2.imwrite("frame%04d.jpg" % count, auto) # save frame as JPEG file
count += 1
#edges = cv2.Canny(gray, 10, 250)
#edges = cv2.Canny(gray,lower,upper)
# write the flipped frame
#out.write(frame)
#out.write(edges)
#cv2.imshow('frame',edges)
cv2.imshow('blur', auto)
#cv2.imshow("Edges", np.hstack([wide, tight, auto]))
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# cv2.imshow('frame',edges)
# if cv2.waitKey(1) & 0xFF == ord('q'):
# break
cap.release()
#out.release()
cv2.destroyAllWindows()