# -*- coding: utf-8 -*-
import cv2
import numpy as np
import time
kernel = np.ones((5,5),np.uint8)
# 从网络摄像头获取输入
cap = cv2.VideoCapture(0)
time.sleep(0.5)
# 将视频尺寸减小到320x240,这样rpi处理速度就会更快
cap.set(3,320)
cap.set(4,240)
def nothing(x):
pass
# 创建一个供以后使用的窗口
cv2.namedWindow('HueComp')
cv2.namedWindow('SatComp')
cv2.namedWindow('ValComp')
cv2.namedWindow('closing')
cv2.namedWindow('tracking')
# 创建跟踪条的最小和最大的色调,饱和度和价值
# 您可以根据需要调整默认值
cv2.createTrackbar('hmin', 'HueComp',12,179,nothing)
cv2.createTrackbar('hmax', 'HueComp',37,179,nothing)
cv2.createTrackbar('smin', 'SatComp',96,255,nothing)
cv2.createTrackbar('smax', 'SatComp',255,255,nothing)
cv2.createTrackbar('vmin', 'ValComp',186,255,nothing)
cv2.createTrackbar('vmax', 'ValComp',255,255,nothing)
while(1):
buzz = 0
_, frame = cap.read()
hsv = cv2.cvtColor(frame,cv2.COLOR_BGR2HSV)
hue,sat,val = cv2.split(hsv)
hmn = cv2.getTrackbarPos('hmin','HueComp')
hmx = cv2.getTrackbarPos('hmax','HueComp')
smn = cv2.getTrackbarPos('smin','SatComp')
smx = cv2.getTrackbarPos('smax','SatComp')
vmn = cv2.getTrackbarPos('vmin','ValComp')
vmx = cv2.getTrackbarPos('vmax','ValComp')
hthresh = cv2.inRange(np.array(hue),np.array(hmn),np.array(hmx))
sthresh = cv2.inRange(np.array(sat),np.array(smn),np.array(smx))
vthresh = cv2.inRange(np.array(val),np.array(vmn),np.array(vmx))
tracking = cv2.bitwise_and(hthresh,cv2.bitwise_and(sthresh,vthresh))
dilation = cv2.dilate(tracking,kernel,iterations = 1)
closing = cv2.morphologyEx(dilation, cv2.MORPH_CLOSE, kernel)
closing = cv2.GaussianBlur(closing,(5,5),0)
circles = cv2.HoughCircles(closing,cv2.HOUGH_GRADIENT,2,120,param1=120,param2=50,minRadius=10,maxRadius=0)
if circles is not None:
x, y, r = circles[0][0]
x_p = int(round(x))
print (x_p)
for i in circles[0,:]:
if int(round(i[2])) < 30:
cv2.circle(frame,(int(round(i[0])),int(round(i[1]))),int(round(i[2])),(0,255,0),5)
cv2.circle(frame,(int(round(i[0])),int(round(i[1]))),2,(0,255,0),10)
elif int(round(i[2])) > 35:
cv2.circle(frame,(int(round(i[0])),int(round(i[1]))),int(round(i[2])),(0,0,255),5)
cv2.circle(frame,(int(round(i[0])),int(round(i[1]))),2,(0,0,255),10)
buzz = 1
cv2.imshow('HueComp',hthresh)
cv2.imshow('SatComp',sthresh)
cv2.imshow('ValComp',vthresh)
cv2.imshow('closing',closing)
cv2.imshow('tracking',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
cap.release()
L_Motor.stop()
R_Motor.stop()
GPIO.cleanup()
cv2.destroyAllWindows()
break
cap.release()
cv2.destroyAllWindows()
没有合适的资源?快使用搜索试试~ 我知道了~
HSV调试程序打包.rar
资源推荐
资源详情
资源评论
收起资源包目录
HSV调试程序打包.rar (4个子文件)
HSV调试程序打包
11.小球颜色追踪HSV调试
variable_BallTracking.py 3KB
6.将BGR转换至HSV
Convert_BGR_HSV.py 401B
3.捕获视频流中一帧图片并保存
Cap_Video_streaming_pictures.py 1KB
Cap_Video_streaming_320x240.py 2KB
共 4 条
- 1
资源评论
NULL指向我
- 粉丝: 2350
- 资源: 99
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功