Template matching on the fourth day of Openmv

What I learned a few days ago was only about the recognition of color and Apriltag, but what if the color is the same? It is not a thing to recognize that thing, which is not good, so template matching should be derived. You give a template picture in the openmv memory, that is, the appearance of your target, and you can find the target that must be like this according to the appearance. What does it mean? I understand the meaning of certain to mean both size and shape. Basically the same, it doesn't matter if it's too small or too big, and the shape should be roughly equal. The ncc algorithm is used, which can only match patterns that are basically the same size and angle as the template image. The limitation is relatively large. If the target pattern in the field of view is slightly larger or smaller than the template image, the matching may not be successful.

So where is the use?

  • 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 suitable for the car to track a moving volleyball (because the distance between the moving volleyball and the camera is dynamic, the size of the volleyball seen by the camera will change, and will not be exactly the same as the template picture) .

Below is the official code

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

Here is a very important function at a glance

那就是image.find_template(templatethreshold[, roi[, step=2[, search=image.SEARCH_EX]]])

This function is to return the roi of the template you are looking for, so that you can frame it. Is it framed? QQVGA is also required, because in order to increase the speed!

What are the parameters passed in?

1: A picture of the template you are looking for

2: Threshold value, it seems inexplicable, it feels like you give a large value that is accurate but slow, and a small value is fast but inaccurate. Small debugging is useful.

3: It is also an inexplicable thing, that is, to ignore the number of pixels when you are looking for a template, it is also for the speed of the algorithm.

4: This is the way to find it. Most of the internal things use this Ex. By the way, 3 must be usefully assigned under EX.

What he returns is a roi. If there is no one, it is none, so it is obvious that you can use if to judge whether it is found. If it is found, use roi and directly draw a frame to frame it.

It should be noted that you need to introduce the search method module and insert an sd card to save your template pictures, and the format should be changed to pmg

It's just that you can use the buffer to draw a roi to save the picture is also allowed.

Another most important template recognition is the grayscale image, that is, it is either a white or black image, and those colored circles or frames cannot be used, and it is not impossible to use it. It is useless if used.

What about multiple templates, I want to identify multiple things, or different looks of one thing

the first saved thing

templates = ["/0.pgm", "/1.pgm", "/2.pgm", "/6.pgm"] 

Then what? With so many things, he can only recognize one at a time. Of course, use the previous for loop to traverse one by one.

 for t in templates:
        template = image.Image(t)
        #对每个模板遍历进行模板匹配
        r = img.find_template(template, 0.70, step=4, search=SEARCH_EX) #, roi=(10, 0, 60, 60))

 Then the frame below will be 0.0

/./ If you are not bothered, you can capture many photos of an object, and then it can be recognized even if it is zoomed in or zoomed out. 0..0

Guess you like

Origin blog.csdn.net/weixin_63163242/article/details/128411112