1.只print前后左右
from collections import deque
import numpy as np
#import imutils
import cv2
import time
#设定红色阈值,HSV空间
redLower = np.array([0, 100, 100])
redUpper = np.array([10, 255, 255])
#初始化追踪点的列表
mybuffer = 64
pts = deque(maxlen=mybuffer)
#打开摄像头
camera = cv2.VideoCapture(0)
#等待两秒
time.sleep(2)
#遍历每一帧,检测红色瓶盖
while True:
#读取帧
(ret, frame) = camera.read()
#判断是否成功打开摄像头
if not ret:
print ('No Camera')
break
#转到HSV空间
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
#根据阈值构建掩膜
mask = cv2.inRange(hsv, redLower, redUpper)
#腐蚀操作
mask = cv2.erode(mask, None, iterations=2)
#膨胀操作,其实先腐蚀再膨胀的效果是开运算,去除噪点
mask = cv2.dilate(mask, None, iterations=2)
#轮廓检测
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
#初始化瓶盖圆形轮廓质心
center = None
#如果存在轮廓
if len(cnts) > 0:
#找到面积最大的轮廓
c = max(cnts, key = cv2.contourArea)
#确定面积最大的轮廓的外接圆
((x, y), radius) = cv2.minEnclosingCircle(c)
#计算轮廓的矩
#print(x,y)
if x>320:
print("左转")
if x<320:
print("右转")
if y>240:
print("下移")
if y<240:
print("上移")
M = cv2.moments(c)
#计算质心
center = (int(M["m10"]/M["m00"]), int(M["m01"]/M["m00"]))
#只有当半径大于10时,才执行画图
if radius > 10:
cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2)
cv2.circle(frame, center, 5, (0, 0, 255), -1)
#把质心添加到pts中,并且是添加到列表左侧
pts.appendleft(center)
#遍历追踪点,分段画出轨迹
for i in range(1, len(pts)):
if pts[i - 1] is None or pts[i] is None:
continue
#计算所画小线段的粗细
thickness = int(np.sqrt(mybuffer / float(i + 1)) * 2.5)
#画出小线段
cv2.line(frame, pts[i - 1], pts[i], (0, 0, 255), thickness)
#res = cv2.bitwise_and(frame, frame, mask=mask)
cv2.imshow('Frame', frame)
#键盘检测,检测到esc键退出
k = cv2.waitKey(5)&0xFF
if k == 27:
break
#摄像头释放
camera.release()
#销毁所有窗口
cv2.destroyAllWindows()
2.树莓派物体跟踪第二版
untitled.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'untitled.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(500, 321)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(60, 60, 54, 12))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(60, 120, 54, 12))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(50, 200, 54, 12))
self.label_3.setText("")
self.label_3.setObjectName("label_3")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(150, 60, 113, 20))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(150, 120, 113, 20))
self.lineEdit_2.setObjectName("lineEdit_2")
self.label_4 = QtWidgets.QLabel(self.centralwidget)
self.label_4.setGeometry(QtCore.QRect(60, 180, 71, 16))
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(self.centralwidget)
self.label_5.setGeometry(QtCore.QRect(60, 230, 71, 16))
self.label_5.setObjectName("label_5")
self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_3.setGeometry(QtCore.QRect(150, 180, 113, 20))
self.lineEdit_3.setObjectName("lineEdit_3")
self.lineEdit_4 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_4.setGeometry(QtCore.QRect(150, 230, 113, 20))
self.lineEdit_4.setObjectName("lineEdit_4")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 500, 23))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "颜色:"))
self.label_2.setText(_translate("MainWindow", "形状:"))
self.label_4.setText(_translate("MainWindow", "左右偏移量:"))
self.label_5.setText(_translate("MainWindow", "上下偏移量:"))
def chuli(a,b):
ui.lineEdit_3.setText(str(a))
ui.lineEdit_4.setText(str(b))
import sys
import haha
from PyQt5.QtWidgets import QApplication, QMainWindow
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
T = haha.MyTemperatureThread()
T.breakSignal.connect(chuli)
T.start()
MainWindow.show()
sys.exit(app.exec_())
haha.py
from collections import deque
import numpy as np
#import imutils
import cv2
import time
from PyQt5.QtCore import QThread,pyqtSignal
#设定红色阈值,HSV空间
redLower = np.array([0, 100, 100])
redUpper = np.array([10, 255, 255])
#初始化追踪点的列表
mybuffer = 64
pts = deque(maxlen=mybuffer)
#打开摄像头
camera = cv2.VideoCapture(0)
#等待两秒
time.sleep(2)
class MyTemperatureThread(QThread):
def __init__(self, parent=None): # 线程初始化
super().__init__(parent)
# 定义一个信号
breakSignal = pyqtSignal(int, int)
# 界面显示,定义x全局变量的时候一认为,先执行界面,所以就没有定义,后来发现,先执行函数再执行界面
def run(self):
#遍历每一帧,检测红色瓶盖
while True:
#读取帧
(ret, frame) = camera.read()
#判断是否成功打开摄像头
if not ret:
print ('No Camera')
break
#转到HSV空间
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
#根据阈值构建掩膜
mask = cv2.inRange(hsv, redLower, redUpper)
#腐蚀操作
mask = cv2.erode(mask, None, iterations=2)
#膨胀操作,其实先腐蚀再膨胀的效果是开运算,去除噪点
mask = cv2.dilate(mask, None, iterations=2)
#轮廓检测
cnts = cv2.findContours(mask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2]
#初始化瓶盖圆形轮廓质心
center = None
#如果存在轮廓
if len(cnts) > 0:
#找到面积最大的轮廓
c = max(cnts, key = cv2.contourArea)
#确定面积最大的轮廓的外接圆
((x, y), radius) = cv2.minEnclosingCircle(c)
#计算轮廓的矩
#print(x,y)
x_pian = x-320
y_pian = y-320
print(x_pian,y_pian)
self.breakSignal.emit(x_pian,y_pian)
M = cv2.moments(c)
#计算质心
center = (int(M["m10"]/M["m00"]), int(M["m01"]/M["m00"]))
#只有当半径大于10时,才执行画图
if radius > 10:
cv2.circle(frame, (int(x), int(y)), int(radius), (0, 255, 255), 2)
cv2.circle(frame, center, 5, (0, 0, 255), -1)
#把质心添加到pts中,并且是添加到列表左侧
pts.appendleft(center)
#遍历追踪点,分段画出轨迹
for i in range(1, len(pts)):
if pts[i - 1] is None or pts[i] is None:
continue
#计算所画小线段的粗细
thickness = int(np.sqrt(mybuffer / float(i + 1)) * 2.5)
#画出小线段
cv2.line(frame, pts[i - 1], pts[i], (0, 0, 255), thickness)
#res = cv2.bitwise_and(frame, frame, mask=mask)
cv2.imshow('Frame', frame)
#键盘检测,检测到esc键退出
k = cv2.waitKey(5)&0xFF
if k == 27:
break