logo

如何编写程序才能让openmv同时识别apriltag和圆形?

2019-06-30 21:56
R
_17826808312

请问如何编写程序才能让openmv同时识别apriltag和圆形?我把识别圆形的程序放到识别apriltag的程序后面,在openmv的视野中没有apriltag只有圆形的情况下,发现识别不了圆形。

import sensor, image, time, math
from pyb import UART

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QQVGA) # we run out of memory if the resolution is much bigger...
sensor.skip_frames(30)
sensor.set_auto_gain(False)  # must turn this off to prevent image washout...
sensor.set_auto_whitebal(False)  # must turn this off to prevent image washout...
clock = time.clock()


f_x = (2.8 / 3.984) * 160 # 默认值
f_y = (2.8 / 2.952) * 120 # 默认值
c_x = 160 * 0.5 # 默认值(image.w * 0.5)
c_y = 120 * 0.5 # 默认值(image.h * 0.5)

def degrees(radians):
    return (180 * radians) / math.pi

while(True):
    clock.tick()
    img = sensor.snapshot()
    for tag in img.find_apriltags(families=image.TAG25H9, fx=f_x, fy=f_y, cx=c_x, cy=c_y): # 默认为TAG36H11
        img.draw_rectangle(tag.rect(), color = (255, 0, 0))
        img.draw_cross(tag.cx(), tag.cy(), color = (0, 255, 0))
        degress = tag.rotation()
        print_args = (tag.x_translation(), tag.y_translation(), tag.z_translation(), \
            degrees(tag.x_rotation()), degrees(tag.y_rotation()), degrees(tag.z_rotation()))
        # 位置的单位是未知的,旋转的单位是角度
        #print("Tx: %f, Ty %f, Tz %f, Rx %f, Ry %f, Rz %f" % print_args)
        #print(tag.id(),degress)
        x = 0
        y = 0
        def tag_id(t):
            global x
            global y
            if (t == 0):
                x = 27.5
                y = 27.5
            elif (t == 1):
                x = 127.5
                y = 27.5
            elif (t == 2):
                x = 227.5
                y = 27.5
        tag_id(tag.id())
        o = tag.x_translation()
        p = tag.y_translation()
        q = tag.z_translation()
        a = o*o + p*p + q*q
        k = 30
        b = k*math.sqrt(a)
        c = b*b-10*10
        d = math.sqrt(c)
        #print(d)
        α = tag.rotation()
        if 0 < α < math.pi/2:
           m = x - d*math.sin(α)
           n = y + d*math.cos(α)
        if 3*math.pi/2 < α < 2*math.pi:
           m = x + d*math.sin(360-α)
           n = y + d*math.cos(360-α)
        if math.pi/2 < α < math.pi:
           m = x - d*math.sin(180-α)
           n = y - d*math.cos(180-α)
        if math.pi < α < 3*math.pi/2:
           m = x + d*math.sin(α-180)
           n = y - d*math.cos(α-180)
        #print(m, n)
        uart = UART(3, 115200)
        #output_str="[%d%03d%03d%03d]" % (1,m,n,α)
        #uart.write(output_str+'\r\n')
        time.sleep(100)
        img = sensor.snapshot().lens_corr(1.8)
        for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,r_min = 2, r_max = 100, r_step = 2):
           img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
           print(c)
2019-07-02 15:29
I
网红小智智 星瞳科技

缩进不对,导致代码的逻辑就不对。

https://singtown.com/learn/50029/

2019-07-03 16:19
R
_17826808312

老师我都是用一个tab缩进的,是这样缩进有问题吗

2019-07-03 16:31
I
网红小智智 星瞳科技

你代码的逻辑不对,你代码的逻辑是如果找到了apriltags,才能find_circles。

问题在于最后的

        img = sensor.snapshot().lens_corr(1.8)
        for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,r_min = 2, r_max = 100, r_step = 2):
           img.draw_circle(c.x(), c.y(), c.r(), color = (255, 0, 0))
           print(c)

前面有8个空格,这个代码段是在find_apriltags里面的。

2019-07-05 15:20
R
_17826808312

@kidswong999 老师我改了缩进以后可以同时识别tag标签和圆形了,但是出来的图像一抖一抖的是为什么

2019-07-05 15:55
I
网红小智智 星瞳科技

因为你的代码不对呗。

你的代码里死循环里面有两个sensor.snapshot(),所以“一抖一抖”。

看视频,你需要把你写的每一句代码都读懂。

https://singtown.com/learn/50029/