Herunterladen Diese Seite drucken

SunFounder PiCar-X Kit Bedienungsanleitung Seite 71

Werbung

cv2.imshow("morphologyEx_img", img_3)
rawCapture.truncate(0)
k
=
cv2.waitKey(1)
# 27 is the ESC key, which means that if you press the ESC key to exit
if
k
==
27:
break
print(
quit ...
cv2.destroyAllWindows()
camera.close()
Wie funktioniert es?
Zunächst wird der Bereich von H im
Farberkennung praktisch ist:
color_dict
=
{
red
:[0,4],
110],
purple
:[115,165],
Anschließend wird ein
Faltungskern
gesetzt wird.
kernel_5
=
np.ones((5,5),np.uint8)
Als nächstes führt die Funktion color_detect() die Bildverarbeitung in vier Schritten durch:
1. Extrahiert die Daten der Ziel-Farbe als neues Binärbild (Array).
2. Führt erweiterte morphologische Transformationen durch.
3. Findet Konturen in einem Binärbild.
4. Zeichnet einen Rahmen um das erkannte Objekt im Bild.
def
color_detect(img,color_name):
# The blue range will be different under different lighting conditions and can
adjusted flexibly.
resize_img
=
cv2.resize(img, (160,120), interpolation=cv2.INTER_LINEAR)
to reduce the amount of calculation, the size of the picture is reduced to (160,120)
hsv
=
cv2.cvtColor(resize_img, cv2.COLOR_BGR2HSV)
HSV
color_type
=
color_name
mask
=
cv2.inRange(hsv,np.array([min(color_dict[color_type]), 60, 60]),
array([max(color_dict[color_type]), 255, 255]) )
between lower/upper white, and the rest black
if
color_type
==
mask_2
=
][1],255,255))
mask
=
cv2.bitwise_or(mask, mask_2)
morphologyEx_img
# Perform an open operation on the image
4.8. Farberkennung
# Release cache
&
0xFF
)
HSV-Farbraum
orange
:[5,18],
red_2
:[165,180]}
der Größe 5x5 definiert, der für morphologische Operationen wie Filterung ein-
H: chroma, S: saturation v: lightness
red
:
cv2.inRange(hsv, (color_dict[
=
cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel_5,iterations=1)
# OpenCV image show
als Wörterbuch definiert, was für den folgenden Algorithmus zur
yellow
:[22,37],
red_2
][0],0,0), (color_dict[
SunFounder PiCar-X Kit
(Fortsetzung der vorherigen Seite)
green
:[42,85],
blue
# In
# Convert from BGR
np.
# inRange()Make the
(Fortsetzung auf der nächsten Seite)
:[92,
be␣
order␣
to␣
ones␣
red_2
67

Werbung

loading