无法检测到不超过一种颜色的轮廓

时间:2019-07-01 11:18:07

标签: python opencv image-processing

我正在使用opencv和python使用颜色检测来进行手势识别。我遇到的问题是仅检测到一种颜色(例如红色)的轮廓,但未检测到其他两种颜色(例如绿色或蓝色),因此我的某些手势不起作用。

在我的代码中,仅检测到红色轮廓而未检测到蓝色

代码:

 import cv2
 import numpy as np
 from pynput.mouse import Button, Controller
 import wx
 import time
 import pyautogui

 mouse = Controller()
 app = wx.App(False)
 (sx, sy) = wx.GetDisplaySize()(camx, camy) = (320, 240)
##green
 lowerBoundG = np.array ([65, 66, 77])
 upperBoundG = np.array ([96, 255, 255])
##red
 lowerBoundR = np.array ([134, 94, 98])
 upperBoundR = np.array ([179, 255, 255])
##blue
 lowerBoundB = np.array ([88, 78, 20])
 upperBoundB = np.array ([128, 255, 255])
 cam = cv2.VideoCapture (0)
 cam.set (3, camx)
 cam.set (4, camy)
 kernelOpen = np.ones ((5, 5))
 kernelClose = np.ones ((20, 20))
 mLocOld = np.array ([0, 0])
 mouseLoc = np.array ([0, 0]) DampingFactor = 3 while True
:
 ret, uf = cam.read()
 img = cv2.flip (uf, 1)
#convert BGR to HSV
 imgHSV = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
#create the Mask
  maskG = cv2.inRange (imgHSV, lowerBoundG, upperBoundG)
  maskR = cv2.inRange (imgHSV, lowerBoundR, upperBoundR)
  maskB = cv2.inRange (imgHSV, lowerBoundR, upperBoundB)
#morphology
  maskOpenG = cv2.morphologyEx(maskG, cv2.MORPH_OPEN, kernelOpen)
  maskCloseG = cv2.morphologyEx(maskOpenG, cv2.MORPH_CLOSE, kernelClose)
  maskOpenR = cv2.morphologyEx(maskR, cv2.MORPH_OPEN, kernelOpen)
  maskCloseR = cv2.morphologyEx(maskOpenR, cv2.MORPH_CLOSE, kernelClose)
  maskOpenB = cv2.morphologyEx(maskB, cv2.MORPH_OPEN, kernelOpen)
  maskCloseB = cv2.morphologyEx(maskOpenB, cv2.MORPH_CLOSE, kernelClose)
  maskFinalG = maskCloseG
  maskFinalR = maskCloseR
  maskFinalB = maskCloseB
  contsG, h = cv2.findContours(maskFinalG.copy(), cv2.RETR_EXTERNAL,
             cv2.CHAIN_APPROX_NONE) 
  contsR, v = cv2.findContours (maskFinalR.copy (), cv2.RETR_EXTERNAL,
             cv2.CHAIN_APPROX_NONE) 
  contsB, p = cv2.findContours (maskFinalB.copy (), cv2.RETR_EXTERNAL,
             cv2.CHAIN_APPROX_NONE)
##for COntrolling mouse cursor
  if (len (contsR) == 1):
     x, y, w, h = cv2.boundingRect(contsR[0])
     cv2.rectangle (img, (x, y), (x + w, y + h), (255, 0, 0), 2)
     cx = int (x + w / 2)
     cy = int (y + h / 2)
     div = int (w + h / 4)
     cv2.circle (img, (cx, cy), div, (0, 0, 255), 2)
     mouseLoc = mLocOld + ((cx, cy) - mLocOld) / DampingFactor
     mouse.position = (mouseLoc[0] * sx / camx, mouseLoc[1] * sy / camy)
     mLocOld = mouseLoc
##For Mouse right click
  elif (len (contsR) == 1 and len (contsB) == 1):
     mouse.click (Button.right, 1) time.sleep (1) cv2.imshow ("cam", img)
     ##cv2.imshow("mask",maskClose)
 cv2.waitKey (5)

1 个答案:

答案 0 :(得分:2)

您的代码说:

##for COntrolling mouse cursor
 if (len (contsR) == 1):
 # [.....]
##For Mouse right click
  elif (len (contsR) == 1 and len (contsB) == 1):

如果红色具有任何值,则将永远不会达到elif,因为执行了if

相反,请先在更多条件下尝试该情况:

##For Mouse right click
 if (len (contsR) == 1 and len (contsB) == 1):
 # [.....]
##for COntrolling mouse cursor
  elif (len (contsR) == 1):

因此,如果有红色和蓝色,请单击鼠标。如果没有蓝色,但是有红色,请移动光标。