|
| 1 | +# Image Edge Detection - A simple image edge detection program |
| 2 | +# Copyright (c) 2024 Ercan Ersoy |
| 3 | +# This file licensed under MIT License. |
| 4 | +# Write this code using ChatGPT and GitHub CoPilot. |
| 5 | + |
| 6 | +# Imports |
| 7 | +import cv2 |
| 8 | +import numpy as np |
| 9 | +import sys |
| 10 | + |
| 11 | +# Initialize video capture |
| 12 | +capture = cv2.VideoCapture(0) |
| 13 | + |
| 14 | +# Check if the webcam is opened correctly |
| 15 | +if not capture.isOpened(): |
| 16 | + # Print error message |
| 17 | + print("Error: Could not open webcam.", file=sys.stderr) |
| 18 | + |
| 19 | + # Exit the program |
| 20 | + exit() |
| 21 | + |
| 22 | +ret, image = capture.read() |
| 23 | + |
| 24 | +# If frame is not read correctly |
| 25 | +if not ret: |
| 26 | + # Print error message |
| 27 | + print("Error: Failed to capture frame.", file=sys.stderr) |
| 28 | + |
| 29 | + # Exit the program |
| 30 | + exit() |
| 31 | + |
| 32 | +# Convert the image to grayscale |
| 33 | +image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) |
| 34 | + |
| 35 | +# Apply median blur to the image |
| 36 | +image = cv2.medianBlur(image, 5) |
| 37 | + |
| 38 | +# Define a kernel |
| 39 | +kernel = np.array([[-1, -1, -1], |
| 40 | + [-1, 9, -1], |
| 41 | + [-1, -1, -1]]) |
| 42 | + |
| 43 | +# Apply the kernel to the image |
| 44 | +image = cv2.filter2D(image, -1, kernel) |
| 45 | + |
| 46 | +# Apply Gaussian blur to the image |
| 47 | +image = cv2.GaussianBlur(image, (5, 5), 0) |
| 48 | + |
| 49 | +# Apply Canny edge detection to the image |
| 50 | +image = cv2.Canny(image, 100, 200) |
| 51 | + |
| 52 | +# Save the image |
| 53 | +cv2.imwrite("image.jpg", image) |
0 commit comments