logo

为什么模板匹配 我打印t 是一堆数字 按官方的应该是我的文件名

2021-11-09 18:06
9
bxr1
import time, sensor, image,pyb
from image import SEARCH_EX, SEARCH_DS
from pyb import UART
import ustruct
from pyb import LED
one=LED(1)
#从imgae模块引入SEARCH_EX和SEARCH_DS。使用from import仅仅引入SEARCH_EX,
#SEARCH_DS两个需要的部分,而不把image模块全部引入。
clock = time.clock()
# Reset sensor
sensor.reset()

# Set sensor settings
sensor.set_contrast(1)      #设置对比度
sensor.set_gainceiling(16)  #设定增益曲线
sensor.set_framesize(sensor.QQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)
#串口3定义
uart1 = UART(3,115200)   #定义串口3变量
uart1.init(115200, bits=8, parity=None, stop=1) # init with given parameters




def sending_data(cx,cy):
    global uart1;
    #frame=[0x2C,18,cx%0xff,int(cx/0xff),cy%0xff,int(cy/0xff),0x5B];
    #data = bytearray(frame)
    datasum=ustruct.pack("<bbhhhhb",      #格式为俩个字符俩个短整型(2字节)
                    0x2C,                      #帧头1
                    0x12,                      #帧头2
                    int(cx), # up sample by 4   #数据1
                    int(cy), # up sample by 4   #数据1
                    0x5B)

    uart1.write(datasum);   #必须要传入一个字节数组
    

#初始模板
templatedemo = ["/1.pgm", "/2.pgm", "/3.pgm", "/4.pgm","/5.pgm", "/6.pgm", "/7.pgm", "/8.pgm"]
##分类模板
templates = (["/3.pgm", "/32.pgm", "/33.pgm", "/34.pgm","/35.pgm", "/36.pgm"],
            ["/4.pgm", "/42.pgm", "/43.pgm", "/44.pgm","/45.pgm", "/46.pgm"],
            ["/5.pgm", "/52.pgm", "/53.pgm", "/54.pgm","/55.pgm", "/56.pgm"],
            ["/6.pgm", "/62.pgm", "/63.pgm", "/64.pgm","/65.pgm", "/66.pgm"],
            ["/7.pgm", "/72.pgm", "/73.pgm","/74.pgm","/75.pgm", "/76.pgm","/701.pgm","/702.pgm","/703.pgm","/704.pgm","/705.pgm","/706.pgm"],
            ["/8.pgm","/82.pgm", "/83.pgm", "/84.pgm","/85.pgm", "/86.pgm","/801.pgm","/802.pgm","/803.pgm","/804.pgm","/805.pgm","/806.pgm"]) #8\

imgarray=[0,0,0,0,0,0,0,0]
imgarrays=[0,0,0,0,0,0,0,0,0,0,0,0]

first=0
cx=0
cy=0

def get_first(img):#病房号识别输入 返回一到八 判断初始模板
    #print("get_first")
    global imgarray
    r = img.find_template(imgarray[0], 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    if r:
        img.draw_rectangle(r)
        return 1
    r = img.find_template(imgarray[1], 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    if r:
        img.draw_rectangle(r)
        return 2
    r = img.find_template(imgarray[2], 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    if r:
        img.draw_rectangle(r)
        return 3
    r = img.find_template(imgarray[3], 0.7 ,step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    if r:
        img.draw_rectangle(r)
        return 4
    r = img.find_template(imgarray[4], 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    if r:
        img.draw_rectangle(r)
        return 5
    r = img.find_template(imgarray[5], 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    if r:
        img.draw_rectangle(r)
        return 6
    r = img.find_template(imgarray[6], 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    if r:
        img.draw_rectangle(r)
        return 7
    r = img.find_template(imgarray[7], 0.7, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
    if r:
        img.draw_rectangle(r)
        return 8
    return 0


'''def isget(num):#判断是否输入成功
    if num is not 0:
        for i in range (num):
            one.on()
            print(num) #使用时删除 给自己看的
            time.sleep_ms(100)
            one.off()
            time.sleep_ms(100)
            cx = num'''



def getimgs(imgarr):
    i=0
    #print("getimgs")
    for t in imgarr:
        imgarrays[i]=image.Image((t))
        #print(imgarrays[i])
        i+=1
    return imgarrays

def match_img(photos,img):#模板匹配
    isget=0
    #print("match_img")
    for t in photos:
        print("tt",t)
        r = img.find_template(t, 0.50, step=4, search=SEARCH_EX)#不能将int转换为type
       
        if r:
            img.draw_rectangle(r)
            isget=1
           
            break
        else:
            pass
    if isget==1:
        sending_data(0x37,0x12) #55
        isget=0
    else:
        sending_data(0x73,0x21)  #115

def loadimg():  #导入正常图片八张
    #print("loading")
    i=0
    for t in templatedemo:
        imgarray[i]=image.Image(str(t))
        i+=1
    return imgarray


imgarray=loadimg()  #加载初始八张图


while (True):
    clock.tick()
    img = sensor.snapshot()
    if imgarrays[0]!=0:
        match_img(imgarrays,img)#比对当前帧图片与加载进来的模板库匹配
        '''cy=int(img)'''
        print("y:",cy)
    else:
        if first==0:  #获取初始图片标志位
            first=get_first(img)  #first用于判断当前是那张图片
            if first is not 0:
                for i in range (first):
                    one.on()
                    print(first) #使用时删除 给自己看的
                    time.sleep_ms(100)
                    one.off()
                    time.sleep_ms(100)
                    cx = first#亮灯提醒是否识别到初始图片
                    print("1:",cx)
        elif first==1:
            sending_data(0x10) # 确定是1
        elif first==2:
            sending_data(0x20) #确定是2
        elif imgarrays[0]==0:  #确定图片缓冲区第一次是空
            imgarrays=getimgs(templates[first-3])#load photo 加载图片 执行一次
        else:#之后程序只会在这里执行
            pass
   
    FH = bytearray([0x2C,0x12,cx,cy,0x5B])
    print("6:",cx)
    uart1.write(FH)
    print("FPS:",clock.fps())#帧率显示

print(t)的值:tt {"w":70, "h":96, "type"="grayscale", "size":6720}

tt {"w":60, "h":68, "type"="grayscale", "size":4080}

tt {"w":60, "h":68, "type"="grayscale", "size":4080}

tt {"w":60, "h":68, "type"="grayscale", "size":4080}

tt {"w":60, "h":68, "type"="grayscale", "size":4080}

tt {"w":60, "h":68, "type"="grayscale", "size":4080}

tt {"w":60, "h":65, "type"="grayscale", "size":3900}

tt {"w":60, "h":48, "type"="grayscale", "size":2880}

tt {"w":60, "h":53, "type"="grayscale", "size":3180}

tt {"w":60, "h":53, "type"="grayscale", "size":3180}

tt {"w":60, "h":47, "type"="grayscale", "size":2820}

tt {"w":60, "h":53, "type"="grayscale", "size":3180}

9
bxr1

我的模板匹配为什么打印的是这个。 我目的是为了得到这个扫描到模板的文件名 然后写它在列表的哪个位置 现在它怎么打印这些东西

2021-11-10 14:02
I
网红小智智 星瞳科技

你的photos是图片数组,不是字符串数组。

2021-11-10 15:01
9
bxr1

@kidswong999 那请问 我该怎么样才能得到一个参数 让它输出的是这个扫描到的数字号 现在我只能知道它是否扫到 。不知道是数字几 传输不出去

2021-11-10 19:38
I
网红小智智 星瞳科技

https://forum.singtown.com/topic/7877/怎么才能知道扫描到这张图片在我的templates组中-是在什么位置-templates-1-还是多少