资源简介
树莓派沿着单个白线行走,用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实现SGBM图像匹配算法
- python实现灰度直方图均衡化
- scrapy_qunar_one
- Python学习全系列教程永久可用
- python简明教程.chm
- 抽奖大转盘python的图形化界面
- 双边滤波器实验报告及代码python
- python +MYSQL+HTML实现21蛋糕网上商城
- Python-直播答题助手自动检测出题搜索
- OpenCV入门教程+OpenCV官方教程中文版
- Python 串口工具源码+.exe文件
- Python开发的全栈股票系统.zip
- Python操作Excel表格并将其中部分数据写
- python书籍 PDF
- 利用python绘制散点图
- python+labview+No1.vi
- 老男孩python项目实战
- python源码制作whl文件.rar
- python3.5可用的scipy
- PYTHON3 经典50案例.pptx
- 计算机科学导论-python.pdf
- python模拟鼠标点击屏幕
- windows鼠标自动点击py脚本
- 鱼c小甲鱼零基础学python全套课后题和
- Python 练习题100道
- Practical Programming 2nd Edition
- wxPython Application Development Cookbook
- python 3.6
- Python 3.5.2 中文文档 互联网唯一CHM版本
- python3.5.2.chm官方文档
评论
共有 条评论