资源简介
树莓派沿着单个白线行走,用opencv进行图像处理,
代码片段和文件信息
import cv2
import cv2.cv as cv
import cv
import picamera
import picamera.array
import math
import numpy as np
import zmq
import time
import serial time sys
from PIL import Image
# For OpenCV2 image display
IMAGE_WINDOW_NAME = ‘YelloBarTracker‘
CONTROL_WINDOW_NAME = ‘Control‘
MASK_WINDOW_NAME = ‘Mask‘
# For socket communication
port = ‘5556‘
context = zmq.Context()
socket = context.socket(zmq.PUB)
# Setting the initial mask threshold
# 根据环境调试数据以便准确的捕捉小球
iLowH = 5
iHighH = 22
iLowS = 219
iHighS = 255
iLowV = 149
iHighV = 255
# 是否抓到球
getball = 0
# connect arduino
# 将arduino接到树莓派上输入ls /dev查看我的是ttyUSB0
def connect_arduino():
arduino = serial.Serial(‘/dev/ttyUSB0‘9600timeout=1)
arduino.close()
arduino.open()
return arduino
# Require by cv2.createTrackbar. we have nothing to do with nothing method
def nothing(var):
pass
def connect():
print(‘Getting data from camera...‘)
socket.bind(‘tcp://*:%s‘ % port)
# Create trackbars for easier adjustment of the HSV threshold 方便手动调节区间
def make_hsv_adjustment():
cv2.namedWindow(CONTROL_WINDOW_NAME)
cv2.createTrackbar(‘LowH‘ CONTROL_WINDOW_NAME iLowH 255 nothing); #Hue (0 - 179)
cv2.createTrackbar(‘HighH‘ CONTROL_WINDOW_NAME iHighH 255 nothing);
cv2.createTrackbar(‘LowS‘ CONTROL_WINDOW_NAME iLowS 255 nothing); #Saturation (0 - 255)
cv2.createTrackbar(‘HighS‘ CONTROL_WINDOW_NAME iHighS 255 nothing);
cv2.createTrackbar(‘LowV‘ CONTROL_WINDOW_NAME iLowV 255 nothing); #Value (0 - 255)
cv2.createTrackbar(‘HighV‘ CONTROL_WINDOW_NAME iHighV 255 nothing);
def track(image):
‘‘‘Accepts BGR image as Numpy array
Returns: (xy) coordinates of centroid if found
(-1-1) if no centroid was found
None if user hit ESC
‘‘‘
# Blur the image to reduce noise
blur = cv2.GaussianBlur(image (55)0)
# Convert BGR to HSV
hsv = cv2.cvtColor(blur cv2.COLOR_BGR2HSV)
# Get the treshold from the trackbars
iLowH = cv2.getTrackbarPos(‘LowH‘ CONTROL_WINDOW_NAME)
iHighH = cv2.getTrackbarPos(‘HighH‘ CONTROL_WINDOW_NAME)
iLowS = cv2.getTrackbarPos(‘LowS‘ CONTROL_WINDOW_NAME)
iHighS = cv2.getTrackbarPos(‘HighS‘ CONTROL_WINDOW_NAME)
iLowV = cv2.getTrackbarPos(‘LowV‘ CONTROL_WINDOW_NAME)
iHighV = cv2.getTrackbarPos(‘HighV‘ CONTROL_WINDOW_NAME)
# Threshold the HSV image for only green colors
lower_yellow = np.array([iLowHiLowSiLowV])
upper_yellow = np.array([iHighHiHighSiHighV])
# Threshold the HSV image to get only yellow colors
mask = cv2.inRange(hsv lower_yellow upper_yellow)
cv2.imshow(MASK_WINDOW_NAME mask)
# Blur the mask
bmask = cv2.GaussianBlur(mask (55)0)
# Take the moments to get the centroid
moments = cv2.moments(bmask)
m00 = moments[‘m00‘]
centroid_x centroid_y radius = None None N
- 上一篇:python九宫格拼图小游戏
- 下一篇:AlexNet深度神经网络
相关资源
- python九宫格拼图小游戏
- chapter7 data
- python_tweets.rar
- python深度学习电子版+源码
- VSM模型构建样例项目文件
- Python获取气象网站中的台风详细数据
- python实现的视频播放器
- Python 控制ITECH IT6302 POWER SUPPLY
- Python 控制ITECH IT8511 Loader
- 携程机票python爬取脚本最终优化版本
- 携程机票python爬取脚本
- Python自动化运维视频开发
- 大地坐标转换为空间直角坐标
- python编写的贪吃蛇
- CNN卷积神经网络python代码
- 零基础:21天搞定Python分布式爬虫入门
- Ta-lib 64位whl安装文件
- distribute_setup.zip
- Python 球面墨卡托投影和地体坐标大批
- python模拟登陆163邮箱并获取通讯录.
- python支付宝爬虫获取个人账单
- 你没有的food_info.csv文件
- 隐马尔可夫模型(HMM) Python代码 《
- python数据离差标准化
- Qt界面文件(.ui)和资源文件(.qrc)
- python人脸检测源代码
- Python获取招聘网站数据
- python串口通信模块包--pyserial-3.4.tar.
- Caffe-Windows下画loss和accuracy曲线python代
- Python3实现的用户管理系统
评论
共有 条评论