You can download this code by clicking the button below.
This code is now available for download.
This function uses the Canny edge detection algorithm from the OpenCV library to detect edges in an image. It first reads the image, converts it to a grayscale image, applies Gaussian blur to reduce noise, and finally detects edges using the Canny algorithm.
Technology Stack : OpenCV, NumPy
Code Type : Image processing
Code Difficulty : Intermediate
def edge_detection(image_path, threshold=50):
"""
Detects edges in an image using the Canny edge detection algorithm.
"""
import cv2
import numpy as np
# Read the image
image = cv2.imread(image_path)
# Convert the image to grayscale
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Apply Gaussian blur to reduce noise
blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)
# Detect edges using Canny
edges = cv2.Canny(blurred_image, threshold, threshold * 2)
return edges
# JSON representation of the code