Make sure we are in our home directory
1 2 3 |
[code lang="bash"] cd ~ [/code] |
Create a new directory for this exercise
1 2 3 4 |
[code lang="bash"] mkdir pytest cd pytest [/code] |
Download the image below to this new directory
# import the necessary packages
import
numpy
as
np
import
cv2
# load the games image
image
=
cv2
.
imread
(
"test.png"
)
# find the red color game in the image
upper
=
np
.
array
(
[
65
,
65
,
255
]
)
lower
=
np
.
array
(
[
0
,
0
,
200
]
)
mask
=
cv2
.
inRange
(
image
,
lower
,
upper
)
# find contours in the masked image and keep the largest one
(
_
,
cnts
,
_
)
=
cv2
.
findContours
(
mask
.
copy
(
)
,
cv2
.
RETR_EXTERNAL
,
cv2
.
CHAIN_APPROX_SIMPLE
)
c
=
max
(
cnts
,
key
=
cv2
.
contourArea
)
# approximate the contour
peri
=
cv2
.
arcLength
(
c
,
True
)
approx
=
cv2
.
approxPolyDP
(
c
,
0.05
*
peri
,
True
)
# draw a green bounding box surrounding the red game
cv2
.
drawContours
(
image
,
[
approx
]
,
-
1
,
(
0
,
255
,
0
)
,
4
)
cv2
.
imshow
(
"Image"
,
image
)
cv2
.
waitKey
(
0
)
Paste the code into a file called pytest.py
1 2 3 |
[code lang="bash"] vi pytest.py [/code] |
Paste then save and exit vi
Run the python program
1 2 3 |
[code lang="python"] python pytest.py [/code] |
Now it should make a green square around the red logo box.
The exercise is to make it make the square around the blue logo box (middle one).
Email me back the result