import time, sensor, image
from image import SEARCH_EX, SEARCH_DS
from pyb import UART
import ustruct
#从imgae模块引入SEARCH_EX和SEARCH_DS。使用from import仅仅引入SEARCH_EX,
#SEARCH_DS两个需要的部分,而不把image模块全部引入。
# Reset sensor
sensor.reset()
# Set sensor settings
sensor.set_contrast(1)
sensor.set_gainceiling(16)
# Max resolution for template matching with SEARCH_EX is QQVGA
sensor.set_framesize(sensor.QQVGA)
# You can set windowing to reduce the search image.
#sensor.set_windowing(((640-80)//2, (480-60)//2, 80, 60))
sensor.set_pixformat(sensor.GRAYSCALE)
uart = UART(3,115200) #定义串口3变量
uart.init(115200, bits=8, parity=None, stop=1) # init with given parameters
# Load template.
# Template should be a small (eg. 32x32 pixels) grayscale image.
Template = [image.Image("/5.pgm"),image.Image("/6.pgm"),image.Image("/7.pgm"),image.Image("/8.pgm")]
#加载模板图片
clock = time.clock()
# Run template matching
while (True):
clock.tick()
img = sensor.snapshot()
#r = img.find_template(template, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
#find_template(template, threshold, [roi, step, search]),threshold中
#的0.7是相似度阈值,roi是进行匹配的区域(左上顶点为(10,0),长80宽60的矩形),
#注意roi的大小要比模板图片大,比frambuffer小。
#把匹配到的图像标记出来
for i in range(4):
r = img.find_template(Template[i], 0.70, step=4, search=SEARCH_EX)
if r:
img.draw_rectangle(r)
num = i + 5
print(num)
FH = bytearray([0x2C,0x12,num,0x5B])
uart.write(FH)
OpenMv多模板匹配
最新推荐文章于 2024-05-16 21:32:14 发布