Skip to content

Commit 65cd7b5

Browse files
committed
added edge detection tutorial
1 parent ad40682 commit 65cd7b5

File tree

6 files changed

+53
-0
lines changed

6 files changed

+53
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
2828
- [Building a Speech Emotion Recognizer using Scikit-learn](https://www.thepythoncode.com/article/building-a-speech-emotion-recognizer-using-sklearn). ([code](machine-learning/speech-emotion-recognition))
2929
- [How to Make an Image Classifier in Python using Keras](https://www.thepythoncode.com/article/image-classification-keras-python). ([code](machine-learning/image-classifier))
3030
- [How to Use Transfer Learning for Image Classification using Keras in Python](https://www.thepythoncode.com/article/use-transfer-learning-for-image-flower-classification-keras-python). ([code](machine-learning/image-classifier-using-transfer-learning))
31+
- [How to Perform Edge Detection in Python using OpenCV](https://www.thepythoncode.com/article/canny-edge-detection-opencv-python). ([code](machine-learning/edge-detection))
3132
- [Top 8 Python Libraries For Data Scientists and Machine Learning Engineers](https://www.thepythoncode.com/article/top-python-libraries-for-data-scientists).
33+
3234

3335
- ### [General Python Topics](https://www.thepythoncode.com/topic/general-python-topics)
3436
- [How to Make Facebook Messenger bot in Python](https://www.thepythoncode.com/article/make-bot-fbchat-python). ([code](general/messenger-bot))
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# [How to Perform Edge Detection in Python using OpenCV](https://www.thepythoncode.com/article/canny-edge-detection-opencv-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
- To detect edges of a specific image (`little_flower.jpg`):
5+
```
6+
python edge_detector.py little_flower.jpg
7+
```
8+
- To detect edges live using your webcam:
9+
```
10+
python live_edge_detector.py
11+
```
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import cv2
2+
import numpy as np
3+
import matplotlib.pyplot as plt
4+
import sys
5+
6+
# read the image
7+
image = cv2.imread(sys.argv[1])
8+
9+
# convert it to grayscale
10+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
11+
12+
# show the grayscale image, if you want to show, uncomment 2 below lines
13+
# plt.imshow(gray, cmap="gray")
14+
# plt.show()
15+
16+
# perform the canny edge detector to detect image edges
17+
edges = cv2.Canny(gray, threshold1=30, threshold2=100)
18+
19+
# show the detected edges
20+
plt.imshow(edges, cmap="gray")
21+
plt.show()
Loading
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import numpy as np
2+
import cv2
3+
4+
cap = cv2.VideoCapture(0)
5+
6+
while True:
7+
_, frame = cap.read()
8+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
9+
edges = cv2.Canny(gray, 30, 100)
10+
cv2.imshow("edges", edges)
11+
cv2.imshow("gray", gray)
12+
if cv2.waitKey(1) == ord("q"):
13+
break
14+
15+
cap.release()
16+
cv2.destroyAllWindows()
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
opencv-python
2+
numpy
3+
matplotlib

0 commit comments

Comments
 (0)