在锡月平台上移植好的源程序
# -*- coding:utf-8 -*-
# 创建时间:2019年8月8日
# 功能:模式一:二维码/条形码识别+拐点圆柱识别
# 模式二:靶子识别定位
import numpy as np
import imutils
import time
import cv2
import V_Display as vd
import V_UCom as com
import pyzbar.pyzbar as pyzbar
# 模式一发送数据
Uart_buf1 = [0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xAA]
# 模式三发送数据
Uart_buf3 = [0x55, 0xAA, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0xAA]
# 切换两种模式的标志位
global flag
flag = 0 # 默认模式一
# 二维码识别函数
# 输入参数:读取图像矩阵
# 返回参数:image标记后的图像矩阵;C_x,C_y:目标二维码中心点坐标;
# (返回参数:flag:等于1时表示成功检测到二维码,0表示未成功检测)
def decodeDisplay(image):
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 转换成灰度图
barcodes = pyzbar.decode(image)
# print('barcodes', barcodes)
if barcodes:
print('成功检测二维码')
for barcode in barcodes:
# 提取条形码的边界框的位置
# 画出图像中条形码的边界框
(x, y, w, h) = barcode.rect
# 二维码中心坐标
cx = int(x + w / 2)
cy = int(y + h / 2)
cv2.circle(image, (cx, cy), 2, (0, 255, 0), 8) # 做出中心坐标
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 0, 255), 2) # 做出矩形
print('code position', cx, cy)
return image, cx, cy
return image, 320, 240 # 返回默认图像中心位置
# 寻找竖直黑色直线并标记
# 输入参数:image输入图像矩阵
# 输出参数:黑色直线x轴坐标,True,False判断是否成功判断
def black_line(image):
# 设置闸值,只寻找黑色直线
Lower = np.array([0, 0, 0])
Upper = np.array([180, 255, 80])
HSV = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
mask = cv2.inRange(HSV, Lower, Upper)
vd.show(image)
# lines = cv2.HoughLines(mask, 1, np.pi/180, threshold=400, max_theta=0.1)
lines = cv2.HoughLines(mask, 1, np.pi/180, threshold=200, max_theta=0.5)
#print('lines', lines)
# 判断是否检测到直线
if lines is not None:
for line in lines:
r, theta = line[0]
# 画出垂直直线
if theta<0.015:
x0 = r * np.cos(theta)
y0 = r * np.sin(theta)
x1 = int(x0 - 1000 * np.sin(theta))
y1 = int(y0 + 1000 * np.cos(theta))
x2 = int(x0 + 1000 * np.sin(theta))
y2 = int(y0 - 1000 * np.cos(theta))
cv2.line(image, (x1, y1), (x2, y2), (0, 0, 255), 2)
vd.show(image)
x = abs((x1+x2)/2)
print('line x position', x)
return True, x
return False, 320 # 返回默认中心坐标
# Blob圆检测
# 输入参数:img输入图像矩阵;
# 输出参数:cx,cy坐标
def blob(img):
# 实例化
params = cv2.SimpleBlobDetector_Params()
# 设置检测闸值
params.minThreshold = 10
params.maxThreshold = 200
# 设置面积闸值
params.filterByArea = True
params.minArea = 150
# 设置圆性
params.filterByCircularity = True
params.minCircularity = 0.8
# 设置凸性
params.filterByConvexity = True
params.minConvexity = 0.87
# 设置惯量比
params.filterByInertia = True
params.minInertiaRatio = 0.01
# 选择合适的版本实例化
ver = (cv2.__version__).split('.')
if int(ver[0]) < 3:
detector = cv2.SimpleBlobDetector(params)
else:
detector = cv2.SimpleBlobDetector_create(params)
keypoints = detector.detect(img)
# 作图
im_with_keypoints = cv2.drawKeypoints(img, keypoints, np.array([]), (0, 0, 255),
cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)
# Show blobs
vd.show(im_with_keypoints)
if keypoints:
for i in range(0, len(keypoints)):
x = keypoints[i].pt[0]
y = keypoints[i].pt[1]
cx = int(x)
cy = int(y)
print('circle position', cx, cy)
return cx, cy
else:
print('未检测到圆!')
return 320, 240 # 返回默认中心坐标
# 各种初始化操作
com.init(mode=2)
cap = cv2.VideoCapture(1) # 使用前置摄像头
cap.set(3, 320) # 设置摄像头输出宽
cap.set(4, 240) # 设置摄像头输出高
print("start reading video...")
time.sleep(2.0)
print("start working")
# vd初始化
vd.init()
while True:
# 通信切换部分
a = com.read()
b = ''
for i in a:
b = b + str(ord(i))
b = int(b)
# 模式一
if b == 1:
com.send(Uart_buf1)
flag = 0
# 模式二
else:
com.send(Uart_buf3)
flag = 1
# 默认模式一
if flag == 0:
print('Mode 1')
while cap.isOpened():
ret, frame = cap.read()
frame_copy = frame.copy()
# 二维码检测
img, x, y = decodeDisplay(frame)
vd.show(img)
# 竖直黑线检测
ret_1, x = black_line(frame_copy)
if flag == 1:
print('Mode 2')
while cap.isOpened():
ret, frame = cap.read()
x, y = blob(frame) # blob圆检测定位