手势控制的鼠标指针无法识别
Gesture controlled mouse pointer not recognizing
我正在尝试创建一个基于手势的鼠标指针,一切正常,只是相机无法识别粘在我手指上的红色框,因此我的鼠标无法正常工作。
我试图找出问题所在,但它完全在逃避我。我在互联网上浏览过各种类似的代码,但其中大部分都运行良好,或者与我所做的完全不同。
矩形的代码是:
while True:
ret, img = cam.read()
#convert BGR to HSV
imgHSV= cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
# create the Mask
mask=cv2.inRange(imgHSV,lowerBound,upperBound)
#morphology
maskOpen=cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernelOpen)
maskClose=cv2.morphologyEx(maskOpen,cv2.MORPH_CLOSE,kernelClose)
maskFinal=maskClose
conts,h=cv2.findContours(maskFinal.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
if(len(conts)==2):
if(pinchFlag==1):
pinchFlag = 0
mouse.release(Button.left)
x1,y1,w1,h1=cv2.boundingRect(conts[0])
x2,y2,w2,h2=cv2.boundingRect(conts[1])
cv2.rectangle(img,(x1,y1),(x1+w1,y1+h1),(255,0,0),2)
cv2.rectangle(img,(x2,y2),(x2+w2,y2+h2),(255,0,0),2)
请帮我看看可能是什么问题?
我的整个代码如下:
import cv2
import numpy as np
from pynput.mouse import Button, Controller
import wx
mouse=Controller()
app=wx.App(False)
(sx,sy)=wx.GetDisplaySize()
(camx,camy)=(320,240)
upperBound=np.array([70,70,255])
lowerBound=np.array([22,28,112])
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 = 2
pinchFlag = 0
#mouseLoc = mLocOld + (targetLoc - mLocOld)/DampingFactor
while True:
ret, img = cam.read()
#convert BGR to HSV
imgHSV= cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
# create the Mask
mask=cv2.inRange(imgHSV,lowerBound,upperBound)
#morphology
maskOpen=cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernelOpen)
maskClose=cv2.morphologyEx(maskOpen,cv2.MORPH_CLOSE,kernelClose)
maskFinal=maskClose
conts,h=cv2.findContours(maskFinal.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
if(len(conts)==2):
if(pinchFlag==1):
pinchFlag = 0
mouse.release(Button.left)
x1,y1,w1,h1=cv2.boundingRect(conts[0])
x2,y2,w2,h2=cv2.boundingRect(conts[1])
cv2.rectangle(img,(x1,y1),(x1+w1,y1+h1),(255,0,0),2)
cv2.rectangle(img,(x2,y2),(x2+w2,y2+h2),(255,0,0),2)
cx1=(x1+w1/2)
cy1=(y1+h1/2)
cx2=(x2+w2/2)
cy2=(y2+h2/2)
cx=((cx1+cx2)/2)
cy=((cy1+cy2)/2)
cv2.line(img, (int(cx1),int(cy1)),(int(cx2),int(cy2)),(255,0,0),2)
cv2.circle(img, (int(cx),int(cy)),2,(0,0,255),2)
mouseLoc = mLocOld + ((cx,cy) - mLocOld)/DampingFactor
mouse.position= (sx-(mouseLoc[0]*sx/camx), mouseLoc[1]*sy/camy)
while mouse.position!=(sx-(mouseLoc[0]*sx/camx), mouseLoc[1]*sy/camy):
break
mLocOld = mouseLoc
elif(len(conts)==1):
if(pinchFlag==0):
pinchFlag = 1
mouse.press(Button.left)
x,y,w,h=cv2.boundingRect(conts[0])
cv2.rectangle(img,(int(x),int(y)),(int(x+w),int(y+h)),(255,0,0),2)
cx=(x+w/2)
cy=(y+h/2)
cv2.circle(img,(int(cx),int(cy)),int((w+h)/4),(0,0,255),2)
mouseLoc = mLocOld + ((cx,cy) - mLocOld)/DampingFactor
mouse.position= (sx-(mouseLoc[0]*sx/camx), mouseLoc[1]*sy/camy)
while mouse.position!=(sx-(mouseLoc[0]*sx/camx), mouseLoc[1]*sy/camy):
break
mLocOld = mouseLoc
cv2.imshow("cam",img)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
我找到了答案。我使用 brg 值来输入所需的颜色范围。将其更改为 hsv 值,现在可以使用了。
我正在尝试创建一个基于手势的鼠标指针,一切正常,只是相机无法识别粘在我手指上的红色框,因此我的鼠标无法正常工作。
我试图找出问题所在,但它完全在逃避我。我在互联网上浏览过各种类似的代码,但其中大部分都运行良好,或者与我所做的完全不同。
矩形的代码是:
while True:
ret, img = cam.read()
#convert BGR to HSV
imgHSV= cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
# create the Mask
mask=cv2.inRange(imgHSV,lowerBound,upperBound)
#morphology
maskOpen=cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernelOpen)
maskClose=cv2.morphologyEx(maskOpen,cv2.MORPH_CLOSE,kernelClose)
maskFinal=maskClose
conts,h=cv2.findContours(maskFinal.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
if(len(conts)==2):
if(pinchFlag==1):
pinchFlag = 0
mouse.release(Button.left)
x1,y1,w1,h1=cv2.boundingRect(conts[0])
x2,y2,w2,h2=cv2.boundingRect(conts[1])
cv2.rectangle(img,(x1,y1),(x1+w1,y1+h1),(255,0,0),2)
cv2.rectangle(img,(x2,y2),(x2+w2,y2+h2),(255,0,0),2)
请帮我看看可能是什么问题?
我的整个代码如下:
import cv2
import numpy as np
from pynput.mouse import Button, Controller
import wx
mouse=Controller()
app=wx.App(False)
(sx,sy)=wx.GetDisplaySize()
(camx,camy)=(320,240)
upperBound=np.array([70,70,255])
lowerBound=np.array([22,28,112])
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 = 2
pinchFlag = 0
#mouseLoc = mLocOld + (targetLoc - mLocOld)/DampingFactor
while True:
ret, img = cam.read()
#convert BGR to HSV
imgHSV= cv2.cvtColor(img,cv2.COLOR_BGR2HSV)
# create the Mask
mask=cv2.inRange(imgHSV,lowerBound,upperBound)
#morphology
maskOpen=cv2.morphologyEx(mask,cv2.MORPH_OPEN,kernelOpen)
maskClose=cv2.morphologyEx(maskOpen,cv2.MORPH_CLOSE,kernelClose)
maskFinal=maskClose
conts,h=cv2.findContours(maskFinal.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)
if(len(conts)==2):
if(pinchFlag==1):
pinchFlag = 0
mouse.release(Button.left)
x1,y1,w1,h1=cv2.boundingRect(conts[0])
x2,y2,w2,h2=cv2.boundingRect(conts[1])
cv2.rectangle(img,(x1,y1),(x1+w1,y1+h1),(255,0,0),2)
cv2.rectangle(img,(x2,y2),(x2+w2,y2+h2),(255,0,0),2)
cx1=(x1+w1/2)
cy1=(y1+h1/2)
cx2=(x2+w2/2)
cy2=(y2+h2/2)
cx=((cx1+cx2)/2)
cy=((cy1+cy2)/2)
cv2.line(img, (int(cx1),int(cy1)),(int(cx2),int(cy2)),(255,0,0),2)
cv2.circle(img, (int(cx),int(cy)),2,(0,0,255),2)
mouseLoc = mLocOld + ((cx,cy) - mLocOld)/DampingFactor
mouse.position= (sx-(mouseLoc[0]*sx/camx), mouseLoc[1]*sy/camy)
while mouse.position!=(sx-(mouseLoc[0]*sx/camx), mouseLoc[1]*sy/camy):
break
mLocOld = mouseLoc
elif(len(conts)==1):
if(pinchFlag==0):
pinchFlag = 1
mouse.press(Button.left)
x,y,w,h=cv2.boundingRect(conts[0])
cv2.rectangle(img,(int(x),int(y)),(int(x+w),int(y+h)),(255,0,0),2)
cx=(x+w/2)
cy=(y+h/2)
cv2.circle(img,(int(cx),int(cy)),int((w+h)/4),(0,0,255),2)
mouseLoc = mLocOld + ((cx,cy) - mLocOld)/DampingFactor
mouse.position= (sx-(mouseLoc[0]*sx/camx), mouseLoc[1]*sy/camy)
while mouse.position!=(sx-(mouseLoc[0]*sx/camx), mouseLoc[1]*sy/camy):
break
mLocOld = mouseLoc
cv2.imshow("cam",img)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
我找到了答案。我使用 brg 值来输入所需的颜色范围。将其更改为 hsv 值,现在可以使用了。