追踪矩形的小车
其中car.py和pid.py与"追小球的小车"中一致,不需要更改 需要更改的地方
- a.返回摄像头中的矩形:
rects = find_rects(threshold)函数 - b.计算方向误差(左右):原先为:
x_error = max_blob[5]-img.width()/2 —>视野中最大色块的中心x位置-摄像头的中心位置 用于计算目标在摄像头中偏离中心的距离 - 改成:
x_error = max_rect[0]+max_rect[2]/2-img.width()/2 —>rect类无法直接调出矩形的中心x坐标,因此得进行计算视野中最大矩形左上角的x坐标 + 矩形一半的宽度 - 摄像头中心的x坐标 - c.在色块的中心(cx,cy)画十字:
img.draw_cross(max_blob[5], max_blob[6]) - 改成:
img.draw_cross(int(max_rect[0]+max_rect[2]/2), int(max_rect[1]+max_rect[3]/2)) 此处要强制类型转换!
import sensor, image, time
import car
from pid import PID
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
clock = time.clock()
green_threshold = (76, 96, -110, -30, 8, 66)
size_threshold = 2000
x_pid = PID(p=0.5, i=1, imax=100)
h_pid = PID(p=0.05, i=0.1, imax=50)
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
while(True):
clock.tick()
img = sensor.snapshot()
rects = img.find_rects()
if rects:
max_rect = find_max(rects)
x_error = max_rect[0]+max_rect[2]/2-img.width()/2
h_error = max_rect[2]*max_rect[3]-size_threshold
print("x error: ", x_error)
'''
for b in blobs:
# Draw a rect around the blob.
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
'''
img.draw_rectangle(max_blob[0:4])
img.draw_cross(int(max_rect[0]+max_rect[2]/2), int(max_rect[1]+max_rect[3]/2))
x_output=x_pid.get_pid(x_error,1)
h_output=h_pid.get_pid(h_error,1)
print("h_output",h_output)
car.run(-h_output-x_output,-h_output+x_output)
else:
car.run(18,-18)
追踪AprilTags的小车
其中car.py和pid.py与"追小球的小车"中一致,不需要更改 需要更改的地方
- a.返回摄像头中的AprilTags:
rects = find_apriltags()函数 - b.计算方向误差(左右):原先为:
x_error = max_blob[5]-img.width()/2 —>视野中最大色块的中心x位置-摄像头的中心位置 用于计算目标在摄像头中偏离中心的距离 - 改成:
x_error = max_apriltag[6]-img.width()/2 —>AprilTags返回值的数组apriltag索引值和blob不一样 - c.在色块的中心(cx,cy)画十字:
img.draw_cross(max_blob[5], max_blob[6]) - 改成:
img.draw_cross(max_apriltag[6],max_apriltag[7]) AprilTags返回值的数组apriltag索引值和blob不一样 - d.如果想查找二维码就改成
image.find_qrcodes([roi])
import sensor, image, time
import car
from pid import PID
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
clock = time.clock()
green_threshold = (76, 96, -110, -30, 8, 66)
size_threshold = 2000
x_pid = PID(p=0.5, i=1, imax=100)
h_pid = PID(p=0.05, i=0.1, imax=50)
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
while(True):
clock.tick()
img = sensor.snapshot()
rects = img.find_apriltags()
if apriltags:
max_apriltag = find_max(apriltags)
x_error = max_apriltag[6]-img.width()/2
h_error = max_rect[2]*max_rect[3]-size_threshold
print("x error: ", x_error)
'''
for b in blobs:
# Draw a rect around the blob.
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
'''
img.draw_rectangle(max_blob[0:4])
img.draw_cross(max_apriltag[6],max_apriltag[7])
x_output=x_pid.get_pid(x_error,1)
h_output=h_pid.get_pid(h_error,1)
print("h_output",h_output)
car.run(-h_output-x_output,-h_output+x_output)
else:
car.run(18,-18)
追踪模板的小车
其中car.py和pid.py与"追小球的小车"中一致,不需要更改 需要更改的地方
- 图像格式改为灰度图
sensor.set_pixformat(sensor.GRAYSCALE) - 导入模块
from image import SEARCH_EX, SEARCH_DS - 创建或导入一个模板,
template = image.Image("/template.pgm") 注意这个模板必须得是pgm格式的 - 修改查找函数
img.find_template(template, 0.70, step=4, search=SEARCH_EX)
r = img.find_template(template, 0.70, step=4, search=SEARCH_EX)
import sensor, image, time
import car
from pid import PID
from image import SEARCH_EX, SEARCH_DS
sensor.reset()
sensor.set_pixformat(sensor.GRAYSCALE)
sensor.set_framesize(sensor.QQVGA)
sensor.skip_frames(10)
sensor.set_auto_whitebal(False)
clock = time.clock()
green_threshold = (76, 96, -110, -30, 8, 66)
size_threshold = 2000
x_pid = PID(p=0.5, i=1, imax=100)
h_pid = PID(p=0.05, i=0.1, imax=50)
template = image.Image("/template.pgm")
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
while(True):
clock.tick()
img = sensor.snapshot()
r = img.find_template(template, 0.70, step=4, search=SEARCH_EX)
if r:
max_r = find_max(r)
x_error = max_r[0]+max_r[2]/2-img.width()/2
h_error = max_rect[2]*max_rect[3]-size_threshold
print("x error: ", x_error)
'''
for b in blobs:
# Draw a rect around the blob.
img.draw_rectangle(b[0:4]) # rect
img.draw_cross(b[5], b[6]) # cx, cy
'''
img.draw_rectangle(max_blob[0:4])
img.draw_cross(int(max_r[0]+max_r[2]/2),int(max_r[1]+max_r[3]/2))
x_output=x_pid.get_pid(x_error,1)
h_output=h_pid.get_pid(h_error,1)
print("h_output",h_output)
car.run(-h_output-x_output,-h_output+x_output)
else:
car.run(18,-18)
|