-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcanny_edge_detection.py
More file actions
56 lines (38 loc) · 1.24 KB
/
canny_edge_detection.py
File metadata and controls
56 lines (38 loc) · 1.24 KB
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
import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('test_images/smarties.png', 0)
## Canny Edge Detection : It is multi-stage algorithm useful for wide range of edge detection.
## Total 5 Steps :
# (1) Noise Reduction
# (2) Gradient Calculation
# (3) Non-maximum supression
# (4) Double threshold
# (5) Edge Tracking by hysteresis
## All things will be done by using one simple function frmo OpenCV
### Using Matplotlib ###
canny = cv2.Canny(img ,100 ,200)
titles = ['Main image', 'Canny']
images = [img , canny]
for i in range(len(images)):
plt.subplot(1,2,i+1)
plt.imshow(images[i] , 'gray')
plt.title(titles[i])
plt.xticks([]) , plt.yticks([])
plt.show()
### Using Trackbar ###
# def nothing(x):
# pass
# cv2.namedWindow('threshold meter')
# cv2.createTrackbar('th1' , 'threshold meter' , 0 ,255 , nothing)
# cv2.createTrackbar('th2' , 'threshold meter', 0 , 255 , nothing)
# cv2.imshow('Image' , img)
# while True:
# th1 = cv2.getTrackbarPos('th1','threshold meter')
# th2 = cv2.getTrackbarPos('th2','threshold meter')
# canny = cv2.Canny(img , th1 , th2 )
# cv2.imshow('canny' , canny)
# key = cv2.waitKey(1)
# if key == 27:
# break
# cv2.destroyAllWindows()