Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit070ab98

Browse files
committed
added contour detection tutorial
1 parent060f569 commit070ab98

File tree

7 files changed

+68
-0
lines changed

7 files changed

+68
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
2929
-[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))
3030
-[How to Perform Edge Detection in Python using OpenCV](https://www.thepythoncode.com/article/canny-edge-detection-opencv-python). ([code](machine-learning/edge-detection))
3131
-[How to Detect Shapes in Images in Python](https://www.thepythoncode.com/article/detect-shapes-hough-transform-opencv-python). ([code](machine-learning/shape-detection))
32+
-[How to Detect Contours in Images using OpenCV in Python](https://www.thepythoncode.com/article/contour-detection-opencv-python). ([code](machine-learning/contour-detection))
3233
-[How to Recognize Optical Characters in Images in Python](https://www.thepythoncode.com/article/optical-character-recognition-pytesseract-python). ([code](machine-learning/optical-character-recognition))
3334
-[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))
3435
-[How to Convert Speech to Text in Python](https://www.thepythoncode.com/article/using-speech-recognition-to-convert-speech-to-text-python). ([code](machine-learning/speech-recognition))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#[How to Detect Contours in Images using OpenCV in Python](https://www.thepythoncode.com/article/contour-detection-opencv-python)
2+
To run this:
3+
-`pip3 install -r requirements.txt`
4+
- To detect contour of the the image`thumbs_up_down.jpg` using 225 value of binary threshold conversion:
5+
```
6+
python contour-detector.py thumbs_up_down.jpg 225
7+
```
8+
- For live detection, consider using `live-contour-detector.py` script.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
importcv2
2+
importmatplotlib.pyplotasplt
3+
importsys
4+
5+
# read the image
6+
image=cv2.imread(sys.argv[1])
7+
8+
# convert to RGB
9+
image=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
10+
11+
# convert to grayscale
12+
gray=cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
13+
14+
# create a binary thresholded image
15+
_,binary=cv2.threshold(gray,sys.argv[2],255,cv2.THRESH_BINARY_INV)
16+
# show it
17+
plt.imshow(binary,cmap="gray")
18+
plt.show()
19+
20+
# find the contours from the thresholded image
21+
contours,hierarchy=cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
22+
23+
# draw all contours
24+
image=cv2.drawContours(image,contours,-1, (0,255,0),2)
25+
26+
# show the image with the drawn contours
27+
plt.imshow(image)
28+
plt.show()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
importcv2
2+
3+
cap=cv2.VideoCapture(0)
4+
5+
whileTrue:
6+
_,frame=cap.read()
7+
8+
# convert to grayscale
9+
gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
10+
11+
# create a binary thresholded image
12+
_,binary=cv2.threshold(gray,255//2,255,cv2.THRESH_BINARY_INV)
13+
14+
# find the contours from the thresholded image
15+
contours,hierarchy=cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
16+
17+
# draw all contours
18+
image=cv2.drawContours(frame,contours,-1, (0,255,0),2)
19+
20+
# show the images
21+
cv2.imshow("gray",gray)
22+
cv2.imshow("image",image)
23+
cv2.imshow("binary",binary)
24+
25+
ifcv2.waitKey(1)==ord("q"):
26+
break
27+
28+
cap.release()
29+
cv2.destroyAllWindows()
61.1 KB
Loading
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
opencv-python
2+
matplotlib
Loading

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp