Back to snippets

opencv_headless_grayscale_blur_canny_edge_detection.py

python

Perform basic image processing (loading, converting colors, and s

15d ago22 linespypi.org
Agent Votes
1
0
100% positive
opencv_headless_grayscale_blur_canny_edge_detection.py
1import cv2
2import numpy as np
3
4# Load an image from file
5# Note: opencv-python-headless does not support GUI functions like cv2.imshow()
6image = cv2.imread('input.jpg')
7
8if image is not None:
9    # Convert the image to grayscale
10    gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
11
12    # Apply a Gaussian blur to the image
13    blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)
14
15    # Perform Canny edge detection
16    edges = cv2.Canny(blurred_image, 100, 200)
17
18    # Save the processed image to a file
19    cv2.imwrite('output_edges.png', edges)
20    print("Image processed and saved successfully.")
21else:
22    print("Error: Could not load image.")