Herunterladen Diese Seite drucken

SunFounder PiCar-X Kit Bedienungsanleitung Seite 72

Werbung

SunFounder PiCar-X Kit
# Find the contour in morphologyEx_img, and the contours are arranged according
the area from small to large.
_tuple
=
cv2.findContours(morphologyEx_img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
# compatible with opencv3.x and openc4.x
if
len(_tuple)
==
_, contours, hierarchy
else:
contours, hierarchy
color_area_num
=
if
color_area_num
for
in
i
contours:
x,y,w,h
coordinates of the upper left corner and the width and height of the recognition object
# Draw a rectangle on the image (picture, upper left corner
lower right corner coordinate, color, line width)
if
w
>=
original size, if you want to draw a rectangle on the original picture to circle
target, you have to multiply x, y, w, h by 4.
x
=
y
=
w
=
h
=
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
frame
cv2.putText(img,color_type,(x,y), cv2.FONT_HERSHEY_SIMPLEX, 1,(0,0,255),
2)# Add character description
return
img,mask,morphologyEx_img
Die img, mask und morphologyEx_img werden in drei Fenstern angezeigt, um die Verarbeitungsergebnisse jedes
Schritts direkt zu beobachten.
68
3:
=
_tuple
=
_tuple
len(contours)
# Count the number of contours
>
0:
# Traverse all contours
=
cv2.boundingRect(i)
8
and
h
>=
8:
# Because the picture is reduced to a quarter of
x
*
4
y
*
4
w
*
4
h
*
4
# Decompose the contour into
# Draw a
Kapitel 4. Mit Python spielen
(Fortsetzung der vorherigen Seite)
to␣
the␣
coordinate,␣
the␣
the␣
rectangular␣

Werbung

loading