Canny() Function in OpenCV Python

This exercising shows how to use the OpenCV cv2.Canny() feature for aspect detection in an picture. In particular, Edge detection is a common step in lots of pc vision packages; indeed, it facilitates to discover obstacles, objects and structures in the photo. To begin, the procedure will, first load an image, convert it to grayscale, use the Canny aspect detection algorithm, and show the results in a window, Thus, this method describes primary photograph processing strategies and emphasizes the importance of thresholds in facet detection.

To detect and display edges in an image using the Canny edge detection algorithm in OpenCV

Let’s learn this with simple example

Step 1: Import OpenCV Library

Firstly, this step imports the OpenCV library, that’s important for appearing photo processing duties. The library provides function for loading, remodeling, and reading images, along with side detection using the Canny set of rules.

import cv2

Step 2: Load the input image

Next, the application reads an photo document using cv2.Imread(). This feature hundreds the photograph into memory in its default BGR (Blue, Green, Red) format. Thus, the image serves as the input for the subsequent aspect detection procedure.

image = cv2.imread('example.jpg')

Step 3: Convert the image to grayscale 

The input picture transforms from BGR color format to grayscale using cv2.cvtColor(). This transformation simplifies the photograph information by specializing in depth tiers, as side detection requires a unmarried-channel photograph to perceive brightness adjustments.

gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

Step 4: Apply Canny edge detection

Then the grayscale image undergoes side detection the usage of the cv2.Canny() feature. This algorithm identifies big adjustments in pixel intensity, representing edges inside the photograph. The algorithm uses two thresholds to decide which modifications qualify as edges, prioritizing stronger gradients.

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

Step 5: Display the edge-detected image

Afterwards, the edge-detected image is displayed in a new window called “Edges”. This step is important as it allows the user to view the results of the edge detection process.

cv2.imshow('Edges', edges)

Step 6: Wait for user interaction

Then the software pauses with cv2.WaitKey(0), waiting indefinitely till the consumer presses a key. As a result, this guarantees the window showing the brink-detected image stays open for inspection.

cv2.waitKey(0)

Step 7: Close all OpenCV windows

Finally, cv2.DestroyAllWindows() is known as to close all OpenCV-created windows. This step cleans up assets and ensures the program terminates properly without leaving putting windows.

cv2.destroyAllWindows()
Output:
The code provided doesn't directly generate output in text form but displays a cropped section of the image in a pop-up window using OpenCV

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top