乒乓球测距(K210)

测距

  • 说明

识别的物体是乒乓球(规格应该是统一的吧), 硬件是K210,测距的函数经过拟合,在50cm范围内是准确的

如果使用起来不准确,可以打印代码中的LM, 然后去测数据,自己再拟合一个函数,代替调代码中的length = 1371.96686*Lm**(-1.06218)

  • 代码
import sensor
import image
import lcd
import time
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_auto_whitebal(False)
sensor.set_hmirror(False)
sensor.run(1)
threshold = (49, 79, 21, 91, -78, 89)
font_color=(255, 0, 0)
while True:
    img=sensor.snapshot()
    blobs = img.find_blobs([threshold])
    max_pix = 0
    count = -1
    max_i = -1
    if blobs:
        for ele in blobs:
            count += 1
            if ele[4] > max_pix:
                max_pix = ele[4]
                local_i = count
        if local_i >= 0:
            b = blobs[local_i]
            Lm = (b[2]+b[3])/2
            #print("LM:", Lm)   # 测量
            length = 1371.96686*Lm**(-1.06218)
            print("length:", length)
            tmp=img.draw_rectangle(b[0:4])
            tmp=img.draw_cross(b[5], b[6])
            c=img.get_pixel(b[5], b[6])
    lcd.display(img)
posted @ 2022-07-31 13:33  ihuahua1415  阅读(670)  评论(0)    收藏  举报
*/