from machine import Pin
import pyb
import ustruct
from pyb import UART,LED
import sensor, image, time,math
from pyb import UART, Pin,Timer
#import seekfree, pyb
# 初始化TFT180屏幕
#lcd = seekfree.LCD180(3)
#比值x=2.88 y=2.5
#a1=1500+(y2-yi)
#a2=1500+(x2-x1)*2.88
# 初始化摄像头
sensor.reset()
sensor.set_pixformat(sensor.RGB565) # 设置图像色彩格式为RGB565格式
sensor.set_framesize(sensor.QQVGA) # 设置图像大小为160*120
sensor.set_auto_whitebal(True) # 设置自动白平衡
sensor.set_brightness(3000) # 设置亮度为3000
sensor.skip_frames(time = 20) # 跳过帧
uart = UART(3,115200) #设置串口波特率,与stm32一致
uart.init(115200, bits=8, parity=None, stop=1 )
tim = Timer(4, freq=1000) # Frequency in Hz
led_dac = tim.channel(1, Timer.PWM, pin=Pin("P7"), pulse_width_percent=50)
led_dac.pulse_width_percent(100)
led_status=0
servo0=1500
servo1=1500
uart.write("{{#000P{:0>4d}T1100!#001P{:0>4d}T1100!}}\n".format(servo0,servo1))
action_demo=0
servo_option=1#操作舵机选择
time.sleep_ms(2000)
clock = time.clock()
corner = 0
while(True):
clock.tick()
img = sensor.snapshot()#畸变矫正
#uart.write("{{#000P1550T1100!#001P1560T1000!}}\n")
time.sleep_ms(1000)
# -----矩形框部分-----
# 在图像中寻找矩形
for r in img.find_rects(threshold = 10000):
# 判断矩形边长是否符合要求
if r.w() > 20 and r.h() > 20:
# 在屏幕上框出矩形
img.draw_rectangle(r.rect(), color = (255, 0, 0), scale = 4)
# 获取矩形角点位置
p = r.corners()
# 在屏幕上圈出矩形角点
img.draw_circle(p[0][0], p[0][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
img.draw_circle(p[1][0], p[1][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
img.draw_circle(p[2][0], p[2][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
img.draw_circle(p[3][0], p[3][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
# 打印四个角点坐标, 角点1的数组是corner[0], 坐标就是(corner[0][0],corner[0][1])
# 角点检测输出的角点排序每次不一定一致,矩形左上的角点有可能是corner0,1,2,3其中一个
corner1_str = (f"corne
高博:
corner1_str = (f"corner1 = ({p[0][0]},{p[0][1]})")
corner2_str = (f"corner2 = ({p[1][0]},{p[1][1]})")
corner3_str = (f"corner3 = ({p[2][0]},{p[2][1]})")
corner4_str = (f"corner4 = ({p[3][0]},{p[3][1]})")
print(corner1_str + "\n" + corner2_str + "\n" + corner3_str + "\n" + corner4_str)
uart.write("{{#000P1500T1100!#001P1500T1000!}}\n")
time.sleep_ms(1000)
uart.write("{{#000P1595T1100!#001P1500T1000!}}\n")
time.sleep_ms(1000)
uart.write("{{#000P1595T1100!#001P1620T1000!}}\n")
time.sleep_ms(1000)
uart.write("{{#000P1500T1100!#001P1620T1000!}}\n")
time.sleep_ms(1000)
uart.write("{{#000P1500T1100!#001P1500T1000!}}\n")
time.sleep_ms(1000)
# 显示到屏幕上,此部分会降低帧率
#lcd.show_image(img, 160, 120, 0, 0, zoom=0) #屏幕显示
# 打印帧率
#print(clock.fps())
W
ws42 发布的帖子
-
现在知道这个矩形的四个点的坐标,激光可以正常绕矩形绕着走,现在想要使矩形回到中心点该怎么做?