2023电赛追踪代码存库自用

import sensor, image, time, math,screen
from pyb import UART,LED
uart = UART(3, 9600)
thresholds = (0, 100, 19, 127, -128, 127)
clock = time.clock()
global x_red
global x_green
global y_red
global y_green
global flag
flag = 0
screen.init()
def color_blob(threshold):
	blobs = img.find_blobs([threshold])
	if len(blobs) == 1:
		b = blobs[0]
		cx = b[5]
		cy = b[6]
	return 160, 120
def find_max(blobs):
	max_size=0
	for blob in blobs:
		if blob[2]*blob[3] > max_size:
			max_blob=blob
			max_size = blob[2]*blob[3]
	return max_blob
def sensor1():
	sensor.reset()
	sensor.set_pixformat(sensor.RGB565)
	sensor.set_framesize(sensor.QVGA)
	sensor.set_auto_gain(False)
	sensor.skip_frames(20)
	sensor.set_vflip(True)
	sensor.set_hmirror(True)
	sensor.set_windowing((240, 240))
	sensor.set_auto_exposure(False,300)
def sensor2():
	sensor.reset()
	sensor.set_pixformat(sensor.RGB565)
	sensor.set_framesize(sensor.QQVGA)
	sensor.skip_frames(time = 2000)
	sensor.set_vflip(True)
	sensor.set_hmirror(True)
	sensor.set_windowing((120, 120))
sensor1()
while(True):
	img = sensor.snapshot()
	img = sensor.snapshot()
	print('点')
	img = sensor.snapshot()
	blobs = img.find_blobs([thresholds])
	if blobs:
			color_blob(thresholds)
			max_blob = find_max(blobs)
			pan_error = max_blob.cx()-83
			tilt_error = max_blob.cy()-155
			img.draw_rectangle(max_blob.rect())
			img.draw_cross(max_blob.cx(), max_blob.cy())
			uart.write('%d %d %d\r\n'%(pan_error,tilt_error,flag))
			print('pan_error',pan_error,'tilt_error',tilt_error)
			if abs(pan_error) < 2 and abs(tilt_error) < 2:
				flag = 1
				uart.write('%d %d %d\r\n'%(pan_error,tilt_error,flag))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值