所以我一直在尝试这段代码,检测到黄色科洛尔,然后打印它的坐标(请忽略led的部分,这只是我正在处理的示例的一部分,由于某些原因代码崩溃,因此无法删除) 。我的计划是添加另一种颜色,并确保当其中一种颜色可见时,程序将打印出颜色名称。
from __future__ import print_function
from imutils.video import VideoStream
import imutils
import time
import cv2
import os
import RPi.GPIO as GPIO
redLed = 21
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(redLed, GPIO.OUT)
def mapObjectPosition (x, y):
print ("[INFO] Object Center coordenates at X0 = {0} and Y0 =
{1}".format(x, y))
print("[INFO] waiting for camera to warmup...")
vs = VideoStream(0).start()
time.sleep(2.0)
colorLower = (24, 100, 100)
colorUpper = (44, 255, 255)
GPIO.output(redLed, GPIO.LOW)
ledOn = False
while True:
frame = vs.read()
frame = imutils.resize(frame, width=500)
#frame = imutils.rotate(frame, angle=180)
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(hsv, colorLower, colorUpper)
mask = cv2.erode(mask, None, iterations=2)
mask = cv2.dilate(mask, None, iterations=2)
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
center = None
if len(cnts) > 0:
c = max(cnts, key=cv2.contourArea)
((x, y), radius) = cv2.minEnclosingCircle(c)
M = cv2.moments(c)
center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))
if radius > 10:
cv2.circle(frame, (int(x), int(y)), int(radius),
(0, 255, 255), 2)
cv2.circle(frame, center, 5, (0, 0, 255), -1)
mapObjectPosition(int(x), int(y))
if not ledOn:
GPIO.output(redLed, GPIO.HIGH)
ledOn = True
elif ledOn:
GPIO.output(redLed, GPIO.LOW)
ledOn = False
cv2.imshow("Frame", frame)
# if [ESC] key is pressed, stop the loop
key = cv2.waitKey(1) & 0xFF
if key == 27:
break
print("\n [INFO] Exiting Program and cleanup stuff \n")
GPIO.cleanup()
cv2.destroyAllWindows()
vs.stop()
我的想法是添加例如绿色并像这样添加: colorLowerG =(50,100,100), colorUpperG =(70,255,255)
然后我知道我必须为其创建一个蒙版。问题是我在哪里添加一个变量,该变量将获得当时可见颜色的名称并将其打印出来。该代码也适用于一一显示的对象。