# Template Matching Example - Normalized Cross Correlation (NCC)
#
# 这个例子展示了如何使用OpenMV凸轮的NCC功能将小部分图像与图像的各个部分
# 进行匹配...期望获得极其可控的环境NCC并不是全部有用的。
#
# 警告:NCC支持需要重做!到目前为止,这个功能需要做大量的工作才能有用。
# 这个脚本将重新表明功能的存在,但在目前的状态是不足的。
import time, sensor, image
from image import SEARCH_EX, SEARCH_DS
from pyb import UART
thresholds = [(30, 100, 15, 127, 15, 127), # generic_red_thresholds -> index is 0 so code == (1 << 0)
(30, 100, -64, -8, -32, 32), # generic_green_thresholds -> index is 1 so code == (1 << 1)
(0, 15, 0, 40, -80, -20)] # generic_blue_thresholds -> index is 2 so code == (1 << 2)
sensor.reset()
sensor.set_contrast(1)
sensor.set_gainceiling(16)
sensor.set_framesize(sensor.QQVGA)
sensor.set_pixformat(sensor.GRAYSCALE)
templates1 = ["0.pgm"] #保存多个模板
templates2 = ["1.pgm"] #加载模板图片
uart = UART(3, 115200)
uart.init(115200, bits=8, parity=None, stop=1) # 8位数据位,无检验位,1位停止位
tmp = 0
clock = time.clock()
while (True):
clock.tick()
img = sensor.snapshot()
for t in templates1:
template = image.Image(t)
r = img.find_template(template, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
if r:
img.draw_rectangle(r)
img.draw_string(10,10, "zhituan")
tmp = uart.write('d')
print(tmp,t)
if t:
img = img.to_rgb656()
for blob in img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200):
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
print(blob.code())
for t in templates2:
template = image.Image(t)
r = img.find_template(template, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))
if r:
img.draw_rectangle(r)
img.draw_string(10,10, "dianchi")
tmp = uart.write('0')
print(tmp,t)
if t:
img = img.to_rgb656()
for blob in img.find_blobs(thresholds, pixels_threshold=200, area_threshold=200):
img.draw_rectangle(blob.rect())
img.draw_cross(blob.cx(), blob.cy())
print(blob.code())
#print(clock.fps())