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