-
Notifications
You must be signed in to change notification settings - Fork 2
/
colour.py
executable file
·68 lines (60 loc) · 1.69 KB
/
colour.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# -*- coding: utf-8 -*-
#以捕捉手指上系的蓝色便签条为例
#摄像头480p,ubuntu 18.10
#macbook air BIG SUR
#拍摄图片中不能出现其他作为标志色的物品啦。
import random
import time
import cv2
import numpy as np
#import matplotlib.pyplot as plt
from turtle import *
from time import ctime,sleep
#import threading
delay(delay=None)
color('red', 'blue')
cap = cv2.VideoCapture(0) # 或传入0,使用摄像头
def getpicture():
# 读取一帧
_, frame = cap.read()
# 把 BGR 转为 HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# HSV中标记色范围
lower_mark = np.array([100,90,99])
upper_mark = np.array([124,255,255])
# 获得标记色区域的mask
mask = cv2.inRange(hsv, lower_mark, upper_mark)
mask = cv2.blur(mask,(10,10))
# 和原始图片进行and操作,获得标记色区域
res = cv2.bitwise_and(frame,frame, mask= mask)
res = cv2.blur(res,(10,10))
#以下这些调试用的
cv2.namedWindow('frame', cv2.WINDOW_NORMAL)
cv2.imshow('frame',frame)
cv2.namedWindow('mask', cv2.WINDOW_NORMAL)
cv2.imshow('mask',mask)
cv2.namedWindow('res', cv2.WINDOW_NORMAL)
cv2.imshow('res',res)
'''k = cv2.waitKey(5) & 0xFF
if k == 27:
sign = 1
else:
sign = 0'''
return res
def getlocation(mask):
location = np.where(mask>150)
if len(location[0])!= 0:
x = location[0][0]
y = location[1][0]
goto(-y+500,-x+500)
#根据屏幕大小自行调整
while True:
mask = getpicture()
getlocation(mask)
#停一下好像效果更好
time.sleep(0.02)
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
cv2.destroyAllWindows()
done()