Canny Edge Detection 



OpenCV puts all the above in single function, cv.Canny(). We will see how to use it. The first argument is our input image. The second and third arguments are our minVal and maxVal respectively. The fourth argument is aperture_size. It is the size of the Sobel kernel used to find image gradients. By default, it is 3. Last argument is L2gradient which specifies the equation for finding gradient magnitude. If it is true, it uses the equation mentioned above which is more accurate, otherwise, it uses this function: 
Edge_Gradient(G)=|Gx|+|Gy|. By default, it is False.


This is widely used in Self Driving car projects to visualise the Lanes and author visuals



import cv2

cv2.namedWindow('window')

def nothing(x):
pass

cv2.createTrackbar('lower', 'window', 0, 255, nothing)
cv2.createTrackbar('upper', 'window', 0, 255, nothing)

#img = cv2.imread('test.jpg')

cap = cv2.VideoCapture(0)

#gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

while True:
_, img = cap.read()

img = cv2.blur(img, (3,3))

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

x = cv2.getTrackbarPos('lower', 'window')
y = cv2.getTrackbarPos('upper', 'window')

edge = cv2.Canny(gray, x, y)

cv2.imshow('window', edge)

if cv2.waitKey(1) == 27:
cv2.destroyAllWindows()
break


--------------------------------------------------------------------------------------------------------------------------------

go to GitHub Code

Comments

Popular Posts