logo

如何判断模板匹配物体在屏幕中间

2018-08-06 16:34
K
gnb2
请在这里粘贴代码
import time, sensor, image, pyb
from image import SEARCH_EX, SEARCH_DS
from pyb import Pin

sensor.reset()
sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.QQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)

template1 = image.Image("/1.pgm")
template2 = image.Image("/2.pgm")
template3 = image.Image("/3.pgm")
p_out1 = Pin('P1', Pin.OUT_PP)
p_out2 = Pin('P2', Pin.OUT_PP)
p_out3 = Pin('P3', Pin.OUT_PP)
p_out1.low()
p_out2.low()
p_out3.low()
clock = time.clock()


while (True):
    clock.tick()
    img = sensor.snapshot()
    r1 = img.find_template(template1, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    r2 = img.find_template(template2, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    r3 = img.find_template(template3, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    if r1:
        img.draw_rectangle(r1)
        x = r1[0]
        p_out1.high()
    else:
        p_out1.low()
    if r2:
        img.draw_rectangle(r2)
        p_out2.high()
    else:
        p_out2.low()
    if r3:
        img.draw_rectangle(r3)
        p_out3.high()
    else:
        p_out3.low()

    print(clock.fps())

怎么在找到物体并用白框圈住后,判断白框是否在屏幕的中间。小白求教高手。

用x=r1[0]求得图案中心坐标对吗?

2018-08-07 09:21
X
Yuan

r是返回的目标物体矩形框,r.x() r.y() r.w() r.h()

屏幕中心坐标是img.width()/2 img.height()/2

做比较即可。