TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

OpenCV Canny Edge Detection

OpenCV Canny Edge Detection with What is OpenCV, History, Installation, Reading Images, Writing Images, Resize Image, Image Rotation, Gaussian Blur, Blob Detection, Face Detection and Face Recognition etc.

<< Back to OPENCV

OpenCV Canny Edge Detection

Edge detection is term where identify the boundary of object in image. We will learn about the edge detection using the canny edge detection technique. The syntax is canny edge detection function is given as:

edges = cv2.Canny('/path/to/img', minVal, maxVal, apertureSize, L2gradient)

Parameters-

  • /path/to/img: file path of the image (required)
  • minVal: Minimum intensity gradient (required)
  • maxVal: Maximum intensity gradient (required)
  • aperture: It is optional argument.
  • L2gradient: Its default value is false, if value is true, Canny () uses a more computationally expensive equation to detect edges, which provides more accuracy at the cost of resources.

Example: 1

import cv2
img = cv2.imread(r'C:\Users\DEVANSH SHARMA\cat_16x9.jpg')
edges = cv2.Canny(img, 100, 200)

cv2.imshow("Edge Detected Image", edges)
cv2.imshow("Original Image", img)
cv2.waitKey(0)  # waits until a key is pressed
cv2.destroyAllWindows()  # destroys the window showing image

Output:

OpenCV Canny Edge detection

Example: Real Time Edge detection

# import libraries of python OpenCV  
import cv2

# import Numpy by alias name np
import numpy as np

# capture frames from a camera 
cap = cv2.VideoCapture(0)

# loop runs if capturing has been initialized 
while (1):

    # reads frames from a camera 
    ret, frame = cap.read()

    # converting BGR to HSV 
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    # define range of red color in HSV 
    lower_red = np.array([30, 150, 50])
    upper_red = np.array([255, 255, 180])

    # create a red HSV colour boundary and  
    # threshold HSV image 
    mask = cv2.inRange(hsv, lower_red, upper_red)

    # Bitwise-AND mask and original image 
    res = cv2.bitwise_and(frame, frame, mask=mask)

    # Display an original image 
    cv2.imshow('Original', frame)

    # discovers edges in the input image image and 
    # marks them in the output map edges 
    edges = cv2.Canny(frame, 100, 200)

    # Display edges in a frame 
    cv2.imshow('Edges', edges)

    # Wait for Esc key to stop 
    k = cv2.waitKey(5) & 0xFF
    if k == 27:
        break

# Close the window 
cap.release()

# De-allocate any associated memory usage 
cv2.destroyAllWindows()

Output:

OpenCV Canny Edge detection
Next TopicOpenCV Blur




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf