Realtime Edge Detection Using OpenCV in Python | Sharp edge detection method

Steps to download requirements are below:

  1. Download Python 2.7.x, numpy, and OpenCV 2.7.x or 3.1.0. Check if your Windows 32-bit or 64-bit is compatible and install accordingly.
  2. Make sure numpy is running in your python, then try installing opencv.

Implementation

# OpenCV realtime edge detection
# import of Python OpenCV libraries
# where its functionality is located

import cv2 

  
# np is an alias pointing to an empty library

import numpy as np

 

 
# capture frames from the camera

cap = cv2.VideoCapture ( 0 )

  

  
# loop starts if capture was initiated

while ( 1 ):

 

  # reads frames from the camera

ret, frame = cap.read ()

 

# convert BGR to HSV

  hsv = cv2.cvtColor (frame, cv2.COLOR_BGR2HSV )

 

# define a red range in HSV

  lower_red = np.array ([ 30 , 150 , 50 ])

upper_red = np.array ([ 255 , 255 , 180 ])

 

# create a red HSV color border and

# HSV threshold image

mask = cv2.inRange (hsv, lower_red, upper_red)

 

# Bitmask and original image

res = cv2.bitwise_and (frame, frame, mask = mask)

 

# Show original image

  cv2.imshow ( `Original` , frame)

 

# finds edges in input image and

# marks them on the output edges of the map

edges = cv2.Canny (frame, 100 , 200 )

 

# Show borders in frame

cv2.imshow ( `Edges` , edges)

 < / code> 

# Wait for the Esc key to stop

k = cv2.waitKey ( 5 ) & amp;  0xFF

if k = = 27 :

break

 

 
# Close the window
cap.release ()

 
# Deallocate any associated memory usage
cv2.destroyAllWindows () 

Output:

Links :

  • http://docs.opencv.org/2.4/doc/tutorials/imgproc /imgtrans/canny_detector/canny_detector.html
  • http://docs.opencv.org/trunk/da/d22/tutorial_py_canny.html
  • https://en.wikipedia.org / wiki / Canny_edge_detector
  • http://www.ijcsmc.com/docs/papers/July2013/V2I7201329.pdf

This article is courtesy of Afzalom Ansari . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

Please post comments if you find anything wrong or if you would like to share more information on the topic discussed above.