星瞳科技OpenMV視頻教程08-NCC模板匹配

https://www.zhihu.com/video/973249409572630528

星瞳科技OpenMV官方代理視頻地址:點擊這裡!

嗶哩嗶哩地址:

星瞳科技OpenMV視頻教程08-NCC模板匹配_野生技術協會_科技_bilibili_嗶哩嗶哩

NCC演算法:

# Template Matching Example - Normalized Cross Correlation (NCC)## This example shows off how to use the NCC feature of your OpenMV Cam to match# image patches to parts of an image... expect for extremely controlled enviorments# NCC is not all to useful.## WARNING: NCC supports needs to be reworked! As of right now this feature needs# a lot of work to be made into somethin useful. This script will reamin to show# that the functionality exists, but, in its current state is inadequate.import time, sensor, imagefrom image import SEARCH_EX, SEARCH_DS#從imgae模塊引入SEARCH_EX和SEARCH_DS。使用from import僅僅引入SEARCH_EX, #SEARCH_DS兩個需要的部分,而不把image模塊全部引入。# Reset sensorsensor.reset()# Set sensor settingssensor.set_contrast(1)sensor.set_gainceiling(16)# Max resolution for template matching with SEARCH_EX is QQVGAsensor.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)# Load template.# Template should be a small (eg. 32x32 pixels) grayscale image.template = image.Image("/template.pgm")#載入模板圖片clock = time.clock()# Run template matchingwhile (True): clock.tick() img = sensor.snapshot() # find_template(template, threshold, [roi, step, search]) # ROI: The region of interest tuple (x, y, w, h). # Step: The loop step used (y+=step, x+=step) use a bigger step to make it faster. # Search is either image.SEARCH_EX for exhaustive search or image.SEARCH_DS for diamond search # # Note1: ROI has to be smaller than the image and bigger than the template. # Note2: In diamond search, step and ROI are both ignored. 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小。 #把匹配到的圖像標記出來 if r: img.draw_rectangle(r) print(clock.fps())

注意,由於我們的模板圖片大小要超過openmv內置的flash,所以我們需要插上sd卡後進行下列步驟。(注意先插sd卡再上電哦) 而且此模板匹配只能用於1.6及以上版本的固件哦,否則運行時會提示 「can not find SEARCH_EX」哦

首先,我們需要創建或導入一個模板,注意這個模板必須得是pgm格式的,而且大小有限制,不能超過openmv的像素大小。 我們可以直接從openmv裡面截取一個模板圖像,可以先運行helloworld.py常式,讓frambuffer顯示出圖像,然後進行截取。

選擇 save image selection to pc,注意從openmv裡面直接截取保存的圖片是bmp格式的,我們需要把它轉換成pgm格式。可以在這個網站進行在線轉換convertio.co/zh/bmp-pgm

然後,我們將轉換完的pgm模板保存到sd卡中(這個sd卡中一共有8個模板,上圖的模板存為了ball0.pgm)

然後我們打開模板匹配的常式

把第28行的模板文件名template.pgm改成剛剛的ball0.pgm

然後直接運行就可以啦!

這個是模板匹配函數find_template的用法:

r = img.find_template(template, 0,7, roi=(10,0,80,60), step=4, search=SEARCH_EX) threshold中的0.7是相似度閾值,roi是進行匹配的區域(左上頂點為(10,0),長80寬60的矩形),注意roi的大小要比模板圖片大,比frambuffer小。

注意這個模板匹配用的是ncc演算法,只能匹配出與模板大小相似的區域哦,如果要匹配不同大小的圖片,需要保存多個不同大小的模板哦。

如果運行程序後出現以下問題:

1.模版圖片太大,建議模版圖片小於80*60

2.OpenMV2內存不夠,要把QQVGA改成QQCIF

歡迎大家點贊!!!


推薦閱讀:

然後製作視頻版雙重曝光效果?只需兩步操作,學不會你揍我
Python基礎視頻教程全集分享
民謠吉他視頻教程【入門】
星瞳科技OpenMV視頻教程05-升級固件

TAG:教程 | 視頻教程 | 模板C |