Skip to content

Commit afb75a5

Browse files
committed
Some updates
* Add "Image Edge Detection" example * Update "Video Capture" example
1 parent 6df5751 commit afb75a5

File tree

5 files changed

+69
-4
lines changed

5 files changed

+69
-4
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ These files licensed by MIT License.
88

99
* [Circle Calculator](circle-calculator)
1010
* [Hello World](hello-world)
11+
* [Image Edge Detection](image-edge-detection)
1112
* [Number Guess Game](number-guess-game)
1213
* [Random Number Generator](random-number-generator)
1314
* [Rock Paper Scissors](rock-paper-scissors)

image-edge-detection/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
image.jpg

image-edge-detection/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Image Edge Detection
2+
3+
An simple image edge detection program
4+
Copyright (c) 2024 Ercan Ersoy
5+
6+
This example detects edges and captures an image from first webcam.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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)

video-capture/video-capture.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
# video Capture - An simple video capture program
1+
# Video Capture - An simple video capture program
22
# Copyright (c) 2024 Ercan Ersoy
33
# This file licensed under MIT License.
4-
# Write this code using ChatGPT.
4+
# Write this code using ChatGPT and GitHub CoPilot.
55

66
# Imports
77
import cv2
@@ -12,22 +12,26 @@
1212

1313
# Check if the webcam is opened correctly
1414
if not capture.isOpened():
15+
# Print error message
1516
print("Error: Could not open webcam.", file=sys.stderr)
17+
18+
# Exit the program
1619
exit()
1720

1821
# Continuously capture frames
1922
while True:
2023
# Read the frame
2124
ret, frame = capture.read()
2225

23-
# If frame is read correctly ret is True
26+
# If frame is not read correctly
2427
if not ret:
2528
print("Error: Failed to capture frame.", file=sys.stderr)
26-
break
29+
exit()
2730

2831
# Display the frame
2932
cv2.imshow('Frame', frame)
3033

34+
# Wait for a key press
3135
key_code = cv2.waitKey(1)
3236

3337
# Break the loop if ESC is pressed

0 commit comments

Comments
 (0)