前言
识别矩形框以及对应角点
from machine import Pin
import sensor, image, time
import pyb
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.set_auto_whitebal(True)
sensor.set_brightness(3000)
sensor.skip_frames(time = 20)
clock = time.clock()
corner = 0
while(True):
clock.tick()
img = sensor.snapshot()
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)
corner = r.corners()
img.draw_circle(corner[0][0], corner[0][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
img.draw_circle(corner[1][0], corner[1][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
img.draw_circle(corner[2][0], corner[2][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
img.draw_circle(corner[3][0], corner[3][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
corner1_str = f"corner1 = ({corner[0][0]},{corner[0][1]})"
corner2_str = f"corner2 = ({corner[1][0]},{corner[1][1]})"
corner3_str = f"corner3 = ({corner[2][0]},{corner[2][1]})"
corner4_str = f"corner4 = ({corner[3][0]},{corner[3][1]})"
print(corner1_str + "\n" + corner2_str + "\n" + corner3_str + "\n" + corner4_str)
跟踪激光灯
from machine import Pin
import sensor, image, time
import pyb
laser_light=Pin("P0", Pin.OUT)
laser_light.value(1)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.set_auto_whitebal(True)
sensor.set_brightness(3000)
sensor.skip_frames(time = 20)
clock = time.clock()
while(True):
clock.tick()
img = sensor.snapshot()
red_td = [(56, 100, 45, 127, -128, 127)]
for b in img.find_blobs(red_td,pixels_threshold=2, area_threshold=15, merge=True,invert = 0):
img.draw_rectangle(b.rect(), color = (0, 255, 0), scale = 2, thickness = 2)
print(f"rect = {b.x() + b.w()/2},{b.y() + b.h()/2}")
break
识别矩形框以及对应角点并跟踪激光灯
from machine import Pin
import sensor, image, time
import pyb
laser_light=Pin("P9", Pin.OUT)
laser_light.value(1)
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.set_auto_whitebal(True)
sensor.set_brightness(3000)
sensor.skip_frames(time = 20)
clock = time.clock()
while(True):
clock.tick()
img = sensor.snapshot()
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)
corner = r.corners()
img.draw_circle(corner[0][0], corner[0][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
img.draw_circle(corner[1][0], corner[1][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
img.draw_circle(corner[2][0], corner[2][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
img.draw_circle(corner[3][0], corner[3][1], 5, color = (0, 0, 255), thickness = 2, fill = False)
red_td = [(56, 100, 45, 127, -128, 127)]
for b in img.find_blobs(red_td,pixels_threshold=2, area_threshold=15, merge=True,invert = 0):
img.draw_rectangle(b.rect(), color = (0, 255, 0), scale = 2, thickness = 2)
break
print(clock.fps())