# Template Matching Example - Normalized Cross Correlation (NCC)
#
# 这个例子展示了如何使用OpenMV凸轮的NCC功能将小部分图像与图像的各个部分
# 进行匹配...期望获得极其可控的环境NCC并不是全部有用的。
#
# 警告:NCC支持需要重做!到目前为止,这个功能需要做大量的工作才能有用。
# 这个脚本将重新表明功能的存在,但在目前的状态是不足的。
import time, sensor, image
from image import SEARCH_EX, SEARCH_DS
#从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)
clock = time.clock()
# Load template.
# Template should be a small (eg. 32x32 pixels) grayscale image.
templates1 = ["/1.1.pgm","/1.2.pgm","/1.3.pgm","/1.4.pgm","/1.5.pgm","/1.6.pgm","/1.7.pgm","/1.8.pgm","/1.9.pgm","/1.10.pgm","/1.11.pgm","/1.12.pgm","/1.13.pgm","/1.14.pgm","/1.15.pgm","/1.16.pgm","/1.17.pgm","/1.18.pgm","/1.19.pgm"]
templates2 = ["/2.1.pgm","/2.2.pgm","/2.3.pgm","/2.4.pgm","/2.5.pgm","/2.6.pgm","/2.7.pgm","/2.8.pgm","/2.9.pgm","/2.10.pgm","/2.11.pgm","/2.12.pgm","/2.13.pgm","/2.14.pgm","/2.15.pgm","/2.16.pgm","/2.17.pgm","/2.18.pgm","/2.19.pgm"]
templates3 = ["/3.1.pgm","/3.2.pgm","/3.3.pgm","/3.4.pgm","/3.5.pgm","/3.6.pgm","/3.7.pgm","/3.8.pgm","/3.9.pgm","/3.10.pgm","/3.11.pgm","/3.12.pgm","/3.13.pgm","/3.14.pgm","/3.15.pgm","/3.16.pgm","/3.17.pgm","/3.18.pgm","/3.19.pgm"]
templates4 = ["/4.1.pgm","/4.2.pgm","/4.3.pgm","/4.4.pgm","/4.5.pgm","/4.6.pgm","/4.7.pgm","/4.8.pgm","/4.9.pgm","/4.10.pgm","/4.11.pgm","/4.12.pgm","/4.13.pgm","/4.14.pgm","/4.15.pgm","/4.16.pgm","/4.17.pgm","/4.18.pgm","/4.19.pgm"]
templates5 = ["/5.1.pgm","/5.2.pgm","/5.3.pgm","/5.4.pgm","/5.5.pgm","/5.6.pgm","/5.7.pgm","/5.8.pgm","/5.9.pgm","/5.10.pgm","/5.11.pgm","/5.12.pgm","/5.13.pgm","/5.14.pgm","/5.15.pgm","/5.16.pgm","/5.17.pgm","/5.18.pgm","/5.19.pgm"]
templates6 = ["/6.1.pgm","/6.2.pgm","/6.3.pgm","/6.4.pgm","/6.5.pgm","/6.6.pgm","/6.7.pgm","/6.8.pgm","/6.9.pgm","/6.10.pgm","/6.11.pgm","/6.12.pgm","/6.13.pgm","/6.14.pgm","/6.15.pgm","/6.16.pgm","/6.17.pgm","/6.18.pgm","/6.19.pgm"]
templates7 = ["/7.1.pgm","/7.2.pgm","/7.3.pgm","/7.4.pgm","/7.5.pgm","/7.6.pgm","/7.8.pgm","/7.9.pgm","/7.10.pgm","/7.11.pgm","/7.12.pgm","/7.13.pgm","/7.14.pgm","/7.16.pgm","/7.17.pgm","/7.18.pgm","/7.19.pgm"]
templates8 = ["/8.1.pgm","/8.2.pgm","/8.3.pgm","/8.4.pgm","/8.5.pgm","/8.6.pgm","/8.7.pgm","/8.8.pgm","/8.9.pgm","/8.10.pgm","/8.11.pgm","/8.12.pgm","/8.13.pgm","/8.14.pgm","/8.15.pgm","/8.16.pgm","/8.17.pgm","/8.18.pgm","/8.19.pgm"]
for t in templates1: templates1[templates1.index(t)] = image.Image(t)
for t in templates2: templates2[templates2.index(t)] = image.Image(t)
for t in templates3: templates3[templates3.index(t)] = image.Image(t)
for t in templates4: templates4[templates4.index(t)] = image.Image(t)
for t in templates5: templates5[templates5.index(t)] = image.Image(t)
for t in templates6: templates6[templates6.index(t)] = image.Image(t)
for t in templates7: templates7[templates7.index(t)] = image.Image(t)
for t in templates8: templates8[templates8.index(t)] = image.Image(t)
#找数字函数:(返回数字的外框)
def findNum(img, templates):
img_gray = img.to_grayscale() #变成灰度图
if(templates!=-1):
for t in templates: #遍历模板列表
rect = img_gray.find_template(t, 0.65, step=7, search=SEARCH_EX)
if rect!=None: return rect
return 0
# Run template matching
while (True):
clock.tick()
img = sensor.snapshot()
Rect = findNum(img, templates4)
if Rect!=0: #如果用某个数字的模板找到了数字
img.draw_rectangle(Rect) #白框框起该数字
print(clock.fps())
K
k6ty 发布的帖子
-
MemoryError: memory allocation failed, allocating 1147 bytes