Template matching NCC

Video tutorial 8 - NCC template matching:https://singtown.com/learn/49598/

NCC algorithm:

# 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, 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)

# Load template.
# Template should be a small (eg. 32x32 pixels) grayscale image.
template = image.Image("/template.pgm")
#加载模板图片

clock = time.clock()

# Run template matching
while (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())

Note that since the size of our template image exceeds the built-in flash of openmv, we need to insert the SD card before performing the following steps. (Note that you should insert the SD card before powering on) And this template matching can only be used for firmware version 1.6 and above, otherwise it will prompt "can not find SEARCH_EX" during operation

First, we need to create or import a template. Note that this template must be in pgm format and has a size limit that cannot exceed the pixel size of openmv. We can directly capture a template image from openmv. You can first run the helloworld.py routine to let frambuffer display the image, and then capture it.

Select save image selection to pc. Note that the image directly captured and saved from openmv is in bmp format. We need to convert it to pgm format. You can do online conversion on this website https://convertio.co/zh/bmp-pgm/

Then, we save the converted pgm template to the sd card (there are 8 templates in this sd card, and the template in the above picture is saved as ball0.pgm)

Then we open the template matching routine

Change the template file name template.pgm in line 28 to ball0.pgm just now

And run it directly!

This is the usage of the template matching function find_template:

r = img.find_template(template, 0,7, roi=(10,0,80,60), step=4, search=SEARCH_EX) The 0.7 in threshold is the similarity threshold, and roi is the matching area (the upper left vertex is (10, 0), the length is 80 and the width is 60). Note that the size of roi should be larger than the template image and smaller than frambuffer.

Note that this template matching uses the ncc algorithm, which can only match areas similar to the template size. If you want to match pictures of different sizes, you need to save multiple templates of different sizes.

If the following problems occur after running the program:

  1. The template image is too large, it is recommended that the template image is smaller than 80*60

  1. OpenMV2 does not have enough memory, change QQVGA to QQCIF


Comparison of template matching and feature point detection:
  • Template matching (find_temolate) uses the ncc algorithm, which can only match patterns that are basically the same size and angle as the template image. The limitations are relatively large. If the target pattern in the field of view is slightly larger or smaller than the template image, the match may not be successful.

  • Template matching is suitable for situations where the distance between the camera and the target object is determined and dynamic movement is not required. For example, it is suitable for the detection of specific objects on the assembly line, but not for the car to track a moving volleyball (because the distance between the moving volleyball and the camera is dynamic, and the size of the volleyball seen by the camera will change, and it will not be exactly the same as the template image).

  • For multi-angle and multi-size matching, you can try to save multiple templates and use Multiple template matching.

  • Feature point detection (find_keypoint): If you just started running the program, the routine extracts the first image as the target object feature, and kpts1 saves the target object feature. By default, it will match multiple scales and angles of the target feature, rather than just saving the size and angle of the target feature. It is more flexible than template matching and does not need to save multiple template images like multi-template matching.

  • Feature point detection can also save the target features in advance. This was not recommended before because interference from ambient light and other reasons may cause different features in different light each time the program is run, and the matching degree will be reduced. However, in the latest version of the firmware, the adjustment of exposure, white balance, and automatic gain value has been added. The exposure value and white balance value can be manually defined, which will relatively reduce the interference of light. You can also try to save the target features in advance.


results matching ""

    No results matching ""